feat(services): add getBySlug method to MysqlService and refine getAll method

Added a new method getBySlug to retrieve a category by its slug in mysql.service.ts. Also, updated the getAll method by specifying the return type to Promise<Array<IDbCategory>> for better type checking.

Issue: #6
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 12:27:25 +02:00
parent 25b573d3a4
commit ead64291df
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -411,7 +411,7 @@ const MySqlService = {
* - The category objects are of type IDbCategory.
* - If an error occurs, the promise will be rejected with an Error object.
*/
getAll(handler: MysqlHandler) {
getAll(handler: MysqlHandler): Promise<Array<IDbCategory>> {
return new Promise((resolve, reject) => {
const _sql = "SELECT * FROM `categories`";
try {
@ -420,6 +420,26 @@ const MySqlService = {
reject(err as Error);
}
});
},
/**
* Retrieves a category from the database by its slug.
*
* @param {MysqlHandler} handler - The MySQL handler object to execute the query.
* @param {string} slug - The slug of the category to retrieve.
* @returns {Promise<IDbCategory>} - A promise that resolves with the retrieved category object.
* @throws {Error} - If an error occurs while executing the query.
*/
getBySlug(handler: MysqlHandler, slug: string): Promise<IDbCategory> {
return new Promise((resolve, reject) => {
if (!slug) return reject('slug is undefined')
const _sql = "SELECT * FROM `categories` WHERE `slug_name` = ?";
const _values = [slug];
try {
resolve(handler.execute(_sql, _values) as unknown as IDbCategory);
} catch (err: unknown) {
reject(err as Error);
}
})
}
}
}