Compare commits
No commits in common. "bd80518b5f15f1e8c49ed1b7b86c0af1a3ba7784" and "36ad90eda6028f5aa0150168f2c45140c09b6de3" have entirely different histories.
bd80518b5f
...
36ad90eda6
@ -30,46 +30,12 @@ async function createCategory(req: Request, res: Response): Promise<Response> {
|
|||||||
logger.error("Failed to create category");
|
logger.error("Failed to create category");
|
||||||
return res.status(500).json({ 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" });
|
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 = {
|
const CategoryController = {
|
||||||
create: createCategory,
|
create: createCategory
|
||||||
update: updateCategory,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CategoryController;
|
export default CategoryController;
|
@ -1,9 +1,8 @@
|
|||||||
//FEAT Create new category
|
//FEAT Create new category
|
||||||
//FEAT Create new category
|
import type IDbCategory from "@interfaces/database/IDbCategory";
|
||||||
import type { IDbCategory } from "@interfaces/database/IDbCategory";
|
|
||||||
import MysqlService from "@services/mysql.service";
|
|
||||||
import {Logger} from "tslog";
|
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
import {Logger} from "tslog";
|
||||||
|
import MysqlService from "@services/mysql.service";
|
||||||
|
|
||||||
const DbHandler = new MysqlService.Handler('CategoryService')
|
const DbHandler = new MysqlService.Handler('CategoryService')
|
||||||
const logger = new Logger({name: 'CategoryService'})
|
const logger = new Logger({name: 'CategoryService'})
|
||||||
@ -33,35 +32,6 @@ 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.
|
* Retrieves all categories from the database.
|
||||||
*
|
*
|
||||||
@ -132,7 +102,6 @@ async function deleteCategory(id:string): Promise<unknown> {
|
|||||||
const CategoryService = {
|
const CategoryService = {
|
||||||
create: createCategory,
|
create: createCategory,
|
||||||
delete: deleteCategory,
|
delete: deleteCategory,
|
||||||
update: updateCategory,
|
|
||||||
getAll,
|
getAll,
|
||||||
getBySlug,
|
getBySlug,
|
||||||
getById
|
getById
|
||||||
|
Loading…
x
Reference in New Issue
Block a user