@@ -6,11 +6,25 @@ const Product = require("../models/product");
66
77router . get ( "/" , ( req , res , next ) => {
88 Product . find ( )
9+ . select ( "name price _id" )
910 . exec ( )
1011 . 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+ } ;
1226 // if (docs.length >= 0) {
13- res . status ( 200 ) . json ( docs ) ;
27+ res . status ( 200 ) . json ( response ) ;
1428 // } else {
1529 // res.status(404).json({
1630 // message: 'No entries found'
@@ -36,8 +50,16 @@ router.post("/", (req, res, next) => {
3650 . then ( result => {
3751 console . log ( result ) ;
3852 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+ }
4163 } ) ;
4264 } )
4365 . catch ( err => {
@@ -51,11 +73,18 @@ router.post("/", (req, res, next) => {
5173router . get ( "/:productId" , ( req , res , next ) => {
5274 const id = req . params . productId ;
5375 Product . findById ( id )
76+ . select ( 'name price _id' )
5477 . exec ( )
5578 . then ( doc => {
5679 console . log ( "From database" , doc ) ;
5780 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+ } ) ;
5988 } else {
6089 res
6190 . status ( 404 )
@@ -77,8 +106,13 @@ router.patch("/:productId", (req, res, next) => {
77106 Product . update ( { _id : id } , { $set : updateOps } )
78107 . exec ( )
79108 . 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+ } ) ;
82116 } )
83117 . catch ( err => {
84118 console . log ( err ) ;
@@ -93,7 +127,14 @@ router.delete("/:productId", (req, res, next) => {
93127 Product . remove ( { _id : id } )
94128 . exec ( )
95129 . 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+ } ) ;
97138 } )
98139 . catch ( err => {
99140 console . log ( err ) ;
0 commit comments