-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupplier.js
92 lines (75 loc) · 2.03 KB
/
Supplier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const express = require('express');
const Suppliers = require('../models/Supplier');
const router = express.Router();
//save Supplier
router.post('/supplier/save',(req,res) => {
let newSupplier = new Suppliers(req.body);
newSupplier.save((err) => {
if(err) {
return res.status(400).json({
error:err
});
}
return res.status(200).json({
success:"supplier saved successful"
});
});
});
// get Supplier
router.get('/suppliers',(req,res) =>{
Suppliers.find().exec((err,suppliers) =>{
if(err){
return res.status(400).json({
error:err
});
}
return res.status(200).json({
success:true,
existingSuppliers:suppliers
});
});
});
//get a Supplier inventry
router.get("/supplier/:id",(req,res)=>{
let supplierId = req.params.id;
Suppliers.findById(supplierId,(err,supplier)=>{
if(err){
return res.status(400).json({success:false, err});
}
return res.status(200).json({
success:true,
supplier
});
});
});
//update Supplier
router.put('/supplier/update/:id',(req,res)=>{
Suppliers.findByIdAndUpdate(
req.params.id,
{
$set:req.body
},
(err,supplier)=>{
if(err){
return res.status(400).json({
error:err
});
}
return res.status(200).json({
success:"updated successfully"
});
}
);
});
//delete Supplier
router.delete('/supplier/delete/:id',(req,res) =>{
Suppliers.findByIdAndRemove(req.params.id).exec((err,deletedSupplier) =>{
if(err) return res.status(400).json({
message:"delete unsuccesful",err
});
return res.json({
success:"delete successfully",deletedSupplier
});
});
});
module.exports = router;