From cd51a04abab34f864641f845adb73d4364860f7b Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 25 Apr 2024 14:52:42 +0200 Subject: [PATCH] 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 --- src/controllers/category.controller.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/controllers/category.controller.ts b/src/controllers/category.controller.ts index 5a257e7..a9e57af 100644 --- a/src/controllers/category.controller.ts +++ b/src/controllers/category.controller.ts @@ -67,9 +67,34 @@ async function updateCategory(req: Request, res:Response): Promise { return res.status(200).json({ message: "Category updated successfully" }); } +/** + * Retrieves all categories. + * + * @param {Response} res - The response object. + * @return {Promise} - A promise that resolves to the response object. + */ +async function getAllCategory(res: Response): Promise { + 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 = { create: createCategory, update: updateCategory, + getAll: getAllCategory, } export default CategoryController; \ No newline at end of file