From aecaf83d85220fed2dca6f965c6897bdea17b456 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 26 Apr 2024 09:40:24 +0200 Subject: [PATCH] feat(services): add getBySlugBrand function in brand.service Adds a new function `getBySlugBrand` in `brand.service.ts`. This function retrieves a brand by its slug, providing a more specific search option. It implements error logging for missing slug or brand not found scenarios, and successful retrieval of the brand. Issue: #13 Signed-off-by: Mathis --- src/services/brand.service.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/services/brand.service.ts b/src/services/brand.service.ts index d5d2ada..b1ca699 100644 --- a/src/services/brand.service.ts +++ b/src/services/brand.service.ts @@ -78,10 +78,31 @@ async function getAllBrand(): Promise| false> { 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; +} + const BrandService = { create: createBrand, update: updateBrand, getAll: getAllBrand, + getBySlug: getBySlugBrand, } export default BrandService; \ No newline at end of file