From baecabc93a3ba571b7e782e2310a1af04a783b58 Mon Sep 17 00:00:00 2001 From: Mathis Date: Wed, 22 May 2024 14:20:55 +0200 Subject: [PATCH] feat(databases.service): add user editing function and disable MongodbService The commit includes the addition of an `editUser` function in `databases.service.ts` that allows users to edit their details. Also, `MongodbService` has been commented out and the getUserById function now takes in a string instead of a number for the userId. The function will need to be tested for its successful implementation. --- src/services/databases/databases.service.ts | 29 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) 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