Compare commits

...

3 Commits

Author SHA1 Message Date
6c626e0b18
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>
2024-04-26 09:41:37 +02:00
aecaf83d85
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 <yidhra@tuta.io>
2024-04-26 09:40:24 +02:00
33d44ee4b6
feat(services): add getAllBrand function in BrandService
The BrandService in the services module now includes a new functionality - the getAllBrand function. This function retrieves all brands from the database. Loggers have been added to check successful retrieval and error handling.

Issue: #13
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 09:35:55 +02:00

View File

@ -64,9 +64,72 @@ async function updateBrand(data: IDbBrand): Promise<boolean> {
return false;
}
/**
* Retrieves all brands from the database.
* @returns {Promise<Array<IDbBrand>|false>} - An array of IDbBrand objects if successful, false otherwise.
*/
async function getAllBrand(): Promise<Array<IDbBrand>| 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<IDbBrand|false>} - A promise that resolves to the retrieved brand object or false if the brand is not found.
*/
async function getBySlugBrand(brandSlug: string): Promise<IDbBrand | false> {
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<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 = {
create: createBrand,
update: updateBrand
update: updateBrand,
getAll: getAllBrand,
getBySlug: getBySlugBrand,
getById: getByIdBrand,
}
export default BrandService;