-
Notifications
You must be signed in to change notification settings - Fork 47
/
blogPostsRouter.js
99 lines (91 loc) · 3.56 KB
/
blogPostsRouter.js
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
const express = require("express");
const router = express.Router();
const { BlogPosts } = require("./models");
// convenience function for generating lorem text for blog
// posts we initially add below
function lorem() {
return (
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " +
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " +
"cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
);
}
// seed some posts so initial GET requests will return something
BlogPosts.create("10 things -- you won't believe #4", lorem(), "Billy Bob");
BlogPosts.create("Lions and tigers and bears oh my", lorem(), "Lefty Lil");
// add endpoint for GET. It should call `BlogPosts.get()`
// and return JSON objects of stored blog posts.
// send back JSON representation of all blog posts
// on GET requests to root
router.get("/", (req, res) => {
res.json(BlogPosts.get());
});
// add endpoint for POST requests, which should cause a new
// blog post to be added (using `BlogPosts.create()`). It should
// return a JSON object representing the new post (including
// the id, which `BlogPosts` will create. This endpoint should
// send a 400 error if the post doesn't contain
// `title`, `content`, and `author`
router.post("/", (req, res) => {
// ensure `name` and `budget` are in request body
const requiredFields = ["title", "content", "author"];
for (let i = 0; i < requiredFields.length; i++) {
const field = requiredFields[i];
if (!(field in req.body)) {
const message = `Missing \`${field}\` in request body`;
console.error(message);
return res.status(400).send(message);
}
}
const item = BlogPosts.create(
req.body.title,
req.body.content,
req.body.author
);
res.status(201).json(item);
});
// add endpoint for PUT requests to update blogposts. it should
// call `BlogPosts.update()` and return the updated post.
// it should also ensure that the id in the object representing
// the post matches the id of the path variable, and that the
// following required fields are in request body: `id`, `title`,
// `content`, `author`, `publishDate`
router.put("/:id", (req, res) => {
const requiredFields = ["id", "title", "content", "author", "publishDate"];
for (let i = 0; i < requiredFields.length; i++) {
const field = requiredFields[i];
if (!(field in req.body)) {
const message = `Missing \`${field}\` in request body`;
console.error(message);
return res.status(400).send(message);
}
}
if (req.params.id !== req.body.id) {
const message = `Request path id (${
req.params.id
}) and request body id ``(${req.body.id}) must match`;
console.error(message);
return res.status(400).send(message);
}
console.log(`Updating blog post with id \`${req.params.id}\``);
BlogPosts.update({
id: req.params.id,
title: req.body.title,
content: req.body.content,
author: req.body.author,
publishDate: req.body.publishDate
});
res.status(204).end();
});
// add endpoint for DELETE requests. These requests should
// have an id as a URL path variable and call
// `BlogPosts.delete()`
router.delete("/:id", (req, res) => {
BlogPosts.delete(req.params.id);
console.log(`Deleted blog post with id \`${req.params.ID}\``);
res.status(204).end();
});
module.exports = router;