Skip to content

Commit 87bd0f1

Browse files
committed
components: brand_controller + brand_routes
1 parent 8b4525b commit 87bd0f1

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

components/brand/brand_controller.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import slugify from "slugify";
2+
import { catchAsyncError } from '../../utils/catch_async_error';
3+
import { AppError } from '../../utils/app_error';
4+
import { brandModel } from '../../models/brand_model';
5+
import { deleteOne } from '../../handler/factor';
6+
import { ApiFeatures } from '../../utils/api_feature';
7+
8+
const addBrand = catchAsyncError(async (req, res, next) => {
9+
req.body.slug = slugify(req.body.name);
10+
const addBrand = new brandModel(req.body);
11+
await addBrand.save();
12+
13+
res.status(201).json({ message: "success", addBrand });
14+
});
15+
16+
const getAllBrands = catchAsyncError(async (req, res, next) => {
17+
let apiFeature = new ApiFeatures(brandModel.find(), req.query)
18+
.pagination()
19+
.fields()
20+
.filteration()
21+
.search()
22+
.sort();
23+
const PAGE_NUMBER = apiFeature.queryString.page * 1 || 1;
24+
const getAllBrands = await apiFeature.mongooseQuery;
25+
26+
res.status(201).json({ page: PAGE_NUMBER, message: "success", getAllBrands });
27+
});
28+
29+
const updateBrand = catchAsyncError(async (req, res, next) => {
30+
const { id } = req.params;
31+
req.body.slug = slugify(req.body.name);
32+
const updateBrand = await brandModel.findByIdAndUpdate(id, req.body, {
33+
new: true,
34+
});
35+
36+
updateBrand && res.status(201).json({ message: "success", updateBrand });
37+
38+
!updateBrand && next(new AppError("Brand was not found", 404));
39+
});
40+
41+
const deleteBrand = deleteOne(brandModel, "brand");
42+
export { addBrand, getAllBrands, updateBrand, deleteBrand };

components/brand/brand_routes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import express from "express";
2+
import * as brand from './brand_controller.js';
3+
import { validate } from "../../middleware/validation.js";
4+
import {
5+
addBrandValidation,
6+
deleteBrandValidation,
7+
updateBrandValidation,
8+
} from "./brand_validation.js";
9+
import { allowedTo, protectedRoutes } from '../auth/auth_controller.js';
10+
11+
const brandRouter = express.Router();
12+
13+
brandRouter.route("/").post(protectedRoutes, allowedTo("admin", "user"), validate(addBrandValidation), brand.addBrand).get(brand.getAllBrands);
14+
15+
brandRouter.route("/:id").put(protectedRoutes, allowedTo("admin"), validate(updateBrandValidation), brand.updateBrand).delete(protectedRoutes, allowedTo("admin"), validate(deleteBrandValidation), brand.deleteBrand);
16+
17+
export default brandRouter;

0 commit comments

Comments
 (0)