diff --git a/src/services/databases/databases.service.ts b/src/services/databases/databases.service.ts index 357e839..5f62999 100644 --- a/src/services/databases/databases.service.ts +++ b/src/services/databases/databases.service.ts @@ -1,5 +1,5 @@ import {MariadbService} from "@services/databases/mariadb.service"; -import {MongodbService} from "@services/databases/mongodb.service"; +//import {MongodbService} from "@services/databases/mongodb.service"; import {LogsUtils} from "@utils/logs.util"; import {UserInDatabase} from "@interfaces/db/mariadb.interface"; @@ -16,12 +16,12 @@ interface MariaDbStatusResult { export class DatabasesService { private readonly _appName; private readonly maria: MariadbService; - private readonly mongo: MongodbService; + //private readonly mongo: MongodbService; private readonly logs: LogsUtils; constructor(appName?: string) { this._appName = appName || 'App'; this.maria = new MariadbService(this._appName + ' DbInteractions'); - this.mongo = new MongodbService(this._appName + ' DbInteractions'); + //this.mongo = new MongodbService(this._appName + ' DbInteractions'); this.logs = new LogsUtils(this._appName + ' DbInteractions'); } @@ -31,7 +31,7 @@ export class DatabasesService { return result; } - async getUserById(id: number) { + async getUserById(id: string) { const result: Array = await this.maria.execute(`SELECT * FROM users WHERE id = ?`, [id]) as unknown as Array; this.logs.debug(`Fetching user with id ${id} from database...`, `${result?.length} user(s) found.`); return result; @@ -69,6 +69,27 @@ export class DatabasesService { return false } } + //ToTest + async editUser(userId: string, data: object): Promise { + const factorized = await this.maria.factorize({ + values: data, + actionName: 'Editing a user', + throwOnError: true + }); + const valuesArray = factorized._valuesArray; + const keysArray = factorized._keysTemplate.split(','); + const setFields = keysArray.map((key) => `${key} = ?`).join(', '); + const _sql = `UPDATE users SET ${setFields} WHERE id = ?`; + valuesArray.push(userId); + try { + const result = await this.maria.execute(_sql, valuesArray) as unknown as MariaDbStatusResult; + this.logs.debug(`Edited user with id ${userId}`, `Rows affected: ${result.affectedRows}`); + return result; + } catch (err) { + this.logs.softError('An error occurred.', err); + return {}; + } + } } export const justForTesting = new DatabasesService('OnlyDevs') \ No newline at end of file