From 90cd80e54069b6ca9349701a99bf83d53832cd09 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 26 Apr 2024 09:30:03 +0200 Subject: [PATCH] feat(services): add updateBrand function to BrandService A new function `updateBrand` has been introduced to `BrandService`. This function handles updating a brand in the database, including checks for missing `id`, brand existence by `slug_name`, and logging for successful or failed updates. Issue: #13 Signed-off-by: Mathis --- src/services/brand.service.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/services/brand.service.ts b/src/services/brand.service.ts index 72e3a0c..ffd1f77 100644 --- a/src/services/brand.service.ts +++ b/src/services/brand.service.ts @@ -34,8 +34,39 @@ async function createBrand(data: IDbBrand): Promise { return { error: 'failed' }; } +/** + * Updates a brand in the database. + * + * @param {IDbBrand} data - The brand data to update. + * @returns {Promise} - Indicates whether the update was successful or not. + */ +async function updateBrand(data: IDbBrand): Promise { + if (!data.id) { + logger.error("Brand ID is missing"); + return false; + } + const doesExist = await MysqlService.Brand.getBySlug(DbHandler, data.slug_name); + if (doesExist && doesExist.id !== data.id) { + logger.error(`Brand already exists (${data.slug_name})`); + return false; + } + const updatedBrand = await MysqlService.Brand.update(DbHandler, { + id: data.id, + slug_name: `${data.slug_name}`, + display_name: `${data.display_name}`, + image_blob: data.image_blob + }); + if (updatedBrand) { + logger.info(`Brand updated successfully (${data.slug_name})`); + return true; + } + logger.error(`Failed to update brand (${data.slug_name})`); + return false; +} + const BrandService = { - create: createBrand + create: createBrand, + update: updateBrand } export default BrandService; \ No newline at end of file