import type {Request, Response} from "express"; import {Logger} from "tslog"; import type IDbCategory from "@interfaces/database/IDbCategory"; import CategoryService from "@services/category.service"; //import {validationResult} from "express-validator"; const logger = new Logger({ name: "CategoryController" }); /** * Creates a new category. * * @param {Request} req - The request object containing the category information. * @param {Response} res - The response object to send back to the client. * @returns {Promise} The response object indicating the outcome of the category creation. */ async function createCategory(req: Request, res: Response): Promise { const body: IDbCategory = req.body const doesExist = await CategoryService.getBySlug(`${body.slug_name}`) if (doesExist) { logger.error("Category already exists"); return res.status(400).json({ error: "Category already exists" }); } const createResult = await CategoryService.create({ display_name: `${body.display_name}`, slug_name: `${body.slug_name}` }) if (!createResult) { 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} - A promise that will be resolved with the result of the update operation. */ async function updateCategory(req: Request, res:Response): Promise { 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" }); } /** * Deletes a category by its slug. * * @param {Request} req - The request object containing the category slug. * @param {Response} res - The response object to send the result. * @returns {Promise} A Promise that resolves to the response object. */ async function deleteCategory(req: Request, res: Response): Promise { const categorySlug = req.params["categorySlug"]; if (!categorySlug) { logger.error("Category slug is missing"); return res.status(400).json({ error: "Category slug is missing" }); } const doesExist = await CategoryService.getBySlug(`${categorySlug}`); if (!doesExist || !doesExist.id) { logger.error("Category not found"); return res.status(404).json({ error: "Category not found" }); } const deleteResult = await CategoryService.delete(`${doesExist.id}`); if (!deleteResult) { logger.error("Failed to delete category"); return res.status(500).json({ error: "Failed to delete category" }); } logger.info(`Category deleted successfully! (${categorySlug})`); return res.status(200).json({ message: "Category deleted successfully" }); } /** * Retrieves all categories. * * @param _req * @param {Response} res - The response object. * @return {Promise} - A promise that resolves to the response object. */ async function getAllCategory(_req: Request, res: Response): Promise { const categories = await CategoryService.getAll(); if (!categories) { logger.error("Failed to get categories"); return res.status(500).json({ error: "Failed to get categories" }); } logger.info("Categories retrieved successfully"); return res .status(200) .json({ iat: Date.now(), categories: categories.map((category: IDbCategory) => ({ id: category.id, display_name: category.display_name, slug_name: category.slug_name })), total: categories.length }); } /** * Get category by slug * * @param {Request} req - The request object containing category slug * @param {Response} res - The response object to send back the category * * @return {Promise} - The response with category data or error message */ async function getBySlugCategory(req: Request, res:Response): Promise { const categorySlug = req.params["categorySlug"]; if (!categorySlug) { logger.error("Category slug is missing"); return res.status(400).json({ error: "Category slug is missing" }); } const category = await CategoryService.getBySlug(`${categorySlug}`); if (!category || !category.id) { logger.error("Category not found"); return res.status(404).json({ error: "Category not found" }); } logger.info(`Category retrieved successfully! (${categorySlug})`); return res.status(200).json({ id: category.id, display_name: category.display_name, slug_name: category.slug_name }); } const CategoryController = { create: createCategory, update: updateCategory, delete: deleteCategory, getAll: getAllCategory, getBySlug: getBySlugCategory } export default CategoryController;