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:
parent
0abdbab627
commit
38b6b05190
47
src/services/category.service.ts
Normal file
47
src/services/category.service.ts
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user