feat(services): add new BrandService with createBrand function
This commit introduces the new file `brand.service.ts` under services. Specifically, it implements the `createBrand` method which handles the creation of a new brand instance in the database if there's no existing brand with the same slug name. The function returns a promise containing the operation result. A UUID is also generated as `brandId`, and several logs will be recorded in different situations. Issue: #13 Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
b1dacb750a
commit
6e429f4f27
41
src/services/brand.service.ts
Normal file
41
src/services/brand.service.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import IDbBrand from "@interfaces/database/IDbBrand";
|
||||
import MysqlService from "@services/mysql.service";
|
||||
import {Logger} from "tslog";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
const DbHandler = new MysqlService.Handler('BrandService')
|
||||
const logger = new Logger({name: 'BrandService'})
|
||||
|
||||
//SEC Blob validation
|
||||
/**
|
||||
* Creates a brand in the database with the given data.
|
||||
*
|
||||
* @param {IDbBrand} data - The data of the brand to be created.
|
||||
* @return {Promise<unknown>} A promise that resolves to the result of the operation.
|
||||
*/
|
||||
async function createBrand(data: IDbBrand): Promise<unknown> {
|
||||
const doesExist = await MysqlService.Brand.getBySlug(DbHandler, data.slug_name);
|
||||
if (doesExist) {
|
||||
logger.error(`Brand already exists (${data.slug_name})`)
|
||||
return {error: 'exist'}
|
||||
}
|
||||
const brandId = uuidv4();
|
||||
const createdBrand = await MysqlService.Brand.insert(DbHandler, {
|
||||
id: brandId,
|
||||
slug_name: `${data.slug_name}`,
|
||||
display_name: `${data.display_name}`,
|
||||
image_blob: data.image_blob
|
||||
});
|
||||
if (createdBrand) {
|
||||
logger.info(`Brand created successfully (${data.slug_name})`);
|
||||
return { success: true, brand: createdBrand };
|
||||
}
|
||||
logger.error(`Failed to create brand (${data.slug_name})`);
|
||||
return { error: 'failed' };
|
||||
}
|
||||
|
||||
const BrandService = {
|
||||
create: createBrand
|
||||
}
|
||||
|
||||
export default BrandService;
|
Loading…
x
Reference in New Issue
Block a user