Changeset a2e5735 for backend/routes/productRoutes.js
- Timestamp:
- 12/13/22 22:38:11 (2 years ago)
- Branches:
- master
- Parents:
- 113029b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/routes/productRoutes.js
r113029b ra2e5735 42 42 priceMontaza: req.body.priceMontaza, 43 43 countInStock: req.body.countInStock, 44 height: req.body.H, 45 width: req.body.W, 46 length: req.body.L, 44 47 }); 45 48 … … 52 55 53 56 const PAGE_SIZE = 7; 57 58 productRouter.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 54 103 productRouter.get( 55 104 "/", … … 62 111 const order = query.order || ""; 63 112 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; 64 119 65 120 const queryFilter = … … 80 135 ...categoryFilter, 81 136 ...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 ], 82 145 }) 83 146 .sort(sortOrder)
Note:
See TracChangeset
for help on using the changeset viewer.