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

View File

@ -363,7 +363,7 @@ const MySqlService = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!brandId) return reject('slug is undefined') if (!brandId) return reject('slug is undefined')
if (brandId.length !== 36) return reject('Id invalid'); if (brandId.length !== 36) return reject('Id invalid');
const _sql = "SELECT * FROM `categories` WHERE `id` = ?"; const _sql = "SELECT * FROM `brands` WHERE `id` = ?";
const _values = [brandId]; const _values = [brandId];
try { try {
resolve(handler.execute(_sql, _values) as unknown as IDbBrand); resolve(handler.execute(_sql, _values) as unknown as IDbBrand);
@ -372,6 +372,26 @@ const MySqlService = {
reject(err as 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: { Model: {