Centralized errors and added their translations. Didn't test
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import db from '../db.js'
|
||||
import customError from '../response/customError.js';
|
||||
import responseCodes from '../response/responseCodes.js';
|
||||
import statuses from '../utils/status.js';
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
@@ -12,12 +14,14 @@ class UserService {
|
||||
|
||||
async getByUsername(username) {
|
||||
let user = (await db.query("SELECT * FROM Users WHERE username = $1", [username])).rows;
|
||||
if (user == undefined) return statuses.not_found;
|
||||
if (user == undefined) throw new customError(`getByUsername user not found`, responseCodes.responses.usernames.not_found);
|
||||
return (user[0]);
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return (await db.query("SELECT * FROM Users")).rows;
|
||||
let res = await db.query("SELECT * FROM Users");
|
||||
if (res.rowCount == 0) throw new customError(`getAll user not found`, responseCodes.responses.usernames.not_found);
|
||||
return res.rows[0]
|
||||
}
|
||||
|
||||
async isInGroup(userId, groupId) {
|
||||
@@ -25,7 +29,7 @@ class UserService {
|
||||
}
|
||||
|
||||
async joinGroup(userId, groupId) {
|
||||
if (await (this.isInGroup(userId, groupId))) return statuses.duplicate;
|
||||
if (await (this.isInGroup(userId, groupId))) throw new customError(`joinGroup user already in group`, responseCodes.responses.user.already_in_group);
|
||||
await db.query("UPDATE Users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
|
||||
return statuses.ok;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user