Compare commits
3 Commits
36ad90eda6
...
bd80518b5f
Author | SHA1 | Date | |
---|---|---|---|
bd80518b5f | |||
ab3b3c9cf5 | |||
56a1722412 |
@ -30,12 +30,46 @@ async function createCategory(req: Request, res: Response): Promise<Response> {
|
||||
logger.error("Failed to create category");
|
||||
return res.status(500).json({ error: "Failed to create category" });
|
||||
}
|
||||
logger.info(`Category created successfully ! (${body.slug_name})`)
|
||||
return res.status(201).json({ message: "Category created successfully" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a category in the database.
|
||||
*
|
||||
* @param {Request} req - The request object containing the new category data in the request body.
|
||||
* @param {Response} res - The response object used to send the result of the update operation.
|
||||
*
|
||||
* @return {Promise<Response>} - A promise that will be resolved with the result of the update operation.
|
||||
*/
|
||||
async function updateCategory(req: Request, res:Response): Promise<Response> {
|
||||
const body: IDbCategory = req.body;
|
||||
const categoryId = req.params["categorySlug"];
|
||||
if (!categoryId) {
|
||||
logger.error("Category slug is missing");
|
||||
return res.status(400).json({ error: "Category slug is missing" });
|
||||
}
|
||||
const doesExist = await CategoryService.getById(`${categoryId}`)
|
||||
if (!doesExist || !doesExist.id) {
|
||||
logger.error("Category not found");
|
||||
return res.status(404).json({ error: "Category not found" });
|
||||
}
|
||||
const updateResult = await CategoryService.update({
|
||||
id: doesExist.id,
|
||||
slug_name: `${body.slug_name}`,
|
||||
display_name: `${body.display_name}`
|
||||
})
|
||||
if (!updateResult) {
|
||||
logger.error("Failed to update category");
|
||||
return res.status(500).json({ error: "Failed to update category" });
|
||||
}
|
||||
logger.info(`Category updated successfully! (${categoryId})`);
|
||||
return res.status(200).json({ message: "Category updated successfully" });
|
||||
}
|
||||
|
||||
const CategoryController = {
|
||||
create: createCategory
|
||||
create: createCategory,
|
||||
update: updateCategory,
|
||||
}
|
||||
|
||||
export default CategoryController;
|
@ -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