Skip to content

Commit 02f89c6

Browse files
committed
components: subcategory[controller + routes + validation]
1 parent 2643a49 commit 02f89c6

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import slugify from "slugify";
2+
import { catchAsyncError } from '../../utils/catch_async_error';
3+
import { AppError } from '../../utils/app_error';
4+
import { subCategoryModel } from '../../models/subcategory_model';
5+
import { deleteOne } from '../../handler/factor';
6+
import { ApiFeatures } from '../../utils/api_feature';
7+
8+
const addSubCategory = catchAsyncError(async (req, res, next) => {
9+
req.body.slug = slugify(req.body.name);
10+
const addSubcategory = new subCategoryModel(req.body);
11+
await addSubcategory.save();
12+
13+
res.status(201).json({ message: "success", addSubcategory });
14+
});
15+
16+
const getAllSubCategories = catchAsyncError(async (req, res, next) => {
17+
console.log(req.params);
18+
let filterObj = {};
19+
20+
21+
if (req.params.category) {
22+
filterObj = { category: req.params.category };
23+
}
24+
25+
const apiFeature = new ApiFeatures(
26+
subCategoryModel.find(filterObj),
27+
req.query
28+
).filteration();
29+
30+
const getAllSubCategories = await apiFeature.mongooseQuery;
31+
32+
res.status(201).json({ message: "success", getAllSubCategories });
33+
});
34+
35+
const updateSubCategory = catchAsyncError(async (req, res, next) => {
36+
const { id } = req.params;
37+
if (req.body.name) {
38+
req.body.slug = slugify(req.body.name);
39+
}
40+
const updateSubCategory = await subCategoryModel.findByIdAndUpdate(
41+
id,
42+
req.body,
43+
{
44+
new: true,
45+
}
46+
);
47+
48+
updateSubCategory &&
49+
res.status(201).json({ message: "success", updateSubCategory });
50+
51+
!updateSubCategory && next(new AppError("subcategory was not found", 404));
52+
});
53+
54+
const deleteSubCategory = deleteOne(subCategoryModel, "subcategory");
55+
export {
56+
addSubCategory,
57+
getAllSubCategories,
58+
updateSubCategory,
59+
deleteSubCategory,
60+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import express from "express";
2+
import * as subCategory from "./subcategory_controller.js";
3+
import { validate } from "./../../middleware/validation.js";
4+
import {
5+
addSubCategoryValidation,
6+
deleteSubCategoryValidation,
7+
updateSubCategoryValidation,
8+
} from "./subcategory_validation.js";
9+
import { allowedTo, protectedRoutes } from "../auth/auth_controller.js";
10+
11+
const subCategoryRouter = express.Router({ mergeParams: true });
12+
13+
subCategoryRouter.route("/").post(protectedRoutes, allowedTo("admin", "user"), validate(addSubCategoryValidation), subCategory.addSubCategory).get(subCategory.getAllSubCategories);
14+
15+
subCategoryRouter.route("/:id").put(protectedRoutes, allowedTo("admin", "user"), validate(updateSubCategoryValidation), subCategory.updateSubCategory).delete(protectedRoutes, allowedTo("admin", "user"), validate(deleteSubCategoryValidation), subCategory.deleteSubCategory);
16+
17+
export default subCategoryRouter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Joi from "joi";
2+
3+
const addSubCategoryValidation = Joi.object({
4+
name: Joi.string().required().min(2).trim(),
5+
category: Joi.string().hex().length(24).required(),
6+
});
7+
8+
const updateSubCategoryValidation = Joi.object({
9+
id: Joi.string().hex().length(24).required(),
10+
name: Joi.string().required().min(2).trim(),
11+
});
12+
13+
const deleteSubCategoryValidation = Joi.object({
14+
id: Joi.string().hex().length(24).required(),
15+
});
16+
17+
export {
18+
addSubCategoryValidation,
19+
updateSubCategoryValidation,
20+
deleteSubCategoryValidation,
21+
};

0 commit comments

Comments
 (0)