bsfe_server/src/services/group.js

34 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-10-26 05:31:22 +03:00
import db from '../db.js';
import status from '../utils/status.js';
class GroupService {
async create(name, creatorId) {
let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch (e => {
if (e.code == 23505) { // already exists
return status.duplicate;
}
})
return res.rows[0];
}
async getById(id) {
let res = (await db.query("SELECT * FROM groups WHERE id = $1", [id]))
if (res.rowCount == 0) return status.not_found;
return res.rows[0];
}
async getAdminId(id) {
return (await db.query("SELECT admin_id FROM groups WHERE ID = $1", [id])).rows[0].admin_id
}
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;
}
};
export default new GroupService();