-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblogRouter.js
More file actions
93 lines (84 loc) · 3.13 KB
/
blogRouter.js
File metadata and controls
93 lines (84 loc) · 3.13 KB
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
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
const {BlogPosts} = require('./models');
//next 2 lines creating a new post and loginin it ---- working
let newpost = BlogPosts.create("intro to Java", "Book", "John Smith");
console.log(newpost);
// when the root of this router is called with GET, return
// all current BlogPosts
router.get('/', (req, res) => {
console.log("Retreving Blog posts");
let blPost = BlogPosts.get();
if(blPost.length === 0){
console.log("No Blog posts were found");
}
res.json(BlogPosts.get());
});
// when a new blog is posted, make sure it's
// got required fields ('title, content, author). if not,
// log an error and return a 400 status code. if okay,
// add new item to BlogPosts and return it with a 201.
router.post('/', jsonParser, (req, res) => {
// ensure `title` , 'content and `author` 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);
});
// when DELETE request comes in with an id in path,
// try to delete that item from BlogPosts .
router.delete('/:id', (req, res) => {
let blPost = BlogPosts.get(req.params.id);
if(blPost){
BlogPosts.delete(req.params.id);
console.log(`Deleted BlogerPost item \`${req.params.ID}\``);
res.status(204).end();
}
else{
console.log("Error: Post not found");
res.status(500).end();
}
});
// when PUT request comes in with updated item, ensure has
// required fields. also ensure that item id in url path, and
// item id in updated item object match. if problems with any
// of that, log error and send back status code 400. otherwise
// call `BlogPosts .update` with updated item.
router.put('/:id', jsonParser, (req, res) => {
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);
}
}
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-posts item \`${req.params.id}\``);
const updatedItem = 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(); SAME error from yesterday status change too 200 plus end to json with parametr updated Item
res.status(200).json(updatedItem);
})
module.exports = router;