feat(services): update methods in category service

Refactor the category service by:
- Calling methods from MysqlService.IDbCategory instead of MysqlService.Category.
- Adding new methods: getById and deleteCategory with typed return and clear comments.
These changes enhances code flexibility and readability.

Issue: #11
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 13:55:55 +02:00
parent 11be3a40c3
commit 325c6ec6a0
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -19,7 +19,7 @@ const logger = new Logger({name: 'CategoryService'})
async function createCategory(data: IDbCategory): Promise<unknown> {
logger.info(`Creating a new category... (${data.display_name})`)
try {
return await MysqlService.Category.insert(DbHandler, {
return await MysqlService.IDbCategory.insert(DbHandler, {
id: uuidv4(),
display_name: data.display_name,
slug_name: data.slug_name
@ -38,34 +38,71 @@ async function createCategory(data: IDbCategory): Promise<unknown> {
async function getAll(): Promise<Promise<Array<IDbCategory>> | null> {
try {
logger.info("Getting all categories...");
return await MysqlService.Category.getAll(DbHandler);
return await MysqlService.IDbCategory.getAll(DbHandler);
} catch (error) {
logger.error(`Error getting all categories: ${error}`);
return null;
}
}
//FEAT Get a category (slug)
async function getBySlug(slug: string) {
/**
* Gets a category by its slug
*
* @param {string} slug - The slug of the category
* @return {Promise<IDbCategory|null>} - A promise that resolves to the category object or null if not found
*/
async function getBySlug(slug: string): Promise<IDbCategory | null> {
try {
logger.info(`Getting category by slug... (${slug})`);
return await MysqlService.Category.getBySlug(DbHandler, slug);
return await MysqlService.IDbCategory.getBySlug(DbHandler, slug);
} catch (error) {
logger.error(`Error getting category by slug: ${error}`);
return null;
}
}
//FEAT Get a category (id)
/**
* Retrieves a category from the database by its id.
*
* @param {string} id - The id of the category to retrieve.
* @returns {Promise<IDbCategory | null>} - A Promise that resolves with the retrieved category object or null if not found.
*/
async function getById(id: string):Promise<IDbCategory | null> {
try {
logger.info(`Getting category by id... (${id})`);
return await MysqlService.IDbCategory.getById(DbHandler, id);
} catch (error) {
logger.error(`Error getting category by id: ${error}`);
return null;
}
}
//FEAT Get all models in category (slug)
//FEAT Delete a category (id)
/**
* Deletes a category with the given ID from the database.
*
* @param {string} id - The ID of the category to delete.
* @return {Promise} - A Promise that resolves to the deleted category if successful, or null if an error occurs.
*/
async function deleteCategory(id:string): Promise<unknown> {
try {
logger.info(`Deleting category... (${id})`);
return await MysqlService.IDbCategory.delete(DbHandler, id);
} catch (error) {
logger.error(`Error deleting category: ${error}`);
return null;
}
}
const CategoryService = {
create: createCategory,
delete: deleteCategory,
getAll,
getBySlug
getBySlug,
getById
}
export default CategoryService;