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