import db from '../db.js';
import statuses from '../utils/status.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) return statuses.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) return statuses.not_found;
        return result.rows[0];
    }
}

export default new CategoryService();