2024-10-27 05:35:36 +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 05:35:36 +03:00
|
|
|
|
|
|
|
class CategoryService {
|
|
|
|
async create(groupId, categoryId, name) {
|
2024-10-27 05:45:12 +03:00
|
|
|
await db.query("INSERT INTO categories (group_id, local_id, name) VALUES ($1, $2, $3)", [groupId, categoryId, name]);
|
2024-10-27 05:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async update(groupId, categoryId, name) {
|
|
|
|
await db.query("UPDATE categories SET name = $1 WHERE group_id = $2 AND local_id = $3", [name, groupId, categoryId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getById(groupId, localId) {
|
2024-10-31 06:33:57 +03:00
|
|
|
let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2", [groupId, localId]));
|
|
|
|
if (result.rowCount == 0) throw new customError(`getById categorirs not found`, responseCodes.responses.category.not_found)
|
2024-10-27 05:45:12 +03:00
|
|
|
return result.rows[0];
|
2024-10-27 05:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async getByName(groupId, localId, name) {
|
|
|
|
let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2 AND name = $3", [groupId, localId, name]));
|
2024-10-31 06:33:57 +03:00
|
|
|
if (result.rowCount == 0) throw new customError(`getByName categories not found`, responseCodes.responses.category.not_found)
|
2024-10-27 05:35:36 +03:00
|
|
|
return result.rows[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new CategoryService();
|