From 287b1d3f838b9316f59e69a9f554340bf113acdb Mon Sep 17 00:00:00 2001 From: Stefan Werfling Date: Mon, 31 Jul 2023 12:34:03 +0200 Subject: [PATCH] update db service object, add db user service object --- backend/src/Routes/Main/Domain/Delete.ts | 10 +-- backend/src/Routes/Main/Domain/List.ts | 4 +- backend/src/Routes/Main/Domain/Save.ts | 10 +-- backend/src/Routes/Main/DynDnsClient/List.ts | 4 +- backend/src/Routes/Main/DynDnsServer/List.ts | 7 +- backend/src/Routes/Main/Login/Login.ts | 11 +-- backend/src/Routes/Main/Route/List.ts | 4 +- backend/src/Routes/Main/Ssl/Details.ts | 4 +- backend/src/Routes/Main/User.ts | 2 +- backend/src/Routes/Main/User/Delete.ts | 13 +-- backend/src/Routes/Main/User/Info.ts | 42 ++++------ backend/src/Routes/Main/User/List.ts | 7 +- backend/src/Routes/Main/User/Save.ts | 32 +++----- backend/src/inc/Db/MariaDb/DBSetup.ts | 11 ++- backend/src/inc/Dns/Dns2Server.ts | 4 +- backend/src/inc/Service/NginxService.ts | 6 +- backend/src/inc/Service/SslCertService.ts | 4 +- backend/src/main.ts | 3 +- core/src/inc/Db/MariaDb/DBService.ts | 11 ++- core/src/inc/Db/MariaDb/DomainService.ts | 79 +++++-------------- .../Db/MariaDb/DynDnsServerDomainService.ts | 7 +- .../inc/Db/MariaDb/DynDnsServerUserService.ts | 47 +++-------- .../src/inc/Db/MariaDb/Entity/User.ts | 0 core/src/inc/Db/MariaDb/UserService.ts | 76 ++++++++++++++++++ core/src/index.ts | 11 ++- ddnsserver/src/Routes/Main/Update.ts | 6 +- 26 files changed, 207 insertions(+), 208 deletions(-) rename {backend => core}/src/inc/Db/MariaDb/Entity/User.ts (100%) create mode 100644 core/src/inc/Db/MariaDb/UserService.ts diff --git a/backend/src/Routes/Main/Domain/Delete.ts b/backend/src/Routes/Main/Domain/Delete.ts index b74ff74..d9578a7 100644 --- a/backend/src/Routes/Main/Domain/Delete.ts +++ b/backend/src/Routes/Main/Domain/Delete.ts @@ -1,4 +1,4 @@ -import {DBHelper, DomainRecordDB, DomainService} from 'flyingfish_core'; +import {DBHelper, DomainRecordDB, DomainServiceDB} from 'flyingfish_core'; import {DomainDelete, DomainDeleteResponse, StatusCodes} from 'flyingfish_schemas'; import {NginxHttp as NginxHttpDB} from '../../../inc/Db/MariaDb/Entity/NginxHttp.js'; import {NginxStream as NginxStreamDB} from '../../../inc/Db/MariaDb/Entity/NginxStream.js'; @@ -17,7 +17,7 @@ export class Delete { const streamRepository = DBHelper.getRepository(NginxStreamDB); const httpRepository = DBHelper.getRepository(NginxHttpDB); - const domain = await DomainService.findOne(data.id); + const domain = await DomainServiceDB.getInstance().findOne(data.id); if (domain) { if (domain.fixdomain) { @@ -50,14 +50,14 @@ export class Delete { domain_id: data.id }); - const result = await DomainService.remove(domain.id); + const result = await DomainServiceDB.getInstance().remove(domain.id); if (result) { if (domain.parent_id !== 0) { - const parent = await DomainService.findOne(domain.parent_id); + const parent = await DomainServiceDB.getInstance().findOne(domain.parent_id); if (parent) { - await DomainService.updateChildrenToNewParent(parent); + await DomainServiceDB.getInstance().updateChildrenToNewParent(parent); } } diff --git a/backend/src/Routes/Main/Domain/List.ts b/backend/src/Routes/Main/Domain/List.ts index d1c30ac..598a7f8 100644 --- a/backend/src/Routes/Main/Domain/List.ts +++ b/backend/src/Routes/Main/Domain/List.ts @@ -1,4 +1,4 @@ -import {DBHelper, DomainRecordDB, DomainService} from 'flyingfish_core'; +import {DBHelper, DomainRecordDB, DomainServiceDB} from 'flyingfish_core'; import {DomainData, DomainRecord, DomainResponse, StatusCodes} from 'flyingfish_schemas'; /** @@ -13,7 +13,7 @@ export class List { const domainRecordRepository = DBHelper.getRepository(DomainRecordDB); const domainList: DomainData[] = []; - const domains = await DomainService.findAll(); + const domains = await DomainServiceDB.getInstance().findAll(); for await (const domain of domains) { const recordList: DomainRecord[] = []; diff --git a/backend/src/Routes/Main/Domain/Save.ts b/backend/src/Routes/Main/Domain/Save.ts index 5d0b071..2f66fea 100644 --- a/backend/src/Routes/Main/Domain/Save.ts +++ b/backend/src/Routes/Main/Domain/Save.ts @@ -1,4 +1,4 @@ -import {DomainDB, DomainService} from 'flyingfish_core'; +import {DomainDB, DomainServiceDB} from 'flyingfish_core'; import {DomainData, DomainSaveResponse, StatusCodes} from 'flyingfish_schemas'; /** @@ -14,7 +14,7 @@ export class Save { let aDomain: DomainDB | null = null; if (data.id !== 0) { - const tDomain = await DomainService.findOne(data.id); + const tDomain = await DomainServiceDB.getInstance().findOne(data.id); if (tDomain) { if (tDomain.fixdomain) { @@ -39,12 +39,12 @@ export class Save { aDomain.domainname = data.name; aDomain.disable = data.disable; - aDomain.parent_id = await DomainService.findParentId(data.name); + aDomain.parent_id = await DomainServiceDB.getInstance().findParentId(data.name); - aDomain = await DomainService.save(aDomain); + aDomain = await DomainServiceDB.getInstance().save(aDomain); if (aDomain.parent_id !== 0) { - await DomainService.updateChildrenToNewParent(aDomain); + await DomainServiceDB.getInstance().updateChildrenToNewParent(aDomain); } return { diff --git a/backend/src/Routes/Main/DynDnsClient/List.ts b/backend/src/Routes/Main/DynDnsClient/List.ts index 578aefc..b443cdf 100644 --- a/backend/src/Routes/Main/DynDnsClient/List.ts +++ b/backend/src/Routes/Main/DynDnsClient/List.ts @@ -1,4 +1,4 @@ -import {DBHelper, DomainService} from 'flyingfish_core'; +import {DBHelper, DomainServiceDB} from 'flyingfish_core'; import { DynDnsClientData, DynDnsClientDomain, @@ -45,7 +45,7 @@ export class List { if (domainList) { for await (const domain of domainList) { - const tdomain = await DomainService.findOne(domain.domain_id); + const tdomain = await DomainServiceDB.getInstance().findOne(domain.domain_id); if (tdomain) { domains.push({ diff --git a/backend/src/Routes/Main/DynDnsServer/List.ts b/backend/src/Routes/Main/DynDnsServer/List.ts index dd631b5..84e1433 100644 --- a/backend/src/Routes/Main/DynDnsServer/List.ts +++ b/backend/src/Routes/Main/DynDnsServer/List.ts @@ -1,4 +1,4 @@ -import {DynDnsServerUserService} from 'flyingfish_core'; +import {DynDnsServerUserServiceDB} from 'flyingfish_core'; import {DynDnsServerData, DynDnsServerListResponse, StatusCodes} from 'flyingfish_schemas'; /** @@ -6,8 +6,11 @@ import {DynDnsServerData, DynDnsServerListResponse, StatusCodes} from 'flyingfis */ export class List { + /** + * getList + */ public static async getList(): Promise { - const users = await DynDnsServerUserService.findAll(); + const users = await DynDnsServerUserServiceDB.getInstance().findAll(); const list: DynDnsServerData[] = []; diff --git a/backend/src/Routes/Main/Login/Login.ts b/backend/src/Routes/Main/Login/Login.ts index 6cb7ceb..7bbce31 100644 --- a/backend/src/Routes/Main/Login/Login.ts +++ b/backend/src/Routes/Main/Login/Login.ts @@ -1,6 +1,5 @@ -import {DBHelper, Logger} from 'flyingfish_core'; +import {Logger, UserServiceDB} from 'flyingfish_core'; import {DefaultReturn, LoginRequest, SessionData, StatusCodes} from 'flyingfish_schemas'; -import {User as UserDB} from '../../../inc/Db/MariaDb/Entity/User.js'; import * as bcrypt from 'bcrypt'; /** @@ -14,13 +13,7 @@ export class Login { * @param login */ public static async login(session: SessionData, login: LoginRequest): Promise { - const userRepository = DBHelper.getDataSource().getRepository(UserDB); - - const user = await userRepository.findOne({ - where: { - email: login.email - } - }); + const user = await UserServiceDB.getInstance().findOneByEmail(login.email); session.user = { isLogin: false, diff --git a/backend/src/Routes/Main/Route/List.ts b/backend/src/Routes/Main/Route/List.ts index d76b822..a957814 100644 --- a/backend/src/Routes/Main/Route/List.ts +++ b/backend/src/Routes/Main/Route/List.ts @@ -1,4 +1,4 @@ -import {DBHelper, DomainService, SshPortDB, SshUserDB} from 'flyingfish_core'; +import {DBHelper, DomainServiceDB, SshPortDB, SshUserDB} from 'flyingfish_core'; import { Location, RouteData, @@ -38,7 +38,7 @@ export class List { const locationRepository = DBHelper.getRepository(NginxLocationDB); const sshportRepository = DBHelper.getRepository(SshPortDB); const sshuserRepository = DBHelper.getRepository(SshUserDB); - const domains = await DomainService.findAll(); + const domains = await DomainServiceDB.getInstance().findAll(); if (domains) { for await (const adomain of domains) { diff --git a/backend/src/Routes/Main/Ssl/Details.ts b/backend/src/Routes/Main/Ssl/Details.ts index eb0238e..57e3034 100644 --- a/backend/src/Routes/Main/Ssl/Details.ts +++ b/backend/src/Routes/Main/Ssl/Details.ts @@ -1,4 +1,4 @@ -import {DBHelper, DomainService} from 'flyingfish_core'; +import {DBHelper, DomainServiceDB} from 'flyingfish_core'; import {SslDetailInfoData, SslDetailsRequest, SslDetailsResponse, StatusCodes} from 'flyingfish_schemas'; import Path from 'path'; import {Certificate} from '../../../inc/Cert/Certificate.js'; @@ -31,7 +31,7 @@ export class Details { }; } - const domain = await DomainService.findOne(http.domain_id); + const domain = await DomainServiceDB.getInstance().findOne(http.domain_id); if (domain) { const sslCert = await Certbot.existCertificate(domain.domainname); diff --git a/backend/src/Routes/Main/User.ts b/backend/src/Routes/Main/User.ts index 96dcb43..a278db9 100644 --- a/backend/src/Routes/Main/User.ts +++ b/backend/src/Routes/Main/User.ts @@ -19,7 +19,7 @@ export class User extends DefaultRoute { '/json/user/info', async(req, res) => { if (this.isUserLogin(req, res)) { - res.status(200).json(await Info.getUserInfo(req)); + res.status(200).json(await Info.getUserInfo(req.session)); } } ); diff --git a/backend/src/Routes/Main/User/Delete.ts b/backend/src/Routes/Main/User/Delete.ts index 03b3439..b819118 100644 --- a/backend/src/Routes/Main/User/Delete.ts +++ b/backend/src/Routes/Main/User/Delete.ts @@ -1,6 +1,5 @@ -import {DBHelper} from 'flyingfish_core'; +import {UserServiceDB} from 'flyingfish_core'; import {DefaultReturn, StatusCodes, UserDeleteRequest} from 'flyingfish_schemas'; -import {User as UserDB} from '../../../inc/Db/MariaDb/Entity/User.js'; /** * Delete @@ -12,13 +11,9 @@ export class Delete { * @param data */ public static async deleteUser(data: UserDeleteRequest): Promise { - const userRepository = DBHelper.getDataSource().getRepository(UserDB); - // check is the last user ---------------------------------------------------------------------------------- - const cUsers = await userRepository.countBy({ - disable: false - }); + const cUsers = await UserServiceDB.getInstance().countDisable(false); if (cUsers < 2) { return { @@ -29,9 +24,7 @@ export class Delete { // delete -------------------------------------------------------------------------------------------------- - const result = await userRepository.delete({ - id: data.id - }); + const result = await UserServiceDB.getInstance().remove(data.id); if (result) { return { diff --git a/backend/src/Routes/Main/User/Info.ts b/backend/src/Routes/Main/User/Info.ts index 2a0d099..90595ed 100644 --- a/backend/src/Routes/Main/User/Info.ts +++ b/backend/src/Routes/Main/User/Info.ts @@ -1,7 +1,5 @@ -import {DBHelper} from 'flyingfish_core'; -import {StatusCodes, UserInfoResponse} from 'flyingfish_schemas'; -import {User as UserDB} from '../../../inc/Db/MariaDb/Entity/User.js'; -import {Request} from 'express'; +import {UserServiceDB} from 'flyingfish_core'; +import {SessionData, StatusCodes, UserInfoResponse} from 'flyingfish_schemas'; /** * Info @@ -11,29 +9,23 @@ export class Info { /** * getUserInfo */ - public static async getUserInfo(req: Request): Promise { - const userRepository = DBHelper.getDataSource().getRepository(UserDB); - // @ts-ignore - const userid = req.session.user.userid; + public static async getUserInfo(session: SessionData): Promise { + if (session.user) { + const user = await UserServiceDB.getInstance().findOne(session.user.userid); - const user = await userRepository.findOne({ - where: { - id: userid - } - }); - - if (user) { - return { - statusCode: StatusCodes.OK, - data: { - islogin: true, - user: { - id: user.id, - username: user.username, - email: user.email + if (user) { + return { + statusCode: StatusCodes.OK, + data: { + islogin: true, + user: { + id: user.id, + username: user.username, + email: user.email + } } - } - }; + }; + } } return { diff --git a/backend/src/Routes/Main/User/List.ts b/backend/src/Routes/Main/User/List.ts index 3241713..b3e3607 100644 --- a/backend/src/Routes/Main/User/List.ts +++ b/backend/src/Routes/Main/User/List.ts @@ -1,6 +1,5 @@ -import {DBHelper} from 'flyingfish_core'; +import {UserServiceDB} from 'flyingfish_core'; import {StatusCodes, UserEntry, UserListResponse} from 'flyingfish_schemas'; -import {User as UserDB} from '../../../inc/Db/MariaDb/Entity/User.js'; /** * List @@ -11,9 +10,7 @@ export class List { * getUserList */ public static async getUserList(): Promise { - const userRepository = DBHelper.getDataSource().getRepository(UserDB); - - const entries = await userRepository.find(); + const entries = await UserServiceDB.getInstance().findAll(); const userList: UserEntry[] = []; diff --git a/backend/src/Routes/Main/User/Save.ts b/backend/src/Routes/Main/User/Save.ts index c134c91..f86184a 100644 --- a/backend/src/Routes/Main/User/Save.ts +++ b/backend/src/Routes/Main/User/Save.ts @@ -1,8 +1,6 @@ -import {DBHelper} from 'flyingfish_core'; +import {UserDB, UserServiceDB} from 'flyingfish_core'; import {DefaultReturn, StatusCodes, UserEntry} from 'flyingfish_schemas'; -import {Not} from 'typeorm'; import * as bcrypt from 'bcrypt'; -import {User as UserDB} from '../../../inc/Db/MariaDb/Entity/User.js'; /** * Save @@ -14,14 +12,12 @@ export class Save { * @param data */ public static async saveUser(data: UserEntry): Promise { - const userRepository = DBHelper.getDataSource().getRepository(UserDB); + const us = UserServiceDB.getInstance(); // check is the last user ---------------------------------------------------------------------------------- if (data.disable) { - const cUsers = await userRepository.countBy({ - disable: false - }); + const cUsers = await us.countDisable(false); if (cUsers < 2) { return { @@ -33,12 +29,9 @@ export class Save { // check username ------------------------------------------------------------------------------------------ - const cUsername = await userRepository.countBy({ - username: data.username, - id: Not(data.id) - }); + const existUsername = await us.existUsername(data.username, data.id); - if (cUsername > 0) { + if (existUsername) { return { statusCode: StatusCodes.INTERNAL_ERROR, msg: 'Username already in use!' @@ -47,12 +40,9 @@ export class Save { // check email --------------------------------------------------------------------------------------------- - const cEmail = await userRepository.countBy({ - email: data.email, - id: Not(data.id) - }); + const existEmail = await us.existEmail(data.email, data.id); - if (cEmail > 0) { + if (existEmail) { return { statusCode: StatusCodes.INTERNAL_ERROR, msg: 'EMail already in use!' @@ -64,11 +54,7 @@ export class Save { let aUser: UserDB|null = null; if (data.id !== 0) { - aUser = await userRepository.findOne({ - where: { - id: data.id - } - }); + aUser = await us.findOne(data.id); } if (aUser === null) { @@ -97,7 +83,7 @@ export class Save { aUser.email = data.email; aUser.disable = data.disable; - await DBHelper.getDataSource().manager.save(aUser); + await us.save(aUser); return { statusCode: StatusCodes.OK diff --git a/backend/src/inc/Db/MariaDb/DBSetup.ts b/backend/src/inc/Db/MariaDb/DBSetup.ts index 539f896..d54eec1 100644 --- a/backend/src/inc/Db/MariaDb/DBSetup.ts +++ b/backend/src/inc/Db/MariaDb/DBSetup.ts @@ -1,9 +1,8 @@ import * as bcrypt from 'bcrypt'; -import {DBHelper, DomainDB, DomainService, Logger} from 'flyingfish_core'; +import {DBHelper, DomainDB, DomainServiceDB, Logger, UserDB, UserServiceDB} from 'flyingfish_core'; import {ListenCategory, ListenProtocol, ListenTypes, NginxListen as NginxListenDB} from './Entity/NginxListen.js'; import {NginxStream as NginxStreamDB, NginxStreamDestinationType} from './Entity/NginxStream.js'; import {NginxUpstream as NginxUpstreamDB} from './Entity/NginxUpstream.js'; -import {User as UserDB} from './Entity/User.js'; /** * DBSetup @@ -16,8 +15,8 @@ export class DBSetup { * firstInit */ public static async firstInit(): Promise { - const userRepository = DBHelper.getRepository(UserDB); - const userCount = await userRepository.count(); + const us = UserServiceDB.getInstance(); + const userCount = await us.countAll(); if (userCount === 0) { const nUser = new UserDB(); @@ -27,7 +26,7 @@ export class DBSetup { nUser.disable = false; // save user to db - await DBHelper.getDataSource().manager.save(nUser); + await us.save(nUser); Logger.getLogger().info('Admin user create for first init.'); } @@ -119,7 +118,7 @@ export class DBSetup { defaultDomain.fixdomain = true; defaultDomain.recordless = true; - defaultDomain = await DomainService.save(defaultDomain); + defaultDomain = await DomainServiceDB.getInstance().save(defaultDomain); // add streams _ -------------------------------------------------------------------------------------------- diff --git a/backend/src/inc/Dns/Dns2Server.ts b/backend/src/inc/Dns/Dns2Server.ts index af352d6..7686e3a 100644 --- a/backend/src/inc/Dns/Dns2Server.ts +++ b/backend/src/inc/Dns/Dns2Server.ts @@ -1,6 +1,6 @@ import {RemoteInfo} from 'dgram'; import DNS, {DnsQuestion, DnsRequest, DnsResponse} from 'dns2'; -import {DBHelper, DomainRecordDB, DomainService, Logger} from 'flyingfish_core'; +import {DBHelper, DomainRecordDB, DomainServiceDB, Logger} from 'flyingfish_core'; import {SchemaErrors} from 'vts'; import {Config} from '../Config/Config.js'; import {DnsAnswerMX} from './RecordType/MX.js'; @@ -55,7 +55,7 @@ export class Dns2Server { Logger.getLogger().info(`Dns2Server::request: ${request.header.id}`, request.questions[0]); Logger.getLogger().info(`Dns2Server::request: Remote-Info ${rinfo.address}:${rinfo.port}`); - const domain = await DomainService.findByName(questionExt.name); + const domain = await DomainServiceDB.getInstance().findByName(questionExt.name); if (domain) { const domainRecordRepository = DBHelper.getRepository(DomainRecordDB); diff --git a/backend/src/inc/Service/NginxService.ts b/backend/src/inc/Service/NginxService.ts index 7033059..2058329 100644 --- a/backend/src/inc/Service/NginxService.ts +++ b/backend/src/inc/Service/NginxService.ts @@ -1,4 +1,4 @@ -import {DBHelper, DomainService, FileHelper, Logger, SshPortDB} from 'flyingfish_core'; +import {DBHelper, DomainServiceDB, FileHelper, Logger, SshPortDB} from 'flyingfish_core'; import fs from 'fs/promises'; import * as Path from 'path'; import {SchemaErrors} from 'vts'; @@ -324,7 +324,7 @@ export class NginxService { }); for await (const astream of tstreams) { - const adomain = await DomainService.findOne(astream.domain_id); + const adomain = await DomainServiceDB.getInstance().findOne(astream.domain_id); if (adomain) { if (!streamMap.has(alisten.listen_port)) { @@ -389,7 +389,7 @@ export class NginxService { }); for await (const http of https) { - const adomain = await DomainService.findOne(http.domain_id); + const adomain = await DomainServiceDB.getInstance().findOne(http.domain_id); if (adomain) { if (!httpMap.has(alisten.listen_port)) { diff --git a/backend/src/inc/Service/SslCertService.ts b/backend/src/inc/Service/SslCertService.ts index 7b8b3ee..a744288 100644 --- a/backend/src/inc/Service/SslCertService.ts +++ b/backend/src/inc/Service/SslCertService.ts @@ -1,5 +1,5 @@ import {Ets} from 'ets'; -import {DateHelper, DBHelper, DomainService, FileHelper, Logger} from 'flyingfish_core'; +import {DateHelper, DBHelper, DomainServiceDB, FileHelper, Logger} from 'flyingfish_core'; import {DomainCheckReachability, SchemaDomainCheckReachability} from 'flyingfish_schemas'; import fs from 'fs/promises'; import got from 'got'; @@ -131,7 +131,7 @@ export class SslCertService { if (http.ssl_enable) { Logger.getLogger().silly(`SslCertService::update: ssl enable http: ${http.id}`); - const domain = await DomainService.findOne(http.domain_id); + const domain = await DomainServiceDB.getInstance().findOne(http.domain_id); if (domain) { if (domain.disable) { diff --git a/backend/src/main.ts b/backend/src/main.ts index 1bce54e..4c0bedc 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -1,6 +1,6 @@ import * as path from 'path'; import * as fs from 'fs'; -import {Args, DBHelper, DomainDB, DomainRecordDB, Logger, SshPortDB, SshUserDB} from 'flyingfish_core'; +import {Args, DBHelper, DomainDB, DomainRecordDB, Logger, SshPortDB, SshUserDB, UserDB} from 'flyingfish_core'; import {InfluxDbHelper} from './inc/Db/InfluxDb/InfluxDbHelper.js'; import {GatewayIdentifier as GatewayIdentifierDB} from './inc/Db/MariaDb/Entity/GatewayIdentifier.js'; import {IpBlacklistCategory as IpBlacklistCategoryDB} from './inc/Db/MariaDb/Entity/IpBlacklistCategory.js'; @@ -50,7 +50,6 @@ import {NginxHttp as NginxHttpDB} from './inc/Db/MariaDb/Entity/NginxHttp.js'; import {NginxListen as NginxListenDB} from './inc/Db/MariaDb/Entity/NginxListen.js'; import {NginxLocation as NginxLocationDB} from './inc/Db/MariaDb/Entity/NginxLocation.js'; import {NginxStream as NginxStreamDB} from './inc/Db/MariaDb/Entity/NginxStream.js'; -import {User as UserDB} from './inc/Db/MariaDb/Entity/User.js'; import {NginxServer} from './inc/Nginx/NginxServer.js'; import {HttpServer} from './inc/Server/HttpServer.js'; import {DynDnsService} from './inc/Service/DynDnsService.js'; diff --git a/core/src/inc/Db/MariaDb/DBService.ts b/core/src/inc/Db/MariaDb/DBService.ts index 4917b8a..9d89300 100644 --- a/core/src/inc/Db/MariaDb/DBService.ts +++ b/core/src/inc/Db/MariaDb/DBService.ts @@ -17,7 +17,7 @@ export abstract class DBService { * repository for T * @private */ - private readonly _repository: Repository; + protected readonly _repository: Repository; /** * getSingleInstance @@ -52,10 +52,17 @@ export abstract class DBService { this._repository = DBHelper.getRepository(target); } + /** + * countAll + */ + public async countAll(): Promise { + return this._repository.count(); + } + /** * findAll */ - public findAll(): Promise { + public async findAll(): Promise { return this._repository.find(); } diff --git a/core/src/inc/Db/MariaDb/DomainService.ts b/core/src/inc/Db/MariaDb/DomainService.ts index 2f931d1..6683ad8 100644 --- a/core/src/inc/Db/MariaDb/DomainService.ts +++ b/core/src/inc/Db/MariaDb/DomainService.ts @@ -1,46 +1,25 @@ -import {Repository, DeleteResult} from 'typeorm'; -import {DBHelper} from './DBHelper.js'; +import {DBService} from './DBService.js'; import {Domain} from './Entity/Domain.js'; /** * DomainService */ -export class DomainService { +export class DomainService extends DBService { /** - * repository for domain - * @private + * register name */ - private static _repository: Repository|null = null; + public static REGISTER_NAME = 'domain'; /** - * getRepository + * getInstance */ - public static getRepository(): Repository { - if (DomainService._repository === null) { - DomainService._repository = DBHelper.getRepository(Domain); - } - - return DomainService._repository; - } - - /** - * findAll - */ - public static findAll(): Promise { - return DomainService.getRepository().find(); - } - - /** - * findOne - * @param id - */ - public static findOne(id: number): Promise { - return DomainService.getRepository().findOne({ - where: { - id: id - } - }); + public static getInstance(): DomainService { + return DBService.getSingleInstance( + DomainService, + Domain, + DomainService.REGISTER_NAME + ); } /** @@ -48,8 +27,8 @@ export class DomainService { * @param name * @param disable */ - public static findByName(name: string, disable: boolean = false): Promise { - return DomainService.getRepository().findOne({ + public findByName(name: string, disable: boolean = false): Promise { + return this._repository.findOne({ where: { domainname: name, disable: disable @@ -57,27 +36,11 @@ export class DomainService { }); } - /** - * remove - * @param id - */ - public static async remove(id: number): Promise { - return DomainService.getRepository().delete(id); - } - - /** - * save - * @param domain - */ - public static async save(domain: Domain): Promise { - return DomainService.getRepository().save(domain); - } - /** * findParentId * @param domainname */ - public static async findParentId(domainname: string): Promise { + public async findParentId(domainname: string): Promise { const parts = domainname.split('.'); if (parts.length <= 1) { @@ -87,21 +50,21 @@ export class DomainService { parts.shift(); const parentDomainname = parts.join('.'); - const domain = await DomainService.findByName(parentDomainname); + const domain = await this.findByName(parentDomainname); if (domain) { return domain.id; } - return DomainService.findParentId(parentDomainname); + return this.findParentId(parentDomainname); } /** * getChildrenById * @param id */ - public static async getChildrenById(id: number): Promise { - return DomainService.getRepository().find({ + public async getChildrenById(id: number): Promise { + return this._repository.find({ where: { parent_id: id } @@ -112,13 +75,13 @@ export class DomainService { * updateChildrenToNewParent * @param domain */ - public static async updateChildrenToNewParent(domain: Domain): Promise { + public async updateChildrenToNewParent(domain: Domain): Promise { if (domain.parent_id === 0) { return; } const domainNameParts = domain.domainname.split('.').reverse(); - const childrens = await DomainService.getChildrenById(domain.parent_id); + const childrens = await this.getChildrenById(domain.parent_id); for await (const aChildren of childrens) { if (aChildren.id === domain.id) { @@ -139,7 +102,7 @@ export class DomainService { if (isSubDomain) { aChildren.parent_id = domain.id; - await DomainService.save(aChildren); + await this.save(aChildren); } } } diff --git a/core/src/inc/Db/MariaDb/DynDnsServerDomainService.ts b/core/src/inc/Db/MariaDb/DynDnsServerDomainService.ts index 19bfc1f..1a7bd12 100644 --- a/core/src/inc/Db/MariaDb/DynDnsServerDomainService.ts +++ b/core/src/inc/Db/MariaDb/DynDnsServerDomainService.ts @@ -6,6 +6,11 @@ import {DynDnsServerDomain} from './Entity/DynDnsServerDomain.js'; */ export class DynDnsServerDomainService extends DBService { + /** + * register name + */ + public static REGISTER_NAME = 'dyndns_server_domain'; + /** * getInstance */ @@ -13,7 +18,7 @@ export class DynDnsServerDomainService extends DBService { return DBService.getSingleInstance( DynDnsServerDomainService, DynDnsServerDomain, - 'dyndns_server_domain' + DynDnsServerDomainService.REGISTER_NAME ); } diff --git a/core/src/inc/Db/MariaDb/DynDnsServerUserService.ts b/core/src/inc/Db/MariaDb/DynDnsServerUserService.ts index 770840f..b0dc3c7 100644 --- a/core/src/inc/Db/MariaDb/DynDnsServerUserService.ts +++ b/core/src/inc/Db/MariaDb/DynDnsServerUserService.ts @@ -1,54 +1,33 @@ -import {Repository} from 'typeorm'; -import {DBHelper} from './DBHelper.js'; +import {DBService} from './DBService.js'; import {DynDnsServerUser} from './Entity/DynDnsServerUser.js'; /** * DynDnsServerUserService */ -export class DynDnsServerUserService { +export class DynDnsServerUserService extends DBService { /** - * repository for dyndns server user - * @private + * register name */ - private static _repository: Repository|null = null; + public static REGISTER_NAME = 'dyndns_server_user'; /** - * getRepository + * getInstance */ - public static getRepository(): Repository { - if (DynDnsServerUserService._repository === null) { - DynDnsServerUserService._repository = DBHelper.getRepository(DynDnsServerUser); - } - - return DynDnsServerUserService._repository; - } - - /** - * findAll - */ - public static findAll(): Promise { - return DynDnsServerUserService.getRepository().find(); - } - - /** - * findOne - * @param id - */ - public static findOne(id: number): Promise { - return DynDnsServerUserService.getRepository().findOne({ - where: { - id: id - } - }); + public static getInstance(): DynDnsServerUserService { + return DBService.getSingleInstance( + DynDnsServerUserService, + DynDnsServerUser, + DynDnsServerUserService.REGISTER_NAME + ); } /** * findByName * @param name */ - public static findByName(name: string): Promise { - return DynDnsServerUserService.getRepository().findOne({ + public findByName(name: string): Promise { + return this._repository.findOne({ where: { username: name } diff --git a/backend/src/inc/Db/MariaDb/Entity/User.ts b/core/src/inc/Db/MariaDb/Entity/User.ts similarity index 100% rename from backend/src/inc/Db/MariaDb/Entity/User.ts rename to core/src/inc/Db/MariaDb/Entity/User.ts diff --git a/core/src/inc/Db/MariaDb/UserService.ts b/core/src/inc/Db/MariaDb/UserService.ts new file mode 100644 index 0000000..02de6be --- /dev/null +++ b/core/src/inc/Db/MariaDb/UserService.ts @@ -0,0 +1,76 @@ +import {Not} from 'typeorm'; +import {DBService} from './DBService.js'; +import {User} from './Entity/User.js'; + +/** + * UserService + */ +export class UserService extends DBService { + + /** + * register name + */ + public static REGISTER_NAME = 'user'; + + /** + * getInstance + */ + public static getInstance(): UserService { + return DBService.getSingleInstance( + UserService, + User, + UserService.REGISTER_NAME + ); + } + + /** + * findOneByEmail + * @param email + */ + public async findOneByEmail(email: string): Promise { + return this._repository.findOne({ + where: { + email: email + } + }); + } + + /** + * countDisable + * @param disable + */ + public async countDisable(disable: boolean): Promise { + return this._repository.countBy({ + disable: disable + }); + } + + /** + * existUsername + * @param name + * @param ownId + */ + public async existUsername(name: string, ownId: number): Promise { + const count = await this._repository.countBy({ + username: name, + id: Not(ownId) + }); + + return count > 0; + } + + /** + * existEmail + * @param email + * @param ownId + */ + public async existEmail(email: string, ownId: number): Promise { + const count = await this._repository.countBy({ + email: email, + id: Not(ownId) + }); + + return count > 0; + } + +} \ No newline at end of file diff --git a/core/src/index.ts b/core/src/index.ts index 40ead63..a3b76a0 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -10,15 +10,22 @@ export { } from './inc/Crypto/CertificateHelper.js'; export {DBHelper} from './inc/Db/MariaDb/DBHelper.js'; +export {DBBaseEntityId} from './inc/Db/MariaDb/DBBaseEntityId.js'; +export {DBBaseEntityUnid} from './inc/Db/MariaDb/DBBaseEntityUnid.js'; +export {DBService} from './inc/Db/MariaDb/DBService.js'; + export {Domain as DomainDB} from './inc/Db/MariaDb/Entity/Domain.js'; export {DomainRecord as DomainRecordDB} from './inc/Db/MariaDb/Entity/DomainRecord.js'; export {DynDnsServerDomain as DynDnsServerDomainDB} from './inc/Db/MariaDb/Entity/DynDnsServerDomain.js'; export {DynDnsServerUser as DynDnsServerUserDB} from './inc/Db/MariaDb/Entity/DynDnsServerUser.js'; export {SshUser as SshUserDB} from './inc/Db/MariaDb/Entity/SshUser.js'; export {SshPort as SshPortDB} from './inc/Db/MariaDb/Entity/SshPort.js'; +export {User as UserDB} from './inc/Db/MariaDb/Entity/User.js'; -export {DomainService} from './inc/Db/MariaDb/DomainService.js'; -export {DynDnsServerUserService} from './inc/Db/MariaDb/DynDnsServerUserService.js'; +export {DomainService as DomainServiceDB} from './inc/Db/MariaDb/DomainService.js'; +export {DynDnsServerUserService as DynDnsServerUserServiceDB} from './inc/Db/MariaDb/DynDnsServerUserService.js'; +export {DynDnsServerDomainService as DynDnsServerDomainServiceDB} from './inc/Db/MariaDb/DynDnsServerDomainService.js'; +export {UserService as UserServiceDB} from './inc/Db/MariaDb/UserService.js'; export {Session} from './inc/Server/Session.js'; export {DefaultRoute, DefaultRouteHandlerGet, DefaultRouteHandlerPost} from './inc/Server/Routes/DefaultRoute.js'; diff --git a/ddnsserver/src/Routes/Main/Update.ts b/ddnsserver/src/Routes/Main/Update.ts index 38bbfdd..2d1de95 100644 --- a/ddnsserver/src/Routes/Main/Update.ts +++ b/ddnsserver/src/Routes/Main/Update.ts @@ -1,5 +1,5 @@ import {Request, Response, Router} from 'express'; -import {DefaultRoute, DynDnsServerUserService, Logger} from 'flyingfish_core'; +import {DefaultRoute, DynDnsServerUserServiceDB, Logger} from 'flyingfish_core'; import {SchemaRequestData, SessionData} from 'flyingfish_schemas'; import auth from 'basic-auth'; @@ -19,7 +19,7 @@ export class Update extends DefaultRoute { // @ts-ignore if (req.auth) { // @ts-ignore - const ddnsUser = await DynDnsServerUserService.findByName(req.auth.user); + const ddnsUser = await DynDnsServerUserServiceDB.findByName(req.auth.user); if (ddnsUser) { if (session.user) { @@ -57,7 +57,7 @@ export class Update extends DefaultRoute { if (credentials) { Logger.getLogger().silly(`Update::nic-update: basic auth - name: ${credentials.name}`); - const ddnsUser = await DynDnsServerUserService.findByName(credentials.name); + const ddnsUser = await DynDnsServerUserServiceDB.getInstance().findByName(credentials.name); if (ddnsUser) { Logger.getLogger().silly(`Update::nic-update: basic auth - user found: ${ddnsUser.id}`);