From 5c0d2660021741a71a2f88216e82b57ce19fe78a Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 26 Apr 2024 09:52:09 +0200 Subject: [PATCH] feat(services): add deleteBrand function to brand.service.ts The `brand.service.ts` file is updated to include a new function `deleteBrand`, which allows a brand to be deleted from the database by its ID. Appropriate error handling and activity logging have been incorporated to ensure smooth operation. Issue: #13 Signed-off-by: Mathis --- src/services/brand.service.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/services/brand.service.ts b/src/services/brand.service.ts index 20b935d..9be94bf 100644 --- a/src/services/brand.service.ts +++ b/src/services/brand.service.ts @@ -124,12 +124,40 @@ async function getByIdBrand(brandId: string): Promise { return brand; } +//TODO get models of the brand + +/** + * Deletes a brand from the database. + * + * @param {string} brandId - The ID of the brand to delete. + * @return {Promise} - A promise that resolves to true if the brand was deleted successfully, or false otherwise. + */ +async function deleteBrand(brandId: string): Promise { + if (!brandId) { + logger.error("Brand ID is missing"); + return false; + } + if (brandId.length !== 36) { + logger.error("Invalid brand ID"); + return false; + } + //TODO verify if as models linked + const deletedBrand = await MysqlService.Brand.delete(DbHandler, brandId); + if (!deletedBrand) { + logger.error(`Failed to delete brand (${brandId})`); + return false; + } + logger.info(`Brand deleted successfully (${brandId})`); + return true; +} + const BrandService = { create: createBrand, update: updateBrand, getAll: getAllBrand, getBySlug: getBySlugBrand, getById: getByIdBrand, + delete: deleteBrand, } export default BrandService; \ No newline at end of file