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.
This commit is contained in:
parent
c9e6cb6169
commit
baecabc93a
@ -1,5 +1,5 @@
|
|||||||
import {MariadbService} from "@services/databases/mariadb.service";
|
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 {LogsUtils} from "@utils/logs.util";
|
||||||
import {UserInDatabase} from "@interfaces/db/mariadb.interface";
|
import {UserInDatabase} from "@interfaces/db/mariadb.interface";
|
||||||
|
|
||||||
@ -16,12 +16,12 @@ interface MariaDbStatusResult {
|
|||||||
export class DatabasesService {
|
export class DatabasesService {
|
||||||
private readonly _appName;
|
private readonly _appName;
|
||||||
private readonly maria: MariadbService;
|
private readonly maria: MariadbService;
|
||||||
private readonly mongo: MongodbService;
|
//private readonly mongo: MongodbService;
|
||||||
private readonly logs: LogsUtils;
|
private readonly logs: LogsUtils;
|
||||||
constructor(appName?: string) {
|
constructor(appName?: string) {
|
||||||
this._appName = appName || 'App';
|
this._appName = appName || 'App';
|
||||||
this.maria = new MariadbService(this._appName + ' DbInteractions');
|
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');
|
this.logs = new LogsUtils(this._appName + ' DbInteractions');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ export class DatabasesService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUserById(id: number) {
|
async getUserById(id: string) {
|
||||||
const result: Array<object> = await this.maria.execute(`SELECT * FROM users WHERE id = ?`, [id]) as unknown as Array<object>;
|
const result: Array<object> = await this.maria.execute(`SELECT * FROM users WHERE id = ?`, [id]) as unknown as Array<object>;
|
||||||
this.logs.debug(`Fetching user with id ${id} from database...`, `${result?.length} user(s) found.`);
|
this.logs.debug(`Fetching user with id ${id} from database...`, `${result?.length} user(s) found.`);
|
||||||
return result;
|
return result;
|
||||||
@ -69,6 +69,27 @@ export class DatabasesService {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//ToTest
|
||||||
|
async editUser(userId: string, data: object): Promise<object> {
|
||||||
|
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')
|
export const justForTesting = new DatabasesService('OnlyDevs')
|
Loading…
x
Reference in New Issue
Block a user