Repository navigation
Expand file tree
/
Copy pathitems.js
More file actions
161 lines (139 loc) · 4.33 KB
/
Copy pathitems.js
File metadata and controls
161 lines (139 loc) · 4.33 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const {Products} = require('./db');
const { dbconn} = require('./connect');
const mongoose = require('mongoose');
const express = require('express');
const Auth = require('./middleware/Auth');
const router = express.Router();
router.use(express.json());
router.get('/' ,async(req,res)=>{
try{
const products = await Products.find()
return res.status(200).json({
message :"retrieved data succesfully",
products
})
}
catch(err){
return res.status(500).json({
message:"somehting went wrong"
})
}
})
router.get('/:name',async (req,res)=>{
try {
const name1 = req.params.name;
const products = await Products.findOne({itemname:name1});
res.status(200).json({
message :"retrieved data",
products
})
} catch (err) {
return res.status(500).json({
message:"somehting went wrong",
error:err.message
})
}
})
router.post('/add' , async(req,res)=>{
try {
const { itemname , itemprice ,itemdescription ,itemimage}= req.body;
if(itemname == "" || itemname == undefined)
{
return res.status(400).json({
message :"please enter a name"
})
}
if(itemprice == "" || itemprice == undefined)
{
return res.status(400).json({
message :"please enter a price"
})
}
if(itemdescription == "" || itemdescription == undefined)
{
return res.status(400).json({
message :"please enter a description"
})
}
if(itemimage == "" || itemimage == undefined)
{
return res.status(400).json({
message :"please enter an image"
})
}
const ProductObj = { itemname , itemprice , itemdescription , itemimage};
const products = new Products(ProductObj);
await products.save()
return res.status(200).json({
message:"data saved succesfully",
ProductObj
})
} catch (err) {
return res.status(500).json({
message:"something went wrong",
error: err.message
})
}
})
router.delete('/deleteitem/:name' , async(req,res)=>{
try{
const {name} = req.params;
const products1 = await Products.findOne({itemname:name});
await Products.findByIdAndDelete(products1._id);
return res.status(200).json({
message:"deleted data succesfully",
})
}
catch(err){
return res.status(500).json({
message:"something went wrong",
error: err.message
})
}
})
router.put('/update/:id', async (req,res)=>{
try {
const id = req.params.id;
const { itemname , itemprice ,itemdescription , itemimage }= req.body;
if( itemname == "" || itemname == undefined)
{
return res.status(400).json({
message :"please enter a name"
})
}
if(itemprice == "" || itemprice == undefined)
{
return res.status(400).json({
message :"please enter a price"
})
}
if(itemdescription == "" || itemdescription == undefined)
{
return res.status(400).json({
message :"please enter a description"
})
}
if(itemimage == "" || itemimage == undefined)
{
return res.status(400).json({
message :"please enter a image"
})
}
const products = await Products.findById(id);
products.itemname = itemname;
products.itemprice = itemprice;
products.itemdescription = itemdescription;
await products.save()
return res.status(200).json({
message:"data updated succesfully!"
})
}
catch(err){
return res.status(500).json({
message:"something went wrong",
error: err.message
})
}
})
dbconn();
module.exports = router;