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 <yidhra@tuta.io>
This commit is contained in:
parent
f3bddc7170
commit
90cd80e540
@ -34,8 +34,39 @@ async function createBrand(data: IDbBrand): Promise<unknown> {
|
||||
return { error: 'failed' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a brand in the database.
|
||||
*
|
||||
* @param {IDbBrand} data - The brand data to update.
|
||||
* @returns {Promise<boolean>} - Indicates whether the update was successful or not.
|
||||
*/
|
||||
async function updateBrand(data: IDbBrand): Promise<boolean> {
|
||||
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;
|
Loading…
x
Reference in New Issue
Block a user