Compare commits

...

3 Commits

Author SHA1 Message Date
bd80518b5f
refactor(controllers): remove unused import in category.controller
Remove unused import `{IDbCategory}` from `category.controller.ts`. This reduces clutter and increases code clarity in the file.

Issue: #12
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 14:44:24 +02:00
ab3b3c9cf5
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>
2024-04-25 14:43:41 +02:00
56a1722412
feat(controllers): add update category functionality in category controller
This commit includes the creation of an updateCategory function in the category controller which allows for existing categories to be updated. It also includes appropriate error handling and response messages. In addition, it also introduces logging for the successful creation of a category.

Issue: #12
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 14:42:42 +02:00
2 changed files with 69 additions and 4 deletions

View File

@ -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;

View File

@ -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