feat(services): add updateCategory functionality in category.service
This commit includes the addition of a new function, `updateCategory` in the category.service.ts file. This function allows for updating a category in the database. Error handling and logging are also implemented within the function. Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
56a1722412
commit
ab3b3c9cf5
@ -1,8 +1,9 @@
|
||||
//FEAT Create new category
|
||||
import type IDbCategory from "@interfaces/database/IDbCategory";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import {Logger} from "tslog";
|
||||
//FEAT Create new category
|
||||
import type { IDbCategory } from "@interfaces/database/IDbCategory";
|
||||
import MysqlService from "@services/mysql.service";
|
||||
import {Logger} from "tslog";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
const DbHandler = new MysqlService.Handler('CategoryService')
|
||||
const logger = new Logger({name: 'CategoryService'})
|
||||
@ -32,6 +33,35 @@ async function createCategory(data: IDbCategory): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a category in the database.
|
||||
*
|
||||
* @param {IDbCategory} data - The data of the category to update.
|
||||
* @property {number} data.id - The id of the category.
|
||||
* @property {string} [data.slug_name] - The slug name of the category.
|
||||
* @property {string} [data.display_name] - The display name of the category.
|
||||
*
|
||||
* @returns {boolean} - Returns true if the category is updated successfully, false otherwise.
|
||||
*/
|
||||
async function updateCategory(data: IDbCategory) {
|
||||
if (!data.id) {
|
||||
logger.error("Category id is missing.")
|
||||
return false
|
||||
}
|
||||
try {
|
||||
await MysqlService.Category.update(DbHandler, {
|
||||
id: data.id,
|
||||
slug_name: data.slug_name,
|
||||
display_name: data.display_name
|
||||
});
|
||||
//TODO Return id
|
||||
return true;
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all categories from the database.
|
||||
*
|
||||
@ -102,6 +132,7 @@ async function deleteCategory(id:string): Promise<unknown> {
|
||||
const CategoryService = {
|
||||
create: createCategory,
|
||||
delete: deleteCategory,
|
||||
update: updateCategory,
|
||||
getAll,
|
||||
getBySlug,
|
||||
getById
|
||||
|
Loading…
x
Reference in New Issue
Block a user