Changeset 55ed171 for backend/routes


Ignore:
Timestamp:
10/15/22 15:28:57 (2 years ago)
Author:
Nace Gjorgjievski <nace.gorgievski123@…>
Branches:
master
Children:
113029b
Parents:
ee05663
Message:

Full Admin Functionality Added

Location:
backend/routes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • backend/routes/orderRoutes.js

    ree05663 r55ed171  
    2121      isPaid: req.body.isPaid,
    2222      paidAt: req.body.paidAt,
     23      isConfirmed: req.body.isConfirmed,
     24      contactNumber: req.body.contactNumber,
    2325    });
    2426    const order = await newOrder.save();
     
    3335    const orders = await Order.find({ user: req.user._id });
    3436    res.send(orders);
     37  })
     38);
     39const PAGE_SIZE = 10;
     40orderRouter.get(
     41  "/",
     42  expressAsyncHandler(async (req, res) => {
     43    const { query } = req;
     44    const pageSize = query.pageSize || PAGE_SIZE;
     45    const page = query.page || 1;
     46    const isConfirmed = query.isConfirmed || "";
     47    const isShipped = query.isShipped || "";
     48    const searchQuery = query.query || "";
     49    const isDelivered = false;
     50    const queryFilter =
     51      searchQuery && searchQuery !== "all"
     52        ? { _: { $regex: searchQuery, $options: "i" } }
     53        : {};
     54    const isConfirmedFilter =
     55      isConfirmed && isConfirmed !== "all" ? { isConfirmed } : {};
     56    const isShippedFilter =
     57      isShipped && isShipped !== "all" ? { isShipped } : {};
     58
     59    const orders = await Order.find({
     60      ...queryFilter,
     61      ...isConfirmedFilter,
     62      ...isShippedFilter,
     63      ...isDelivered,
     64    })
     65      .skip(pageSize * (page - 1))
     66      .limit(pageSize);
     67    const countOrders = await Order.countDocuments({
     68      ...isConfirmedFilter,
     69      ...isShippedFilter,
     70    });
     71    res.send({
     72      orders,
     73      countOrders,
     74      page,
     75      pages: Math.ceil(countOrders / pageSize),
     76    });
     77    //const orders = await Order.find();
     78    //res.send(orders);
    3579  })
    3680);
     
    4993);
    5094
     95orderRouter.put(
     96  "/action",
     97  expressAsyncHandler(async (req, res) => {
     98    if (req.body.isConfirmed) {
     99      const order = await Order.updateOne(
     100        { _id: req.body._id },
     101        {
     102          $set: {
     103            isConfirmed: req.body.isConfirmed,
     104          },
     105        }
     106      );
     107    } else if (req.body.isShipped) {
     108      const order = await Order.updateOne(
     109        { _id: req.body._id },
     110        {
     111          $set: {
     112            isShipped: req.body.isShipped,
     113            shippedAt: req.body.shippedAt,
     114          },
     115        }
     116      );
     117    } else if (req.body.isDelivered && req.body.isPaid) {
     118      const order = await Order.updateOne(
     119        { _id: req.body._id },
     120        {
     121          $set: {
     122            isDelivered: req.body.isDelivered,
     123            deliveredAt: req.body.deliveredAt,
     124            isPaid: req.body.isPaid,
     125            paidAt: req.body.paidAt,
     126          },
     127        }
     128      );
     129    } else {
     130      const order = await Order.updateOne(
     131        { _id: req.body._id },
     132        {
     133          $set: {
     134            isDelivered: req.body.isDelivered,
     135            deliveredAt: req.body.deliveredAt,
     136          },
     137        }
     138      );
     139    }
     140
     141    res.status(200).send({ message: "Succesful" });
     142  })
     143);
     144
    51145export default orderRouter;
  • backend/routes/productRoutes.js

    ree05663 r55ed171  
    1 import express from "express";
     1import express, { query } from "express";
    22import Product from "../models/productModel.js";
     3import multer from "multer";
     4import expressAsyncHandler from "express-async-handler";
     5import userRouter from "./userRoutes.js";
    36
    47const productRouter = express.Router();
    58
     9const storage = multer.diskStorage({
     10  destination: (req, file, callback) => {
     11    callback(null, "../frontend/public/uploads/");
     12  },
     13  filename: (req, file, callback) => {
     14    callback(null, file.originalname);
     15  },
     16});
     17
     18const upload = multer({ storage: storage });
     19const multipleUpload = upload.fields([
     20  { name: "image", maxCount: 1 },
     21  { name: "sideImage", maxCount: 1 },
     22  { name: "sideImage2", maxCount: 1 },
     23  { name: "dimension", maxCount: 1 },
     24  { name: "scheme", maxCount: 1 },
     25]);
     26productRouter.post(
     27  "/add",
     28  multipleUpload,
     29  expressAsyncHandler(async (req, res) => {
     30    const newProduct = new Product({
     31      name: req.body.name,
     32      slug: req.body.slug,
     33      image: `/uploads/${req.files.image[0].originalname}`,
     34      sideImage: `/uploads/${req.files.sideImage[0].originalname}`,
     35      sideImage2: `/uploads/${req.files.sideImage2[0].originalname}`,
     36      dimension: `/uploads/${req.files.dimension[0].originalname}`,
     37      scheme: `/uploads/${req.files.scheme[0].originalname}`,
     38      category: req.body.category,
     39      subCategory: req.body.subCategory,
     40      description: req.body.description,
     41      price: req.body.price,
     42      priceMontaza: req.body.priceMontaza,
     43      countInStock: req.body.countInStock,
     44    });
     45
     46    const product = await newProduct.save();
     47    if (product)
     48      res.status(201).send({ message: "New Product Created", product });
     49    else res.status(404).send({ message: "Error creating product" });
     50  })
     51);
     52
     53const PAGE_SIZE = 20;
     54productRouter.get(
     55  "/",
     56  expressAsyncHandler(async (req, res) => {
     57    const { query } = req;
     58    const pageSize = query.pageSize || PAGE_SIZE;
     59    const page = query.page || 1;
     60    const category = query.category || "";
     61    const subCategory = query.subCategory || "";
     62    const order = query.order || "";
     63    const searchQuery = query.query || "";
     64
     65    const queryFilter =
     66      searchQuery && searchQuery !== "all"
     67        ? { name: { $regex: searchQuery, $options: "i" } }
     68        : {};
     69    const categoryFilter = category && category !== "all" ? { category } : {};
     70    const subCategoryFilter =
     71      subCategory && subCategory !== "all" ? { subCategory } : {};
     72    const sortOrder =
     73      order === "lowFirst"
     74        ? { price: 1 }
     75        : order === "highFirst"
     76        ? { price: -1 }
     77        : { createdAt: -1 };
     78    const products = await Product.find({
     79      ...queryFilter,
     80      ...categoryFilter,
     81      ...subCategoryFilter,
     82    })
     83      .sort(sortOrder)
     84      .skip(pageSize * (page - 1))
     85      .limit(pageSize);
     86
     87    const countProducts = await Product.countDocuments({
     88      ...queryFilter,
     89      ...categoryFilter,
     90      ...subCategoryFilter,
     91    });
     92    res.send({
     93      products,
     94      countProducts,
     95      page,
     96      pages: Math.ceil(countProducts / pageSize),
     97    });
     98  })
     99);
     100/*
    6101productRouter.get("/", async (req, res) => {
    7102  const products = await Product.find();
    8103  res.send(products);
    9104});
    10 
     105*/
    11106productRouter.get("/slug/:slug", async (req, res) => {
    12107  const product = await Product.findOne({ slug: req.params.slug });
     
    27122});
    28123
     124productRouter.put(
     125  "/edit",
     126  expressAsyncHandler(async (req, res) => {
     127    const product = await Product.updateOne(
     128      { slug: req.body.slug },
     129      {
     130        $set: {
     131          name: req.body.name,
     132          category: req.body.category,
     133          slug: req.body.slug,
     134          subCategory: req.body.subCategory,
     135          description: req.body.description,
     136          price: req.body.price,
     137          priceMontaza: req.body.priceMontaza,
     138          countInStock: req.body.countInStock,
     139        },
     140      }
     141    );
     142    /*
     143    if (product) {
     144      product.name = req.body.name || product.name;
     145      product.slug = req.body.slug || product.slug;
     146      product.price = req.body.price || product.price;
     147      product.category = req.body.category || product.category;
     148      product.subCategory = req.body.subCategory || product.subCategory;
     149      product.description = req.body.description || product.description;
     150      product.priceMontaza = req.body.priceMontaza || product.priceMontaza;
     151      product.countInStock = req.body.countInStock || product.countInStock;
     152      product.image = product.image;
     153      product.sideImage = product.sideImage2;
     154      product.sideImage2 = product.sideImage2;
     155
     156      const updatedProduct = await product.save();*/
     157    res.status(200).send({ message: "Succesful" });
     158    //else {
     159    //res.status(404).send({ message: "Product Not Found" });
     160  })
     161);
    29162export default productRouter;
Note: See TracChangeset for help on using the changeset viewer.