45 lines
1.9 KiB
JavaScript
45 lines
1.9 KiB
JavaScript
import ProductService from "./product.js";
|
|
|
|
import db from '../db.js';
|
|
import customError from '../response/customError.js';
|
|
import responseCodes from '../response/responseCodes.js';
|
|
import AbstractProductService from "./abstractproduct.js";
|
|
|
|
class CategoryService {
|
|
async create(groupId, categoryId, name) {
|
|
await db.query("INSERT INTO categories (group_id, local_id, name) VALUES ($1, $2, $3)", [groupId, categoryId, name]);
|
|
}
|
|
|
|
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) {
|
|
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)
|
|
return result.rows[0];
|
|
}
|
|
|
|
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]));
|
|
if (result.rowCount == 0) throw new customError(`getByName categories not found`, responseCodes.responses.category.not_found)
|
|
return result.rows[0];
|
|
}
|
|
|
|
async getAll(groupId) {
|
|
let result = (await db.query("SELECT group_id, local_id, name FROM categories WHERE group_id = $1", [groupId])).rows
|
|
return result
|
|
}
|
|
|
|
|
|
async delete(groupId, localId) {
|
|
let abstractProductInCategory = await AbstractProductService.getAllFromCategory(groupId, localId);
|
|
abstractProductInCategory.forEach(async (abstractProduct) => {
|
|
await AbstractProductService.delete(groupId, abstractProduct.local_id)
|
|
});
|
|
|
|
await db.query("DELETE FROM categories WHERE group_id = $1 AND local_id = $2", [groupId, localId]);
|
|
}
|
|
}
|
|
|
|
export default new CategoryService(); |