Compare commits

...

3 Commits

Author SHA1 Message Date
0ed130f7b8
feat(services): update MySQL service to fetch brand data
- Corrected the SQL query in fetching brand data based on ID.
- Added a new function, `getBySlug`, to fetch brand data using the brand slug. This ensures a flexible data access method thus enhancing the service's usability.

Issue: #5
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 16:38:41 +02:00
50c1ff797f
feat(services): add getById function to mysql.service
The code now includes a `getById` function in `mysql.service.ts`, which fetches a category by its id. This function will reject with an error message if brandId is not defined or if its length is not 36. The query is executed using a MysqlHandler instance.

Issue: #5
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 16:36:12 +02:00
2640ebbc70
feat(controllers): add detailed comment to getBySlugCategory function
The `getBySlugCategory` function in `category.controller.ts` now has a detailed comment. The comment includes a description of the function, its parameters, and its return value.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 16:31:33 +02:00
2 changed files with 53 additions and 1 deletions

View File

@ -118,7 +118,15 @@ async function getAllCategory(res: Response): Promise<Response> {
});
}
async function getBySlugCategory(req: Request, res:Response) {
/**
* Get category by slug
*
* @param {Request} req - The request object containing category slug
* @param {Response} res - The response object to send back the category
*
* @return {Promise<Response>} - The response with category data or error message
*/
async function getBySlugCategory(req: Request, res:Response): Promise<Response> {
const categorySlug = req.params["categorySlug"];
if (!categorySlug) {
logger.error("Category slug is missing");

View File

@ -348,6 +348,50 @@ const MySqlService = {
reject(err as Error);
}
})
},
/**
* Fetches a category by its Id
*
* @param {MysqlHandler} handler - The instance of the MysqlHandler used for executing the query
* @param {string} brandId - The Id of the category to be fetched
* @return {Promise<IDbBrand>} - A promise that, when resolved, returns the category matching the Id
* @throws {Error} - If an error occurs during execution
* @throws {string} - If brandId is undefined or invalid
*/
getById(handler: MysqlHandler, brandId: string): Promise<IDbBrand> {
return new Promise((resolve, reject) => {
if (!brandId) return reject('slug is undefined')
if (brandId.length !== 36) return reject('Id invalid');
const _sql = "SELECT * FROM `brands` WHERE `id` = ?";
const _values = [brandId];
try {
resolve(handler.execute(_sql, _values) as unknown as IDbBrand);
} catch (err: unknown) {
//TODO Reject with null and logger error
reject(err as Error);
}
})
},
/**
* Retrieves a brand from the database by its slug.
*
* @param {MysqlHandler} handler - The MySQL handler object used to connect to the database.
* @param {string} brandSlug - The slug of the brand to retrieve.
* @returns {Promise<IDbBrand>} - A promise that resolves with the brand object if found, or rejects with an error message.
*/
getBySlug(handler: MysqlHandler, brandSlug: string): Promise<IDbBrand> {
return new Promise((resolve, reject) => {
if (!brandSlug) return reject('slug is undefined')
const _sql = "SELECT * FROM `brands` WHERE `slug_name` = ?";
const _values = [brandSlug];
try {
resolve(handler.execute(_sql, _values) as unknown as IDbBrand);
} catch (err: unknown) {
reject(err as Error);
}
})
}
},
Model: {