@@ -6,11 +6,25 @@ const Product = require("../models/product");
6
6
7
7
router . get ( "/" , ( req , res , next ) => {
8
8
Product . find ( )
9
+ . select ( "name price _id" )
9
10
. exec ( )
10
11
. then ( docs => {
11
- console . log ( docs ) ;
12
+ const response = {
13
+ count : docs . length ,
14
+ products : docs . map ( doc => {
15
+ return {
16
+ name : doc . name ,
17
+ price : doc . price ,
18
+ _id : doc . _id ,
19
+ request : {
20
+ type : "GET" ,
21
+ url : "http://localhost:3000/products/" + doc . _id
22
+ }
23
+ } ;
24
+ } )
25
+ } ;
12
26
// if (docs.length >= 0) {
13
- res . status ( 200 ) . json ( docs ) ;
27
+ res . status ( 200 ) . json ( response ) ;
14
28
// } else {
15
29
// res.status(404).json({
16
30
// message: 'No entries found'
@@ -36,8 +50,16 @@ router.post("/", (req, res, next) => {
36
50
. then ( result => {
37
51
console . log ( result ) ;
38
52
res . status ( 201 ) . json ( {
39
- message : "Handling POST requests to /products" ,
40
- createdProduct : result
53
+ message : "Created product successfully" ,
54
+ createdProduct : {
55
+ name : result . name ,
56
+ price : result . price ,
57
+ _id : result . _id ,
58
+ request : {
59
+ type : 'GET' ,
60
+ url : "http://localhost:3000/products/" + result . _id
61
+ }
62
+ }
41
63
} ) ;
42
64
} )
43
65
. catch ( err => {
@@ -51,11 +73,18 @@ router.post("/", (req, res, next) => {
51
73
router . get ( "/:productId" , ( req , res , next ) => {
52
74
const id = req . params . productId ;
53
75
Product . findById ( id )
76
+ . select ( 'name price _id' )
54
77
. exec ( )
55
78
. then ( doc => {
56
79
console . log ( "From database" , doc ) ;
57
80
if ( doc ) {
58
- res . status ( 200 ) . json ( doc ) ;
81
+ res . status ( 200 ) . json ( {
82
+ product : doc ,
83
+ request : {
84
+ type : 'GET' ,
85
+ url : 'http://localhost:3000/products'
86
+ }
87
+ } ) ;
59
88
} else {
60
89
res
61
90
. status ( 404 )
@@ -77,8 +106,13 @@ router.patch("/:productId", (req, res, next) => {
77
106
Product . update ( { _id : id } , { $set : updateOps } )
78
107
. exec ( )
79
108
. then ( result => {
80
- console . log ( result ) ;
81
- res . status ( 200 ) . json ( result ) ;
109
+ res . status ( 200 ) . json ( {
110
+ message : 'Product updated' ,
111
+ request : {
112
+ type : 'GET' ,
113
+ url : 'http://localhost:3000/products/' + id
114
+ }
115
+ } ) ;
82
116
} )
83
117
. catch ( err => {
84
118
console . log ( err ) ;
@@ -93,7 +127,14 @@ router.delete("/:productId", (req, res, next) => {
93
127
Product . remove ( { _id : id } )
94
128
. exec ( )
95
129
. then ( result => {
96
- res . status ( 200 ) . json ( result ) ;
130
+ res . status ( 200 ) . json ( {
131
+ message : 'Product deleted' ,
132
+ request : {
133
+ type : 'POST' ,
134
+ url : 'http://localhost:3000/products' ,
135
+ body : { name : 'String' , price : 'Number' }
136
+ }
137
+ } ) ;
97
138
} )
98
139
. catch ( err => {
99
140
console . log ( err ) ;
0 commit comments