Add category edit action

This commit is contained in:
Cauê Faleiros
2026-08-10 11:15:15 -03:00
parent 4556659fcf
commit e85dbe6f10
4 changed files with 94 additions and 11 deletions

View File

@@ -110,6 +110,33 @@ const createCategory = async ({ name, description }) => {
return mapCategory(result.rows[0]);
};
const updateCategory = async (id, { name, description }) => {
const normalizedId = Number(id);
const normalizedName = normalizeText(name);
if (!normalizedId || !normalizedName) {
const error = new Error('Nome da categoria é obrigatório.');
error.statusCode = 400;
throw error;
}
const result = await pool.query(`
UPDATE catalog_categories
SET name = $1,
description = $2,
updated_at = CURRENT_TIMESTAMP
WHERE id = $3
RETURNING id, name, description, created_at, updated_at
`, [normalizedName, normalizeText(description) || null, normalizedId]);
if (!result.rows.length) {
const error = new Error('Categoria não encontrada.');
error.statusCode = 404;
throw error;
}
return mapCategory(result.rows[0]);
};
const deleteCategory = async (id) => {
await pool.query('DELETE FROM catalog_categories WHERE id = $1', [id]);
};
@@ -272,5 +299,6 @@ module.exports = {
getCatalogSummary,
listCategories,
listConsumptionReferences,
listProducts
listProducts,
updateCategory
};