Skip to content

Commit cc4f8a8

Browse files
committed
add suppliers
1 parent 3310e19 commit cc4f8a8

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

modules/suppliers.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { default: mongoose } = require("mongoose");
2+
3+
const suppliersSchema = new mongoose.Schema(
4+
{
5+
name: {
6+
type: String,
7+
required: true,
8+
},
9+
website: String,
10+
},
11+
{ timestamps: true }
12+
);
13+
14+
module.exports = mongoose.model("suppliers", suppliersSchema);

routers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const routerCart = require("./routerCart");
1313
const routerOrder = require("./routerOrder");
1414
const routerTexes = require("./routerTexes");
1515
const routerReqProduct = require("./routerReqProduct");
16+
const routerSuppliers = require("./routerSuppliers");
1617

1718
function mountRoutes(app) {
1819
app.use("/api/v1/category", routerCategorie);
@@ -29,5 +30,6 @@ function mountRoutes(app) {
2930
app.use("/api/v1/order", routerOrder);
3031
app.use("/api/v1/texes", routerTexes);
3132
app.use("/api/v1/reqProduct", routerReqProduct);
33+
app.use("/api/v1/suppliers", routerSuppliers);
3234
}
3335
module.exports = mountRoutes;

routers/routerSuppliers.js

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

services/suppliersServiece.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const expressAsyncHandler = require("express-async-handler");
2+
3+
const apiError = require("../utils/apiError");
4+
const suppliersModule = require("../modules/suppliers");
5+
6+
// All Pirmatiens Have a admin and manger
7+
// @desc Add new suppliers
8+
// @Router POST /api/v1/suppliers
9+
// @Access Private/Admin-Manger
10+
const addSuppliers = expressAsyncHandler(async (req, res, next) => {
11+
if (!req.body.name) {
12+
throw next(new apiError("Must be Enter Name suppliers", 400));
13+
}
14+
const suppliers = await suppliersModule.create(req.body);
15+
res.status(201).json({ message: "saccess", data: suppliers });
16+
});
17+
18+
// @desc Update suppliers
19+
// @Router PUT /api/v1/suppliers/:suppliersId
20+
// @Access Private/Admin-Manger
21+
const updateSuppliers = expressAsyncHandler(async (req, res, next) => {
22+
if (!req.body.name) {
23+
throw next(new apiError("Must be Enter Name suppliers", 400));
24+
}
25+
if (!req.params.suppliersId) {
26+
throw next(new apiError("Must be Enter params suppliersId", 400));
27+
}
28+
const suppliers = await suppliersModule.findByIdAndUpdate(
29+
req.params.suppliersId,
30+
req.body,
31+
{ new: true }
32+
);
33+
if (!suppliers) {
34+
throw next(new apiError("invalid suppliersId", 400));
35+
}
36+
res.status(200).json({ message: "saccess", data: suppliers });
37+
});
38+
39+
// @desc Delete suppliers
40+
// @Router DELETE /api/v1/suppliers/:suppliersId
41+
// @Access Private/Admin-Manger
42+
const deleteSuppliers = expressAsyncHandler(async (req, res, next) => {
43+
if (!req.params.suppliersId) {
44+
throw next(new apiError("Must be Enter params suppliersId", 400));
45+
}
46+
const suppliers = await suppliersModule.findByIdAndDelete(
47+
req.params.suppliersId
48+
);
49+
res.status(204).json({ message: "saccess", data: suppliers });
50+
});
51+
52+
// @desc Get All suppliers
53+
// @Router GET /api/v1/suppliers
54+
// @Access Private/Any-User
55+
const getSuppliers = expressAsyncHandler(async (req, res, next) => {
56+
const suppliers = await suppliersModule.find({});
57+
res
58+
.status(200)
59+
.json({
60+
message: "saccess",
61+
suppliers_count: suppliers.length,
62+
data: suppliers,
63+
});
64+
});
65+
66+
// @desc Get spasific supplier
67+
// @Router GET /api/v1/suppliers/:suppliersId
68+
// @Access Private/Admin-Manger
69+
const getSupplier = expressAsyncHandler(async (req, res, next) => {
70+
if (!req.params.suppliersId) {
71+
throw next(new apiError("Must be Enter params suppliersId", 400));
72+
}
73+
const suppliers = await suppliersModule.findById(req.params.suppliersId);
74+
if (!suppliers) {
75+
throw next(new apiError("not found suppliers or this id invalid", 400));
76+
}
77+
res.status(200).json({
78+
message: "saccess",
79+
data: suppliers,
80+
});
81+
});
82+
83+
module.exports = {
84+
addSuppliers,
85+
updateSuppliers,
86+
deleteSuppliers,
87+
getSuppliers,
88+
getSupplier,
89+
};

0 commit comments

Comments
 (0)