Skip to content

Commit 3310e19

Browse files
committed
can user send requests of products
1 parent ca1360d commit 3310e19

File tree

5 files changed

+128
-4
lines changed

5 files changed

+128
-4
lines changed

modules/reqProduct.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { default: mongoose } = require("mongoose");
2+
3+
const reqProductSchema = new mongoose.Schema(
4+
{
5+
titleNeed: {
6+
type: String,
7+
required: true,
8+
},
9+
details: String,
10+
qauntity: Number,
11+
category: String,
12+
user: {
13+
type: mongoose.Types.ObjectId,
14+
}
15+
},
16+
{ timestamps: true }
17+
);
18+
19+
module.exports = mongoose.model("reqProduct", reqProductSchema);

routers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const reouterCoupons = require("./reouterCoupons");
1212
const routerCart = require("./routerCart");
1313
const routerOrder = require("./routerOrder");
1414
const routerTexes = require("./routerTexes");
15+
const routerReqProduct = require("./routerReqProduct");
1516

1617
function mountRoutes(app) {
1718
app.use("/api/v1/category", routerCategorie);
@@ -27,5 +28,6 @@ function mountRoutes(app) {
2728
app.use("/api/v1/cart", routerCart);
2829
app.use("/api/v1/order", routerOrder);
2930
app.use("/api/v1/texes", routerTexes);
31+
app.use("/api/v1/reqProduct", routerReqProduct);
3032
}
3133
module.exports = mountRoutes;

routers/routerReqProduct.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
4+
const {
5+
getReqProducts,
6+
deleteReqProduct,
7+
addReqProduct,
8+
getReqProduct,
9+
} = require("../services/reqProductService");
10+
11+
const { protect, allowedTo } = require("../services/authService");
12+
13+
// the route '/' on actions is Create (post) and get all (get).
14+
//protect, allowedTo("user"), addReqProduct
15+
router
16+
.route("/")
17+
.post(protect, allowedTo("user"), addReqProduct)
18+
.get(protect, allowedTo("admin", "manger"), getReqProducts);
19+
// the route '/:reviewId' on actions is update (put) and delete (delete).
20+
router
21+
.route("/:reqProduct")
22+
.delete(protect, allowedTo("admin", "manger"), deleteReqProduct)
23+
.get(protect, allowedTo("admin", "manger"), getReqProduct);
24+
module.exports = router;

routers/routerReview.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
createReview,
88
updateReview,
99
deleteReview,
10-
getReviewByProductId
10+
getReviewByProductId,
1111
} = require("../services/reviewService");
1212
const meddilewareCategorieError = require("../middleware/categotieErrors");
1313
const {
@@ -19,9 +19,6 @@ const {
1919

2020
const { protect, allowedTo } = require("../services/authService");
2121

22-
23-
24-
2522
// the route '/' on actions is Create (post) and get all (get).
2623
router
2724
.route("/")

services/reqProductService.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const expressAsyncHandler = require("express-async-handler");
2+
3+
const reqProductModule = require("./../modules/reqProduct");
4+
const apiError = require("../utils/apiError");
5+
6+
7+
8+
9+
10+
11+
12+
// @desc Admin can get all Requests products
13+
// @route GET /api/v1/reqProduct
14+
// @access Private/Admin-Manger
15+
const getReqProducts = expressAsyncHandler(async (req, res, next) => {
16+
const reqProducts = await reqProductModule.find({})
17+
res.status(200).json({
18+
status: "sacces",
19+
data: reqProducts,
20+
});
21+
});
22+
23+
// @desc Admin can delete sapsific Request product
24+
// @route DELETE /api/v1/reqProduct/:reqProduct
25+
// @access Private/Admin-Manger
26+
const deleteReqProduct = expressAsyncHandler(async (req, res, next) => {
27+
const { reqProduct } = req.params;
28+
if (!req.body.titleNeed) {
29+
throw next(
30+
new apiError("You must enter the title request or id request", 400)
31+
);
32+
}
33+
const getReqProductVal = await reqProductModule.findByIdAndDelete(reqProduct);
34+
35+
res.status(204).json({
36+
status: "sacces",
37+
data: getReqProductVal,
38+
});
39+
});
40+
41+
// @desc Admin can delete sapsific Request product
42+
// @route DELETE /api/v1/reqProduct/:reqProduct
43+
// @access Private/Admin-Manger
44+
const getReqProduct = expressAsyncHandler(async (req, res, next) => {
45+
const { reqProduct } = req.params;
46+
if (!req.body.titleNeed) {
47+
throw next(
48+
new apiError("You must enter the title request or id request", 400)
49+
);
50+
}
51+
const getReqProductVal = await reqProductModule.findById(reqProduct);
52+
if (!getReqProductVal) {
53+
throw next(
54+
new apiError("Not Found Request on this id", 404)
55+
);
56+
}
57+
res.status(200).json({
58+
status: "sacces",
59+
data: getReqProductVal,
60+
});
61+
});
62+
63+
// @desc Insert Requesting products from the user side
64+
// @route POST /api/v1/reqProduct
65+
// @access Public/UserLogged
66+
const addReqProduct = expressAsyncHandler(async (req, res, next) => {
67+
if (!req.body.titleNeed) {
68+
throw next(new apiError("You must enter the request", 400));
69+
}
70+
const reqProduct = await reqProductModule.create(req.body);
71+
res.status(200).json({
72+
status: "sacces",
73+
data: reqProduct,
74+
});
75+
});
76+
77+
module.exports = {
78+
getReqProducts,
79+
deleteReqProduct,
80+
addReqProduct,
81+
getReqProduct,
82+
};

0 commit comments

Comments
 (0)