Skip to content

Commit d4b18b5

Browse files
author
Maximilian Schwarzmüller
committed
added products validation and better responses
1 parent d4aa37b commit d4b18b5

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

api/models/product.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const mongoose = require('mongoose');
22

33
const productSchema = mongoose.Schema({
44
_id: mongoose.Schema.Types.ObjectId,
5-
name: String,
6-
price: Number
5+
name: { type: String, required: true },
6+
price: { type: Number, required: true }
77
});
88

99
module.exports = mongoose.model('Product', productSchema);

api/routes/products.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@ const Product = require("../models/product");
66

77
router.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) => {
5173
router.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);

app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mongoose.connect(
1515
useMongoClient: true
1616
}
1717
);
18+
mongoose.Promise = global.Promise;
1819

1920
app.use(morgan("dev"));
2021
app.use(bodyParser.urlencoded({ extended: false }));

nodemon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"env": {
3-
"MONGO_ATLAS_PW": "node-shop"
3+
"MONGO_ATLAS_PW": "CTdYB4jqX6uovW7O"
44
}
55
}

0 commit comments

Comments
 (0)