feat(services): add getById and delete methods in mysql.service.ts
The changes introduce two new methods in mysql.service.ts. The getById method retrieves a category from the database using its ID while the delete method will delete a category from the database using the provided category ID. Both methods return promises. Issue: #6 Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
3328c5a1df
commit
0abdbab627
@ -441,6 +441,14 @@ const MySqlService = {
|
||||
}
|
||||
})
|
||||
},
|
||||
/**
|
||||
* Retrieves a category from the database by its ID.
|
||||
*
|
||||
* @param {MysqlHandler} handler - The MySQL handler object used for execution.
|
||||
* @param {string} categoryId - The ID of the category to retrieve.
|
||||
* @returns {Promise<IDbCategory>} - A Promise that resolves with the category object.
|
||||
* @throws {Error} - If an error occurs during execution.
|
||||
*/
|
||||
getById(handler: MysqlHandler, categoryId: string): Promise<IDbCategory> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!categoryId) return reject('slug is undefined')
|
||||
@ -454,6 +462,19 @@ const MySqlService = {
|
||||
}
|
||||
});
|
||||
},
|
||||
delete(handler: MysqlHandler, categoryId: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!categoryId) return reject('Id is undefined');
|
||||
if (categoryId.length !== 36) return reject('Id invalid');
|
||||
const _sql = "DELETE FROM `categories` WHERE `id` = ?";
|
||||
const _values = [categoryId];
|
||||
try {
|
||||
resolve(handler.execute(_sql, _values));
|
||||
} catch (err: unknown) {
|
||||
reject(err as Error);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user