瀏覽代碼

refactor: moved account(Schema) logic to own modules

Kristian Vos 4 年之前
父節點
當前提交
38183852be

+ 2 - 0
backend/index.js

@@ -162,6 +162,8 @@ moduleManager.addModule("logger");
 moduleManager.addModule("io");
 moduleManager.addModule("util");
 moduleManager.addModule("mongo");
+moduleManager.addModule("account");
+moduleManager.addModule("accountSchema");
 
 moduleManager.initialize();
 

+ 79 - 0
backend/logic/account.js

@@ -0,0 +1,79 @@
+'use strict';
+
+const async = require("async");
+
+const coreClass = require("../core");
+
+const config = require('config');
+
+module.exports = class extends coreClass {
+	constructor(name, moduleManager) {
+		super(name, moduleManager);
+
+		this.dependsOn = ["mongo"];
+	}
+
+	initialize() {
+		return new Promise(async (resolve, reject) => {
+			this.setStage(1);
+
+			this.mongoModule = this.moduleManager.modules["mongo"];
+			this.utilModule = this.moduleManager.modules["util"];
+
+			this.accountSchema = await this.mongoModule.schema("account");
+			this.accountModel = await this.mongoModule.model("account");
+
+			resolve();
+		})
+	}
+
+	async getAll() {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountModel.find({}, (err, accounts) => {
+				if (err) reject(new Error(err));
+				else resolve(accounts);
+			});
+		});
+	}
+
+	async getById(accountId) {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountModel.findById(accountId, (err, account) => {
+				if (err) reject(new Error(err));
+				else resolve(account);
+			});
+		});
+	}
+
+	async add(account) {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+			
+			this.accountModel.create(account, (err) => {
+				if (err) reject(new Error(err));
+				else {
+					this.utilModule.addAutosuggestAccount(account);
+					resolve();
+				}
+			});
+		});
+	}
+
+	async editById(accountId, account) {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountModel.updateOne({ _id: accountId }, account, (err) => {
+				if (err) reject(new Error(err));
+				else {
+					this.utilModule.addAutosuggestAccount(account);
+					resolve();
+				}
+			});
+		});
+	}
+}

+ 72 - 0
backend/logic/accountSchema.js

@@ -0,0 +1,72 @@
+'use strict';
+
+const async = require("async");
+
+const coreClass = require("../core");
+
+const config = require('config');
+
+module.exports = class extends coreClass {
+	constructor(name, moduleManager) {
+		super(name, moduleManager);
+
+		this.dependsOn = ["mongo"];
+	}
+
+	initialize() {
+		return new Promise(async (resolve, reject) => {
+			this.setStage(1);
+
+			this.mongoModule = this.moduleManager.modules["mongo"];
+
+			this.accountSchemaSchema = await this.mongoModule.schema("accountSchema");
+			this.accountSchemaModel = await this.mongoModule.model("accountSchema");
+
+			resolve();
+		})
+	}
+
+	async getLatest() {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountSchemaModel.find({}, null, { sort: "-version", limit: 1 }, (err, schemas) => {
+				if (err || !schemas || schemas.length !== 1) reject(new Error("Something went wrong."))
+				else resolve(schemas[0]);
+			});
+		});
+	}
+
+	async getAll() {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountSchemaModel.find({}, null, { sort: "-version" }, (err, schemas) => {
+				if (err || !schemas) reject(new Error("Something went wrong."))
+				else resolve(schemas)
+			});
+		});
+	}
+
+	async getById(schemaId) {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountSchemaModel.findById(schemaId, (err, schema) => {
+				if (err || !schema) reject(new Error("Something went wrong."))
+				else resolve(schema)
+			});
+		});
+	}
+
+	async import(name) {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			this.accountSchemaModel.create(this.accountSchemaSchema, (err) => {
+				if (err) reject(new Error("Something went wrong."))
+				else resolve();
+			});
+		});
+	}
+}

+ 33 - 52
backend/logic/io/namespaces/account.js

@@ -1,77 +1,58 @@
 const moduleManager = require("../../../index");
 
+const accountModule = moduleManager.modules["account"];
 const mongoModule = moduleManager.modules["mongo"];
 const utilModule = moduleManager.modules["util"];
 
 module.exports = {
 	"getAll": async cb => {
-		mongoModule.models.then(models => {
-			models.account.find({}, (err, accounts) => {
-				if (err)
-					return cb({
-						status: "failure",
-						err: err
-					});
-				else
-					return cb({
-						status: "success",
-						accounts
-					});
+		accountModule.getAll().then(accounts => {
+			cb({
+				status: "success",
+				accounts
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	},
 
 	"getById": (cb, accountId) => {
-		mongoModule.models.then(models => {
-			models.account.findById(accountId, (err, account) => {
-				if (err || !account)
-					return cb({
-						status: "failure",
-						err: err
-					});
-				else
-					return cb({
-						status: "success",
-						account
-					});
+		accountModule.getById(accountId).then(account => {
+			cb({
+				status: "success",
+				account
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	},
 
 	"add": (cb, account) => {
-		mongoModule.models.then(models => {
-			models.account.create(account, (err) => {
-				if (err)
-					return cb({
-						status: "failure",
-						err: err
-					});
-				else {
-					utilModule.addAutosuggestAccount(account);
-					console.log("Added account!");
-					return cb({
-						status: "success"
-					});
-				}
+		accountModule.add(account).then(() => {
+			console.log("Added account!");
+			cb({
+				status: "success"
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	},
 
 	"editById": (cb, accountId, account) => {
-		mongoModule.models.then(models => {
-			models.account.updateOne({ _id: accountId }, account, (err) => {
-				if (err)
-					return cb({
-						status: "failure",
-						err: err
-					});
-				else {
-					utilModule.addAutosuggestAccount(account);
-					console.log("Editted account!");
-					return cb({
-						status: "success"
-					});
-				}
+		accountModule.editById(accountId, account).then(() => {
+			console.log("Edited account!");
+			cb({
+				status: "success"
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	}

+ 32 - 49
backend/logic/io/namespaces/accountSchema.js

@@ -1,73 +1,56 @@
 const moduleManager = require("../../../index");
 
 const mongoModule = moduleManager.modules["mongo"];
+const accountSchemaModule = moduleManager.modules["accountSchema"];
 
 module.exports = {
 	"getLatest": cb => {
-		mongoModule.models.then(models => {
-			models.accountSchema.find({}, null, { sort: "-version", limit: 1 }, (err, res) => {
-				if (err || !res || res.length !== 1)
-					return cb({
-						status: "failure",
-						message: "Something went wrong."
-					});
-				else
-					return cb({
-						status: "success",
-						schema: res[0]
-					});
+		accountSchemaModule.getLatest().then(schema => {
+			cb({
+				status: "success",
+				schema
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	},
 
 	"getAll": cb => {
-		mongoModule.models.then(models => {
-			models.accountSchema.find({}, null, { sort: "-version" }, (err, res) => {
-				if (err || !res)
-					return cb({
-						status: "failure",
-						message: "Something went wrong."
-					});
-				else
-					return cb({
-						status: "success",
-						schemas: res
-					});
+		accountSchemaModule.getAll().then(schemas => {
+			cb({
+				status: "success",
+				schemas
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	},
 
 	"getById": (cb, schemaId) => {
-		mongoModule.models.then(models => {
-			models.accountSchema.findById(schemaId, (err, res) => {
-				if (err || !res)
-					return cb({
-						status: "failure",
-						message: "Something went wrong."
-					});
-				else
-					return cb({
-						status: "success",
-						schema: res
-					});
+		accountSchemaModule.getById(schemaId).then(schema => {
+			cb({
+				status: "success",
+				schema
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	},
 
 	"import": (cb, name) => {
-		mongoModule.models.then(models => {
-			mongoModule.schemas.then(models => {
-				models.accountSchema.create(schemas[name], (err) => {
-					if (err)
-						return cb({
-							status: "failure",
-							err: err
-						});
-					else
-						return cb({
-							status: "success"
-						});
-				});
+		accountSchemaModule.import(name).then(() => {
+			cb({
+				status: "success"
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
 			});
 		});
 	}

+ 4 - 4
backend/logic/mongo/index.js

@@ -208,17 +208,17 @@ module.exports = class extends coreClass {
 		})
 	}
 
-	get schemas() {
+	async schema(name) {
 		return new Promise(async resolve => {
 			try { await this._validateHook(); } catch { return; }
-			resolve(this._schemas);
+			resolve(this._schemas[name]);
 		});
 	}
 
-	get models() {
+	async model(name) {
 		return new Promise(async resolve => {
 			try { await this._validateHook(); } catch { return; }
-			resolve(this._models);
+			resolve(this._models[name]);
 		});
 	}
 }

+ 7 - 8
backend/logic/util.js

@@ -14,19 +14,20 @@ module.exports = class extends coreClass {
 	}
 
 	initialize() {
-		return new Promise((resolve, reject) => {
+		return new Promise(async (resolve, reject) => {
 			this.setStage(1);
 
-			this.mongo = this.moduleManager.modules["mongo"];
+			this.mongoModule = this.moduleManager.modules["mongo"];
 
 			this._autosuggestCache = {};
 			this._autosuggestMap = {};
 
+			this.accountSchemaModel = await this.mongoModule.model("accountSchema");
+			this.accountModel = await this.mongoModule.model("account");
+
 			async.waterfall([
 				(next) => {
-					this.mongo.models.then(models => {
-						models.accountSchema.find({}, null, { sort: "-version", limit: 1 }, next);
-					});
+					this.accountSchemaModel.find({}, null, { sort: "-version", limit: 1 }, next);
 				},
 
 				(schemas, next) => {
@@ -39,9 +40,7 @@ module.exports = class extends coreClass {
 						});
 					});
 
-					this.mongo.models.then(models => {
-						models.account.find({}, next);
-					});
+					this.accountModel.find({}, next);
 				},
 
 				(accounts, next) => {