Skip to content

Commit 08556fa

Browse files
committed
componenets: category[controller + routes + validation]
1 parent 84a0366 commit 08556fa

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import slugify from "slugify";
2+
import { categoryModel } from '../../models/category_model';
3+
import { catchAsyncError } from "../../utils/catch_async_error";
4+
import { AppError } from "../../utils/app_error";
5+
import { deleteOne } from "../../handler/factor";
6+
import { ApiFeatures } from '../../utils/api_feature';
7+
8+
const addCategory = catchAsyncError(async (req, res, next) => {
9+
console.log(req.file);
10+
req.body.Image = req.file.filename;
11+
req.body.slug = slugify(req.body.name);
12+
const addcategory = new categoryModel(req.body);
13+
await addcategory.save();
14+
15+
res.status(201).json({ message: "success", addcategory });
16+
});
17+
18+
const getAllCategories = catchAsyncError(async (req, res, next) => {
19+
let apiFeature = new ApiFeatures(categoryModel.find(), req.query)
20+
.pagination()
21+
.fields()
22+
.filteration()
23+
.search()
24+
.sort();
25+
26+
const PAGE_NUMBER = apiFeature.queryString.page * 1 || 1;
27+
let getAllCategories = await apiFeature.mongooseQuery;
28+
29+
res
30+
.status(201)
31+
.json({ page: PAGE_NUMBER, message: "success", getAllCategories });
32+
});
33+
34+
const updateCategory = catchAsyncError(async (req, res, next) => {
35+
const { id } = req.params;
36+
const { name } = req.body;
37+
req.body.slug = slugify(req.body.name);
38+
const updateCategory = await categoryModel.findByIdAndUpdate(id, req.body, {
39+
new: true,
40+
});
41+
42+
updateCategory &&
43+
res.status(201).json({ message: "success", updateCategory });
44+
45+
!updateCategory && next(new AppError("category was not found", 404));
46+
});
47+
48+
const deleteCategory = deleteOne(categoryModel, "category");
49+
export { addCategory, getAllCategories, updateCategory, deleteCategory };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import express from "express";
2+
import * as category from './category_controller.js';
3+
import subCategoryRouter from '../subcategory/subcategory_routes.js'
4+
import {
5+
addCategoryValidation,
6+
deleteCategoryValidation,
7+
updateCategoryValidation,
8+
} from "./category_validation.js";
9+
import { validate } from "../../middleware/validation.js";
10+
import { uploadSingleFile } from '../../db/multer.js';
11+
import { allowedTo, protectedRoutes } from '../auth/auth_controller.js';
12+
13+
const categoryRouter = express.Router();
14+
15+
categoryRouter.use("/:categoryId/subcategories", subCategoryRouter);
16+
17+
categoryRouter.route("/").post(protectedRoutes, allowedTo("admin"), uploadSingleFile("Image", "category"), validate(addCategoryValidation), category.addCategory).get(category.getAllCategories);
18+
19+
categoryRouter.route("/:id").put(protectedRoutes, allowedTo("admin"), validate(updateCategoryValidation), category.updateCategory).delete(protectedRoutes, allowedTo("admin"), validate(deleteCategoryValidation), category.deleteCategory);
20+
21+
export default categoryRouter;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Joi from "joi";
2+
3+
const addCategoryValidation = Joi.object({
4+
name: Joi.string().required().min(3).trim(),
5+
slug: Joi.string().lowercase(),
6+
Image: Joi.string(),
7+
});
8+
9+
const updateCategoryValidation = Joi.object({
10+
name: Joi.string().min(3).trim(),
11+
id: Joi.string().hex().length(24).required(),
12+
});
13+
14+
const deleteCategoryValidation = Joi.object({
15+
id: Joi.string().hex().length(24).required(),
16+
});
17+
18+
export {
19+
addCategoryValidation,
20+
updateCategoryValidation,
21+
deleteCategoryValidation,
22+
};

0 commit comments

Comments
 (0)