FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Nodejs-REST-API/Controllers/Product.Controller.js at master · mendaparadarshit/Nodejs-REST-API · GitHub
mendaparadarshit
/
Nodejs-REST-API
Public
forked from
trulymittal/Nodejs-REST-API
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Nodejs-REST-API
/
Controllers
/
Product.Controller.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
109 lines (100 loc) · 2.72 KB
Breadcrumbs
Nodejs-REST-API
/
Controllers
/
Product.Controller.js
Copy path
File metadata and controls
109 lines (100 loc) · 2.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
const
createError
=
require
(
'http-errors'
)
;
const
mongoose
=
require
(
'mongoose'
)
;
const
Product
=
require
(
'../Models/Product.model'
)
;
module
.
exports
=
{
getAllProducts
:
async
(
req
,
res
,
next
)
=>
{
try
{
const
results
=
await
Product
.
find
(
{
}
,
{
__v
:
0
}
)
;
// const results = await Product.find({}, { name: 1, price: 1, _id: 0 });
// const results = await Product.find({ price: 699 }, {});
res
.
send
(
results
)
;
}
catch
(
error
)
{
console
.
log
(
error
.
message
)
;
}
}
,
createNewProduct
:
async
(
req
,
res
,
next
)
=>
{
try
{
const
product
=
new
Product
(
req
.
body
)
;
const
result
=
await
product
.
save
(
)
;
res
.
send
(
result
)
;
}
catch
(
error
)
{
console
.
log
(
error
.
message
)
;
if
(
error
.
name
===
'ValidationError'
)
{
next
(
createError
(
422
,
error
.
message
)
)
;
return
;
}
next
(
error
)
;
}
/*Or:
If you want to use the Promise based approach*/
/*
const product = new Product({
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
res.send(result);
})
.catch(err => {
console.log(err.message);
});
*/
}
,
findProductById
:
async
(
req
,
res
,
next
)
=>
{
const
id
=
req
.
params
.
id
;
try
{
const
product
=
await
Product
.
findById
(
id
)
;
// const product = await Product.findOne({ _id: id });
if
(
!
product
)
{
throw
createError
(
404
,
'Product does not exist.'
)
;
}
res
.
send
(
product
)
;
}
catch
(
error
)
{
console
.
log
(
error
.
message
)
;
if
(
error
instanceof
mongoose
.
CastError
)
{
next
(
createError
(
400
,
'Invalid Product id'
)
)
;
return
;
}
next
(
error
)
;
}
}
,
updateAProduct
:
async
(
req
,
res
,
next
)
=>
{
try
{
const
id
=
req
.
params
.
id
;
const
updates
=
req
.
body
;
const
options
=
{
new
:
true
}
;
const
result
=
await
Product
.
findByIdAndUpdate
(
id
,
updates
,
options
)
;
if
(
!
result
)
{
throw
createError
(
404
,
'Product does not exist'
)
;
}
res
.
send
(
result
)
;
}
catch
(
error
)
{
console
.
log
(
error
.
message
)
;
if
(
error
instanceof
mongoose
.
CastError
)
{
return
next
(
createError
(
400
,
'Invalid Product Id'
)
)
;
}
next
(
error
)
;
}
}
,
deleteAProduct
:
async
(
req
,
res
,
next
)
=>
{
const
id
=
req
.
params
.
id
;
try
{
const
result
=
await
Product
.
findByIdAndDelete
(
id
)
;
// console.log(result);
if
(
!
result
)
{
throw
createError
(
404
,
'Product does not exist.'
)
;
}
res
.
send
(
result
)
;
}
catch
(
error
)
{
console
.
log
(
error
.
message
)
;
if
(
error
instanceof
mongoose
.
CastError
)
{
next
(
createError
(
400
,
'Invalid Product id'
)
)
;
return
;
}
next
(
error
)
;
}
}
}
;
Back
|
FazBrowse Home
|
New Git URL