From f61c219f11cc8f82341a24a46ff27f45948e7a22 Mon Sep 17 00:00:00 2001
From: Guillaume Vagner <guillaume.vagner@supelec.fr>
Date: Thu, 21 Feb 2019 00:39:21 +0100
Subject: [PATCH] merge

---
 connection-db.js                          | 109 ----------------------
 models/Channel.js                         |  14 +++
 mongoose.js                               |   9 ++
 node_modules/@types/node/ts3.1/index.d.ts |  18 ----
 node_modules/@types/node/ts3.1/util.d.ts  |  10 --
 5 files changed, 23 insertions(+), 137 deletions(-)
 delete mode 100644 connection-db.js
 create mode 100644 models/Channel.js
 create mode 100644 mongoose.js
 delete mode 100644 node_modules/@types/node/ts3.1/index.d.ts
 delete mode 100644 node_modules/@types/node/ts3.1/util.d.ts

diff --git a/connection-db.js b/connection-db.js
deleted file mode 100644
index 6470d95..0000000
--- a/connection-db.js
+++ /dev/null
@@ -1,109 +0,0 @@
-var mysql = require('promise-mysql');
-
-const config = require('./config');
-
-
-
-function query(req) {
-    return mysql.createConnection(
-        config.mysql
-    ).then(connection => {
-        return Promise.all([
-            connection.query(req),
-            connection
-        ])
-    }).then(([rep, connection]) => {
-        connection.end();
-        return rep;
-    }).catch(error => {
-        //logs out the error
-        console.error(error);
-    });
-}
-
-
-Promise.all([
-    query(`CREATE TABLE IF NOT EXISTS channel(
-    chatId INT PRIMARY KEY NOT NULL,
-    username TEXT,
-    state TEXT,
-    token TEXT,
-    refresh TEXT,
-    expiration TEXT,
-    schedule TEXT
-)`),
-
-    query(`CREATE TABLE IF NOT EXISTS groups(
-    chatId INT NOT NULL,
-    grp INT,
-    primary key (chatId, grp)
-)`)
-]).then(_ => {
-
-    console.log('[mariadb] connected to database')
-
-})
-
-
-
-function getChanByChatId(chatId) {
-    return query(`
-    SELECT *
-    FROM channel
-    WHERE chatId = ${chatId}
-    `).then(rep => rep[0]);
-}
-
-function createChan(data) {
-    return query(`
-    INSERT INTO channel
-    VALUES (${data.chatId}, "${data.username}", "${data.state}", "${data.token}", "${data.refresh}", "${data.expiration}", "${data.schedule}")
-    `)
-}
-
-function deleteChanByChatId(chatId) {
-    return query(`DELETE FROM channel WHERE chatId = ${chatId}`);
-}
-
-function modifyChan(data) {
-    return query(`
-        UPDATE channel
-        SET username = "${data.username}",
-            state = "${data.state}",
-            token = "${data.token}",
-            refresh = "${data.refresh}",
-            expiration = "${data.expiration}",
-            schedule = "${data.schedule}"
-        WHERE chatId = ${data.chatId}
-    `).then(_ => {
-        return getChanByChatId(data.chatId)
-    })
-}
-
-function getChanByState(state) {
-    return query(`
-    SELECT *
-    FROM channel
-    WHERE state = "${state}"
-    `).then(rep => rep[0]);
-}
-
-function addGroup(chatId, groupId) {
-    return query(`
-    INSERT INTO groups
-    VALUES (${chatId}, ${groupId})
-    `);
-}
-
-function getGroups(chatId) {
-    return query(`
-    SELECT grp
-    FROM groups
-    WHERE chatId = ${chatId}
-    `).then(rep => {
-        return rep.map(element => element.grp);
-    })
-}
-
-
-module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups };
\ No newline at end of file
diff --git a/models/Channel.js b/models/Channel.js
new file mode 100644
index 0000000..e2f569b
--- /dev/null
+++ b/models/Channel.js
@@ -0,0 +1,14 @@
+const mongoose = require('../mongoose');
+
+const channelSchema = new mongoose.Schema({
+    token: String,
+    refresh: String,
+    expiration: Date,
+    username: String,
+    chatId: Number,
+    state: String,
+    groups: [Number],
+    scheduleTime: { type: String, validate: /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/ }
+})
+
+module.exports = mongoose.model('Channel', channelSchema);
\ No newline at end of file
diff --git a/mongoose.js b/mongoose.js
new file mode 100644
index 0000000..077b033
--- /dev/null
+++ b/mongoose.js
@@ -0,0 +1,9 @@
+const mongoose = require('mongoose');
+
+
+mongoose.Promise = global.Promise;
+mongoose.connect('mongodb://localhost/botday', { useNewUrlParser: true })
+    .then(() => console.log('[mongoose] Connection succesful'))
+    .catch((err) => console.error(err));
+
+module.exports = mongoose;
\ No newline at end of file
diff --git a/node_modules/@types/node/ts3.1/index.d.ts b/node_modules/@types/node/ts3.1/index.d.ts
deleted file mode 100644
index be36602..0000000
--- a/node_modules/@types/node/ts3.1/index.d.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-// NOTE: These definitions support NodeJS and TypeScript 3.1.
-
-// NOTE: TypeScript version-specific augmentations can be found in the following paths:
-//          - ~/base.d.ts         - Shared definitions common to all TypeScript versions
-//          - ~/index.d.ts        - Definitions specific to TypeScript 2.1
-//          - ~/ts3.1/index.d.ts  - Definitions specific to TypeScript 3.1
-
-// Reference required types from the default lib:
-/// <reference lib="es2018" />
-/// <reference lib="esnext.asyncIterable" />
-/// <reference lib="esnext.intl" />
-
-// Base definitions for all NodeJS modules that are not specific to any version of TypeScript:
-// tslint:disable-next-line:no-bad-reference
-/// <reference path="../base.d.ts" />
-
-// TypeScript 3.1-specific augmentations:
-/// <reference path="util.d.ts" />
diff --git a/node_modules/@types/node/ts3.1/util.d.ts b/node_modules/@types/node/ts3.1/util.d.ts
deleted file mode 100644
index e35ef8d..0000000
--- a/node_modules/@types/node/ts3.1/util.d.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-// tslint:disable-next-line:no-bad-reference
-/// <reference path="../util.d.ts" />
-declare module "util" {
-    namespace inspect {
-        const custom: unique symbol;
-    }
-    namespace promisify {
-        const custom: unique symbol;
-    }
-}
-- 
GitLab