From 50c1ff797f4c970f198d7f29a688ca8d98517142 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 25 Apr 2024 16:36:12 +0200 Subject: [PATCH] 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 --- src/services/mysql.service.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/services/mysql.service.ts b/src/services/mysql.service.ts index 5869e8f..d842ee3 100644 --- a/src/services/mysql.service.ts +++ b/src/services/mysql.service.ts @@ -348,6 +348,30 @@ 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} - 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 { + 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: {