From ab3b3c9cf549a7c5f0afcb16dfe7da3f6a240d57 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 25 Apr 2024 14:43:41 +0200 Subject: [PATCH] feat(services): add updateCategory functionality in category.service This commit includes the addition of a new function, `updateCategory` in the category.service.ts file. This function allows for updating a category in the database. Error handling and logging are also implemented within the function. Signed-off-by: Mathis --- src/services/category.service.ts | 37 +++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/services/category.service.ts b/src/services/category.service.ts index 97c48c6..ffddc59 100644 --- a/src/services/category.service.ts +++ b/src/services/category.service.ts @@ -1,8 +1,9 @@ //FEAT Create new category -import type IDbCategory from "@interfaces/database/IDbCategory"; -import { v4 as uuidv4 } from 'uuid'; -import {Logger} from "tslog"; +//FEAT Create new category +import type { IDbCategory } from "@interfaces/database/IDbCategory"; import MysqlService from "@services/mysql.service"; +import {Logger} from "tslog"; +import { v4 as uuidv4 } from 'uuid'; const DbHandler = new MysqlService.Handler('CategoryService') const logger = new Logger({name: 'CategoryService'}) @@ -32,6 +33,35 @@ async function createCategory(data: IDbCategory): Promise { } } +/** + * Update a category in the database. + * + * @param {IDbCategory} data - The data of the category to update. + * @property {number} data.id - The id of the category. + * @property {string} [data.slug_name] - The slug name of the category. + * @property {string} [data.display_name] - The display name of the category. + * + * @returns {boolean} - Returns true if the category is updated successfully, false otherwise. + */ +async function updateCategory(data: IDbCategory) { + if (!data.id) { + logger.error("Category id is missing.") + return false + } + try { + await MysqlService.Category.update(DbHandler, { + id: data.id, + slug_name: data.slug_name, + display_name: data.display_name + }); + //TODO Return id + return true; + } catch (err) { + logger.error(err) + return false; + } +} + /** * Retrieves all categories from the database. * @@ -102,6 +132,7 @@ async function deleteCategory(id:string): Promise { const CategoryService = { create: createCategory, delete: deleteCategory, + update: updateCategory, getAll, getBySlug, getById