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 <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-26 09:41:37 +02:00
parent aecaf83d85
commit 6c626e0b18
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -98,11 +98,38 @@ async function getBySlugBrand(brandSlug: string): Promise<IDbBrand | false> {
return brand; 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<IDbBrand | false>} 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<IDbBrand | false> {
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 = { const BrandService = {
create: createBrand, create: createBrand,
update: updateBrand, update: updateBrand,
getAll: getAllBrand, getAll: getAllBrand,
getBySlug: getBySlugBrand, getBySlug: getBySlugBrand,
getById: getByIdBrand,
} }
export default BrandService; export default BrandService;