1 | import express from "express";
|
---|
2 | import expressAsyncHandler from "express-async-handler";
|
---|
3 | import Category from "../models/categoryModel.js";
|
---|
4 | import SubCategory from "../models/subCategoryModel.js";
|
---|
5 |
|
---|
6 | const categoryRouter = express.Router();
|
---|
7 |
|
---|
8 | categoryRouter.post(
|
---|
9 | "/addCategory",
|
---|
10 | expressAsyncHandler(async (req, res) => {
|
---|
11 | const newCategory = new Category({
|
---|
12 | categoryName: req.body.category,
|
---|
13 | categorySlug: req.body.categorySlug,
|
---|
14 | subCategories: req.body.categories,
|
---|
15 | });
|
---|
16 | const category = await newCategory.save();
|
---|
17 | if (category)
|
---|
18 | res.status(201).send({ message: "New Category Created", category });
|
---|
19 | else res.status(404).send({ message: "Error creating category" });
|
---|
20 | })
|
---|
21 | );
|
---|
22 |
|
---|
23 | categoryRouter.put(
|
---|
24 | "/updateCategory",
|
---|
25 | expressAsyncHandler(async (req, res) => {
|
---|
26 | const category = await Category.findOne({
|
---|
27 | categorySlug: req.body.category,
|
---|
28 | });
|
---|
29 |
|
---|
30 | if (category) {
|
---|
31 | category.categoryName = category.categoryName;
|
---|
32 | category.categorySlug = category.categorySlug;
|
---|
33 | category.subCategories = req.body.categories;
|
---|
34 |
|
---|
35 | const updatedCategory = await category.save();
|
---|
36 | res.send(updatedCategory);
|
---|
37 | } else {
|
---|
38 | res.status(404).send({ message: "Category Not Found" });
|
---|
39 | }
|
---|
40 | })
|
---|
41 | );
|
---|
42 |
|
---|
43 | categoryRouter.get(
|
---|
44 | "/getCategories",
|
---|
45 | expressAsyncHandler(async (req, res) => {
|
---|
46 | const categories = await Category.find();
|
---|
47 | res.send(categories);
|
---|
48 | })
|
---|
49 | );
|
---|
50 |
|
---|
51 | categoryRouter.get(
|
---|
52 | "/getCategory",
|
---|
53 | expressAsyncHandler(async (req, res) => {
|
---|
54 | const category = await Category.find({ categorySlug: req.query.category });
|
---|
55 | res.send(category);
|
---|
56 | })
|
---|
57 | );
|
---|
58 |
|
---|
59 | categoryRouter.post(
|
---|
60 | "/addSubCategory",
|
---|
61 | expressAsyncHandler(async (req, res) => {
|
---|
62 | const newSubCategory = new SubCategory({
|
---|
63 | subCategoryName: req.body.subCategory,
|
---|
64 | subCategorySlug: req.body.subCategorySlug,
|
---|
65 | });
|
---|
66 | const subCategory = await newSubCategory.save();
|
---|
67 | if (subCategory)
|
---|
68 | res.status(201).send({ message: "New Category Created", subCategory });
|
---|
69 | else res.status(404).send({ message: "Error creating category" });
|
---|
70 | })
|
---|
71 | );
|
---|
72 |
|
---|
73 | categoryRouter.get(
|
---|
74 | "/getSubCategory",
|
---|
75 | expressAsyncHandler(async (req, res) => {
|
---|
76 | const subCategory = await SubCategory.find({
|
---|
77 | subCategorySlug: req.query.subCategory,
|
---|
78 | });
|
---|
79 | res.send(subCategory);
|
---|
80 | })
|
---|
81 | );
|
---|
82 |
|
---|
83 | export default categoryRouter;
|
---|