- Timestamp:
- 12/13/22 22:38:11 (2 years ago)
- Branches:
- master
- Parents:
- 113029b
- Location:
- backend
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/models/productModel.js
r113029b ra2e5735 5 5 name: { type: String, required: true, unique: true }, 6 6 slug: { type: String, required: true, unique: true }, 7 height: { type: Number, required: true }, 8 width: { type: Number, required: true }, 9 length: { type: Number, required: true }, 7 10 image: { type: String, required: true }, 8 11 sideImage: { type: String, required: true }, -
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) -
backend/server.js
r113029b ra2e5735 7 7 import userRouter from "./routes/userRoutes.js"; 8 8 import orderRouter from "./routes/orderRoutes.js"; 9 import categoryRouter from "./routes/categoryRoutes.js"; 9 10 10 11 dotenv.config(); … … 28 29 app.use("/api/users", userRouter); 29 30 app.use("/api/orders", orderRouter); 31 app.use("/api/category", categoryRouter); 30 32 31 33 app.use((err, req, res, next) => {
Note:
See TracChangeset
for help on using the changeset viewer.