The return type of the createCategory function in category.service.ts has been changed from unknown to boolean. Now, the function will return true if the creation is successful, and false if an error occurs. Signed-off-by: Mathis <yidhra@tuta.io>
109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
//FEAT Create new category
|
|
import type IDbCategory from "@interfaces/database/IDbCategory";
|
|
import { v4 as uuidv4 } 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<boolean>} 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<boolean> {
|
|
logger.info(`Creating a new category... (${data.display_name})`)
|
|
try {
|
|
await MysqlService.Category.insert(DbHandler, {
|
|
id: uuidv4(),
|
|
display_name: data.display_name,
|
|
slug_name: data.slug_name
|
|
})
|
|
return true;
|
|
} catch (error) {
|
|
logger.error(`Error creating category: ${error}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves all categories from the database.
|
|
*
|
|
* @returns {Promise<Array<IDbCategory>> | null} Promise that resolves to an array of IDbCategory objects or null if an error occurred.
|
|
*/
|
|
async function getAll(): Promise<Promise<Array<IDbCategory>> | null> {
|
|
try {
|
|
logger.info("Getting all categories...");
|
|
return await MysqlService.Category.getAll(DbHandler);
|
|
} catch (error) {
|
|
logger.error(`Error getting all categories: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 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);
|
|
} catch (error) {
|
|
logger.error(`Error getting category by slug: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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.Category.getById(DbHandler, id);
|
|
} catch (error) {
|
|
logger.error(`Error getting category by id: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//FEAT Get all models in category (slug)
|
|
|
|
|
|
/**
|
|
* 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.Category.delete(DbHandler, id);
|
|
} catch (error) {
|
|
logger.error(`Error deleting category: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const CategoryService = {
|
|
create: createCategory,
|
|
delete: deleteCategory,
|
|
getAll,
|
|
getBySlug,
|
|
getById
|
|
}
|
|
|
|
export default CategoryService; |