2024-10-26 05:31:22 +03:00
|
|
|
import db from '../db.js';
|
2024-10-31 06:33:57 +03:00
|
|
|
import customError from '../response/customError.js';
|
|
|
|
import responseCodes from '../response/responseCodes.js';
|
2024-10-27 04:45:13 +03:00
|
|
|
import errorHandler from '../utils/pgerrorhandler.js';
|
2024-10-26 05:31:22 +03:00
|
|
|
|
|
|
|
class GroupService {
|
|
|
|
async create(name, creatorId) {
|
2024-10-31 07:47:27 +03:00
|
|
|
let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch((e) => errorHandler(e, "groups"));
|
2024-10-26 05:31:22 +03:00
|
|
|
|
|
|
|
return res.rows[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getById(id) {
|
2024-10-27 05:45:12 +03:00
|
|
|
let res = (await db.query("SELECT * FROM groups WHERE id = $1", [id]));
|
2024-10-31 07:47:27 +03:00
|
|
|
if (res.rowCount == 0) throw new customError(`getById group not found`, responseCodes.responses.groups.id_not_found);
|
2024-10-26 05:31:22 +03:00
|
|
|
return res.rows[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAdminId(id) {
|
2024-10-27 05:45:12 +03:00
|
|
|
return (await db.query("SELECT admin_id FROM groups WHERE ID = $1", [id])).rows[0].admin_id;
|
2024-10-26 05:31:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async updatePassword(id, password) {
|
|
|
|
return (await db.query("UPDATE groups SET password = $1 WHERE id = $2", [password, id]));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPassword(id) {
|
|
|
|
return (await db.query("SELECT password FROM groups WHERE id = $1", [id])).rows[0].password;
|
|
|
|
}
|
2024-10-26 20:18:14 +03:00
|
|
|
|
|
|
|
async getByName(name) {
|
2024-10-31 06:33:57 +03:00
|
|
|
let res = (await db.query("SELECT * FROM groups WHERE name = $1", [name]));
|
|
|
|
if (res.rowCount == 0) throw new customError(`getByName group not found`, responseCodes.responses.groups.name_not_found);
|
|
|
|
return res.rows[0];
|
2024-10-26 20:18:14 +03:00
|
|
|
}
|
2024-10-26 05:31:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default new GroupService();
|