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>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 16:36:12 +02:00
parent 2640ebbc70
commit 50c1ff797f
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -348,6 +348,30 @@ const MySqlService = {
reject(err as Error); 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 `categories` 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);
}
})
} }
}, },
Model: { Model: {