|
| 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