source: backend/routes/categoryRoutes.js

Last change on this file was a2e5735, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 19 months ago

Final Version

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import express from "express";
2import expressAsyncHandler from "express-async-handler";
3import Category from "../models/categoryModel.js";
4import SubCategory from "../models/subCategoryModel.js";
5
6const categoryRouter = express.Router();
7
8categoryRouter.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
23categoryRouter.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
43categoryRouter.get(
44 "/getCategories",
45 expressAsyncHandler(async (req, res) => {
46 const categories = await Category.find();
47 res.send(categories);
48 })
49);
50
51categoryRouter.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
59categoryRouter.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
73categoryRouter.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
83export default categoryRouter;
Note: See TracBrowser for help on using the repository browser.