feat(controllers): add getAllCategory function in category controller

The getAllCategory function is added in `category.controller.ts`. This function retrieves all categories and presents an error message if it fails. Specifically, it interacts with the Category service to get all categories, logs the process, and maps each element to an {`id`, `display_name`, `slug_name`} object before sending the response.

Issue: #12
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 14:52:42 +02:00
parent bd80518b5f
commit cd51a04aba
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -67,9 +67,34 @@ 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" });
} }
/**
* Retrieves all categories.
*
* @param {Response} res - The response object.
* @return {Promise<Response>} - A promise that resolves to the response object.
*/
async function getAllCategory(res: Response): Promise<Response> {
const categories = await CategoryService.getAll();
if (!categories) {
logger.error("Failed to get categories");
return res.status(500).json({ error: "Failed to get categories" });
}
logger.info("Categories retrieved successfully");
//ToTest categories output type
return res.status(200).json({
iat: Date.now(),
categories: categories.map((category: IDbCategory) => ({
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,
getAll: getAllCategory,
} }
export default CategoryController; export default CategoryController;