diff --git a/index.js b/index.js index 462b377..73cd622 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ import conf from "./src/conf/index.js"; -import Bot from "./src/bot/bot.js"; -import Router from "./src/routes.js"; +import Bot from "./src/controllers/bot.controller.js"; +import Router from "./src/routes/index.routes.js"; const router = new Router(); router.registerRoutes(); diff --git a/src/conf/index.js b/src/conf/index.js index f359287..215baad 100644 --- a/src/conf/index.js +++ b/src/conf/index.js @@ -15,8 +15,5 @@ export default { }, server: { port: 3000, - }, - database: { - connector: "Mongoose" } }; diff --git a/src/bot/bot.js b/src/controllers/bot.controller.js similarity index 82% rename from src/bot/bot.js rename to src/controllers/bot.controller.js index 1e93452..e747eee 100644 --- a/src/bot/bot.js +++ b/src/controllers/bot.controller.js @@ -1,6 +1,6 @@ import { Telegraf } from "telegraf"; -import Commands from "./commands.js" -import LichessService from "../services/lichess/lichess.js"; +import Commands from "./commands.controller.js" +import LichessService from "../services/lichess.service.js"; export default class Bot { diff --git a/src/bot/commands.js b/src/controllers/commands.controller.js similarity index 100% rename from src/bot/commands.js rename to src/controllers/commands.controller.js diff --git a/src/database/connectors/index.js b/src/database/connectors/index.js deleted file mode 100644 index 08f0037..0000000 --- a/src/database/connectors/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import conf from "../../conf/index.js"; -import MongooseConnector from "./mongoose.js"; - -export default class DBConnector { - - constructor() { - this.connectorType = conf.database.connector; - this.connector = null; - } - - getConnector() { - if (this.connector !== null) { - return this.connector; - } - - if (this.connectorType === "Mongoose") { - this.connector = new MongooseConnector(); - } else { - // default connector - this.connector = new MongooseConnector(); - } - - return this.connector; - } - -} diff --git a/src/database/connectors/mongoose.js b/src/database/db-connector.js similarity index 78% rename from src/database/connectors/mongoose.js rename to src/database/db-connector.js index 40fbe84..0f933ad 100644 --- a/src/database/connectors/mongoose.js +++ b/src/database/db-connector.js @@ -1,7 +1,7 @@ import mongoose from 'mongoose'; -import conf from "../../conf/index.js"; +import conf from "../conf/index.js"; -export default class MongooseConnector { +export default class DBConnector { constructor() { const { dbUserName, dbPassword, dbName } = conf.mongodb; @@ -22,7 +22,11 @@ export default class MongooseConnector { } } - close() { + disconnect() { mongoose.connection.close(); } + + buildModel(type, schema) { + return mongoose.model(type, mongoose.Schema(schema)); + } } diff --git a/src/database/schemas.js b/src/database/schemas.js new file mode 100644 index 0000000..f418a21 --- /dev/null +++ b/src/database/schemas.js @@ -0,0 +1,25 @@ +// eslint-disable-next-line import/prefer-default-export +export const userSchema = { + id: { + type: String, + required: true, + trim: true, + index: true, + unique: true, + }, + name: { + type: String, + required: true, + lowercase: true, + trim: true, + index: true, + }, + email: { + type: String, + required: true, + lowercase: true, + trim: true, + index: true, + unique: true, + } +}; \ No newline at end of file diff --git a/src/models/lichess.js b/src/models/lichess.js deleted file mode 100644 index 0d61c34..0000000 --- a/src/models/lichess.js +++ /dev/null @@ -1,17 +0,0 @@ -import DBConnector from "../database/connectors/index.js"; - -export default class LichessModel { - - constructor() { - this.connector = new DBConnector().getConnector(); - this.connector.connect(); - } - - login(chatId) { - console.log(chatId); - } - - getLiveGames() { - console.log("getLiveGames"); - } -} diff --git a/src/models/user.model.js b/src/models/user.model.js new file mode 100644 index 0000000..dc20e26 --- /dev/null +++ b/src/models/user.model.js @@ -0,0 +1,18 @@ +import DBConnector from "../database/db-connector.js"; +import { userSchema } from "../database/schemas.js"; + +export default class UserModel { + constructor() { + this.connector = new DBConnector(); + this.connection = this.connector.connect(); + this.user = this.connector.buildModel("User", userSchema); + } + + login(chatId) { + console.log(chatId); + } + + getLiveGames() { + console.log("getLiveGames"); + } +} diff --git a/src/routes.js b/src/routes/index.routes.js similarity index 81% rename from src/routes.js rename to src/routes/index.routes.js index bf73827..9e65f33 100644 --- a/src/routes.js +++ b/src/routes/index.routes.js @@ -1,7 +1,7 @@ import express from "express"; import passport from "passport"; -import conf from "./conf/index.js"; -import LichessRouter from "./services/lichess/router-lichess.js"; +import conf from "../conf/index.js"; +import LichessRouter from "./lichess.routes.js"; export default class Router { diff --git a/src/services/lichess/router-lichess.js b/src/routes/lichess.routes.js similarity index 90% rename from src/services/lichess/router-lichess.js rename to src/routes/lichess.routes.js index 2153d6e..02ce118 100644 --- a/src/services/lichess/router-lichess.js +++ b/src/routes/lichess.routes.js @@ -1,4 +1,4 @@ -import lichessStrategy from "./lichess-strategy.js"; +import lichessStrategy from "./strategies/lichess-strategy.js"; export default class LichessRouter { constructor(app, passport) { diff --git a/src/services/lichess/lichess-strategy.js b/src/routes/strategies/lichess-strategy.js similarity index 100% rename from src/services/lichess/lichess-strategy.js rename to src/routes/strategies/lichess-strategy.js diff --git a/src/services/lichess/lichess.js b/src/services/lichess.service.js similarity index 54% rename from src/services/lichess/lichess.js rename to src/services/lichess.service.js index 4c7a03d..b7e9289 100644 --- a/src/services/lichess/lichess.js +++ b/src/services/lichess.service.js @@ -1,18 +1,18 @@ -import LichessModel from "../../models/lichess.js"; +import User from "../models/user.model.js"; export default class LichessService { constructor(credentials) { this.credentials = credentials; - this.lichessModel = new LichessModel(this.credentials); + this.user = new User(this.credentials); } login(chatId) { - this.lichessModel.login(chatId); + this.user.login(chatId); return `https://chessito.org/auth/lichess?chat_id=${chatId}`; } getLiveGames() { - return this.lichessModel.getLiveGames(); + return this.user.getLiveGames(); } }