From 6c626e0b18a511298eee818ba7e2e3cf1fe92933 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 26 Apr 2024 09:41:37 +0200 Subject: [PATCH] feat(services): add getByIdBrand function in brand service A new function, `getByIdBrand`, has been added to the brand service. This function retrieves a brand from the database based on the provided brand ID, Checks are performed on the brand ID before the retrieval attempts. Issue: #13 Signed-off-by: Mathis --- src/services/brand.service.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/services/brand.service.ts b/src/services/brand.service.ts index b1ca699..20b935d 100644 --- a/src/services/brand.service.ts +++ b/src/services/brand.service.ts @@ -98,11 +98,38 @@ async function getBySlugBrand(brandSlug: string): Promise { 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; +} + const BrandService = { create: createBrand, update: updateBrand, getAll: getAllBrand, getBySlug: getBySlugBrand, + getById: getByIdBrand, } export default BrandService; \ No newline at end of file