import type { Request, Response } from "express"; import { Logger } from "tslog"; import type IDbBrand from "@interfaces/database/IDbBrand"; import BrandService from "@services/brand.service"; //import {body} from "express-validator"; const logger = new Logger({ name: "BrandController", }); /** * Creates a new brand. * * @param req - The request object containing the brand information. * @param res - The response object used to send the response. * * @returns A Promise that resolves to the response object with a status code and a JSON message indicating the success or failure of brand creation. */ async function createBrand(req: Request, res: Response): Promise { const body: IDbBrand = req.body; const doesExist = await BrandService.getBySlug(`${body.slug_name}`); if (doesExist) { logger.error("Brand already exists"); return res.status(400).json({ error: "Brand already exists", }); } const createResult = await BrandService.create({ slug_name: `${body.slug_name}`, display_name: `${body.display_name}`, image_blob: `${body.image_blob}`, }); if (!createResult) { logger.error("Failed to create brand"); return res.status(500).json({ error: "Failed to create brand", }); } logger.info(`Brand created successfully ! (${body.slug_name})`); return res.status(201).json({ message: "Brand created successfully", }); } /** * Update a brand in the database. * * @param {Request} req - The HTTP request object. * @param {Response} res - The HTTP response object. * @return {Promise} A promise that resolves with the HTTP response. */ async function updateBrand(req: Request, res: Response): Promise { const body: IDbBrand = req.body; const brandSlug = req.params["brandSlug"]; if (!brandSlug) { logger.error("Brand slug is missing"); return res.status(400).json({ error: "Brand slug is missing", }); } const doesExist = await BrandService.getBySlug(brandSlug); if (!doesExist) { logger.error("Brand not found"); return res.status(404).json({ error: "Brand not found", }); } const updateResult = await BrandService.update({ slug_name: `${body.slug_name}`, display_name: `${body.display_name}`, image_blob: `${body.image_blob}`, }); if (!updateResult) { logger.error("Failed to update brand"); return res.status(500).json({ error: "Failed to update brand", }); } logger.info(`Brand updated successfully ! (${brandSlug})`); return res.status(200).json({ message: "Brand updated successfully", }); } /** * Retrieves a brand by its slug. * * @param {Request} req - The request object containing the brand slug in the parameters. * @param {Response} res - The response object to send the result. * @returns {Promise} - A promise that resolves to the response with the retrieved brand. */ async function getBySlugBrand(req: Request, res: Response): Promise { const brandSlug = req.params["brandSlug"]; if (!brandSlug) { logger.error("Brand slug is missing"); return res.status(400).json({ error: "Brand slug is missing", }); } const brand = await BrandService.getBySlug(brandSlug); if (!brand) { logger.error("Brand not found"); return res.status(404).json({ error: "Brand not found", }); } logger.info(`Brand retrieved successfully ! (${brandSlug})`); return res.status(200).json(brand); } /** * Retrieves all brands. * * @param {Request} _req - The request object. * @param {Response} res - The response object. * @returns {Promise} - A promise with the response object. */ async function getAllBrand(_req: Request, res: Response): Promise { const brands = await BrandService.getAll(); if (!brands) { logger.error("Failed to retrieve brands"); return res.status(500).json({ error: "Failed to retrieve brands", }); } logger.info("Brands retrieved successfully !"); return res.status(200).json({ uat: Date.now(), brands: brands, total: brands.length, }); } /** * Deletes a brand. * * @async * @param {Request} req - The request object. * @param {Response} res - The response object. * @returns {Promise} - The response object indicating the success or failure of the delete operation. */ async function deleteBrand(req: Request, res: Response): Promise { const brandSlug = req.params["brandSlug"]; if (!brandSlug) { logger.error("Brand slug is missing"); return res.status(400).json({ error: "Brand slug is missing", }); } //TODO verify if models linked to brand const doesExist = await BrandService.getBySlug(brandSlug); if (!doesExist) { logger.error("Brand not found"); return res.status(404).json({ error: "Brand not found", }); } const deleteResult = await BrandService.delete(brandSlug); if (!deleteResult) { logger.error("Failed to delete brand"); return res.status(500).json({ error: "Failed to delete brand", }); } logger.info(`Brand deleted successfully ! (${brandSlug})`); return res.status(200).json({ message: "Brand deleted successfully", }); } //TODO get models of the brand logger.debug("\nController loaded."); const BrandController = { create: createBrand, update: updateBrand, getBySlug: getBySlugBrand, getAll: getAllBrand, delete: deleteBrand, }; export default BrandController;