feat(controllers): add BrandController with CRUD operations
This commit introduces a new `BrandController` in the controllers folder, offering key Create, Read, Update, and Delete (CRUD) operations. This includes `createBrand`, `updateBrand`, `getBySlugBrand`, `getAllBrand`, and `deleteBrand` methods, providing better control and operation of brands within the application. Issue: #14 Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
0a6321deb0
commit
c25b204c67
151
src/controllers/brand.controller.ts
Normal file
151
src/controllers/brand.controller.ts
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
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<Response> {
|
||||||
|
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<Response>} A promise that resolves with the HTTP response.
|
||||||
|
*/
|
||||||
|
async function updateBrand(req: Request, res: Response): Promise<Response> {
|
||||||
|
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<Response>} - A promise that resolves to the response with the retrieved brand.
|
||||||
|
*/
|
||||||
|
async function getBySlugBrand(req: Request, res: Response): Promise<Response> {
|
||||||
|
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 and sends the response.
|
||||||
|
*
|
||||||
|
* @param {Response} res - The response object.
|
||||||
|
* @returns {Promise<Response>} - A Promise that resolves with the response object.
|
||||||
|
*/
|
||||||
|
async function getAllBrand(res: Response): Promise<Response> {
|
||||||
|
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<Response>} - The response object indicating the success or failure of the delete operation.
|
||||||
|
*/
|
||||||
|
async function deleteBrand(req: Request, res: Response): Promise<Response> {
|
||||||
|
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" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const BrandController = {
|
||||||
|
create: createBrand,
|
||||||
|
update: updateBrand,
|
||||||
|
getBySlug: getBySlugBrand,
|
||||||
|
getAll: getAllBrand,
|
||||||
|
delete: deleteBrand,
|
||||||
|
}
|
||||||
|
|
||||||
|
export default BrandController;
|
Loading…
x
Reference in New Issue
Block a user