import type 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 todo 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} A promise that resolves to the result of the operation. */ async function createBrand(data: IDbBrand): Promise { 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' }; } //SEC todo Blob validation /** * 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; } /** * Retrieves all brands from the database. * @returns {Promise|false>} - An array of IDbBrand objects if successful, false otherwise. */ async function getAllBrand(): Promise| false> { const brands = await MysqlService.Brand.getAll(DbHandler); if (!brands) { logger.error("Failed to retrieve brands"); return false; } logger.info(`Retrieved all brands successfully (${brands.length})`); return brands; } /** * Retrieves a brand by its slug. * * @param {string} brandSlug - The slug of the brand. * @returns {Promise} - A promise that resolves to the retrieved brand object or false if the brand is not found. */ async function getBySlugBrand(brandSlug: string): Promise { if (!brandSlug) { logger.error("Brand slug is missing"); return false; } const brand = await MysqlService.Brand.getBySlug(DbHandler, brandSlug); if (!brand) { logger.error(`Brand not found (${brandSlug})`); return false; } logger.info(`Retrieved brand by slug successfully (${brandSlug})`); return brand; } /** * Retrieves a brand from the database based on the provided brand ID. * * @param {string} brandId - The ID of the brand to retrieve. * * @returns {Promise} A promise that resolves to the retrieved brand object, or false if the brand is not found or the ID is invalid. */ async function getByIdBrand(brandId: string): Promise { if (!brandId) { logger.error("Brand ID is missing"); return false; } if (brandId.length !== 36) { logger.error("Invalid brand ID"); return false; } const brand = await MysqlService.Brand.getById(DbHandler, brandId); if (!brand) { logger.error(`Brand not found (${brandId})`); return false; } logger.info(`Retrieved brand by ID successfully (${brandId})`); return brand; } //TODO get models of the brand //TODO get stats 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;