feat(controllers): add delete and getBySlug methods in category controller

The `category.controller.ts` has been updated to handle deleting a category and retrieving a category by slug. The `deleteCategory` function takes a category slug from the request params and first checks if the category exists before attempting to delete it. While the `getBySlugCategory` function simply retrieves a category by its slug.

Issue: #12
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 15:02:46 +02:00
parent da001bf7cc
commit 62cb05cb33
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -67,6 +67,33 @@ async function updateCategory(req: Request, res:Response): Promise<Response> {
return res.status(200).json({ message: "Category updated successfully" }); return res.status(200).json({ message: "Category updated successfully" });
} }
/**
* Deletes a category by its slug.
*
* @param {Request} req - The request object containing the category slug.
* @param {Response} res - The response object to send the result.
* @returns {Promise<Response>} A Promise that resolves to the response object.
*/
async function deleteCategory(req: Request, res: Response): Promise<Response> {
const categorySlug = req.params["categorySlug"];
if (!categorySlug) {
logger.error("Category slug is missing");
return res.status(400).json({ error: "Category slug is missing" });
}
const doesExist = await CategoryService.getBySlug(`${categorySlug}`);
if (!doesExist || !doesExist.id) {
logger.error("Category not found");
return res.status(404).json({ error: "Category not found" });
}
const deleteResult = await CategoryService.delete(`${doesExist.id}`);
if (!deleteResult) {
logger.error("Failed to delete category");
return res.status(500).json({ error: "Failed to delete category" });
}
logger.info(`Category deleted successfully! (${categorySlug})`);
return res.status(200).json({ message: "Category deleted successfully" });
}
/** /**
* Retrieves all categories. * Retrieves all categories.
* *
@ -91,10 +118,31 @@ async function getAllCategory(res: Response): Promise<Response> {
}); });
} }
async function getBySlugCategory(req: Request, res:Response) {
const categorySlug = req.params["categorySlug"];
if (!categorySlug) {
logger.error("Category slug is missing");
return res.status(400).json({ error: "Category slug is missing" });
}
const category = await CategoryService.getBySlug(`${categorySlug}`);
if (!category || !category.id) {
logger.error("Category not found");
return res.status(404).json({ error: "Category not found" });
}
logger.info(`Category retrieved successfully! (${categorySlug})`);
return res.status(200).json({
id: category.id,
display_name: category.display_name,
slug_name: category.slug_name
});
}
const CategoryController = { const CategoryController = {
create: createCategory, create: createCategory,
update: updateCategory, update: updateCategory,
delete: deleteCategory,
getAll: getAllCategory, getAll: getAllCategory,
getBySlug: getBySlugCategory
} }
export default CategoryController; export default CategoryController;