feat(services): add CategoryService with createCategory function

A new CategoryService has been added in the services layer. This includes a function createCategory for creating a new category using IDbCategory data. Some placeholders for future features such as get, delete and retrieve by id or slug are also included.

Issue: #11
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 13:33:09 +02:00
parent 0abdbab627
commit 38b6b05190
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -0,0 +1,47 @@
//FEAT Create new category
import type IDbCategory from "@interfaces/database/IDbCategory";
import * as uuid from 'uuid'
import {Logger} from "tslog";
import MysqlService from "@services/mysql.service";
const DbHandler = new MysqlService.Handler('CategoryService')
const logger = new Logger({name: 'CategoryService'})
/**
* Creates a new category with the given data.
*
* @param {IDbCategory} data - The category data.
* @returns {Promise<unknown>} A promise that resolves with the created category.
* If an error occurs, the promise will reject with the error.
*/
async function createCategory(data: IDbCategory): Promise<unknown> {
logger.info(`Creating a new category... (${data.display_name})`)
try {
return await MysqlService.Category.insert(DbHandler, {
id: uuid.v4(undefined, undefined, undefined),
display_name: data.display_name,
slug_name: data.slug_name
})
} catch (e) {
logger.error(`Error creating category: ${e.message}`);
return;
}
}
//FEAT Get all categories
//FEAT Get a category (slug)
//FEAT Get a category (id)
//FEAT Get all models in category (slug)
//FEAT Delete a category (id)
const CategoryService = {
create: createCategory
}
export default CategoryService;