Ignore:
Timestamp:
12/13/22 22:38:11 (2 years ago)
Author:
Nace Gjorgjievski <nace.gorgievski123@…>
Branches:
master
Parents:
113029b
Message:

Final Version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • backend/routes/productRoutes.js

    r113029b ra2e5735  
    4242      priceMontaza: req.body.priceMontaza,
    4343      countInStock: req.body.countInStock,
     44      height: req.body.H,
     45      width: req.body.W,
     46      length: req.body.L,
    4447    });
    4548
     
    5255
    5356const PAGE_SIZE = 7;
     57
     58productRouter.get(
     59  "/search",
     60  expressAsyncHandler(async (req, res) => {
     61    const { query } = req;
     62    const pageSize = query.pageSize || PAGE_SIZE;
     63    const page = query.page || 1;
     64    const searchQuery = query.text;
     65    console.log("HEEEY: " + searchQuery);
     66    const queryFilter =
     67      searchQuery && searchQuery !== "all"
     68        ? { name: { $regex: searchQuery, $options: "i" } }
     69        : {};
     70    const descriptionFilter =
     71      searchQuery && searchQuery !== "all"
     72        ? { description: { $regex: searchQuery, $options: "i" } }
     73        : {};
     74    const slug =
     75      searchQuery && searchQuery !== "all"
     76        ? { slug: { $regex: searchQuery, $options: "i" } }
     77        : {};
     78    const products = await Product.find({
     79      $or: [
     80        { name: { $regex: searchQuery, $options: "i" } },
     81        { slug: { $regex: searchQuery, $options: "i" } },
     82        { description: { $regex: searchQuery, $options: "i" } },
     83      ],
     84    })
     85      .skip(pageSize * (page - 1))
     86      .limit(pageSize);
     87    const countProducts = await Product.countDocuments({
     88      $or: [
     89        { name: { $regex: searchQuery, $options: "i" } },
     90        { slug: { $regex: searchQuery, $options: "i" } },
     91        { description: { $regex: searchQuery, $options: "i" } },
     92      ],
     93    });
     94    res.send({
     95      products,
     96      countProducts,
     97      page,
     98      pages: Math.ceil(countProducts / pageSize),
     99    });
     100  })
     101);
     102
    54103productRouter.get(
    55104  "/",
     
    62111    const order = query.order || "";
    63112    const searchQuery = query.query || "";
     113    const HF = query.HF || 0;
     114    const HT = query.HT || 1000;
     115    const WF = query.WF || 0;
     116    const WT = query.WT || 1000;
     117    const LF = query.LF || 0;
     118    const LT = query.LT || 1000;
    64119
    65120    const queryFilter =
     
    80135      ...categoryFilter,
    81136      ...subCategoryFilter,
     137      $and: [
     138        { height: { $gte: HF } },
     139        { height: { $lte: HT } },
     140        { width: { $gte: WF } },
     141        { width: { $lte: WT } },
     142        { length: { $gte: LF } },
     143        { length: { $lte: LT } },
     144      ],
    82145    })
    83146      .sort(sortOrder)
Note: See TracChangeset for help on using the changeset viewer.