Skip to content

Commit

Permalink
update in part 7
Browse files Browse the repository at this point in the history
  • Loading branch information
went2 committed Jan 9, 2021
1 parent aa06235 commit 65d347c
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 17 deletions.
37 changes: 28 additions & 9 deletions controllers/blogs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const blogsRouter = require('express').Router();
const Blog = require('../models/blog');
const User = require('../models/user');
const logger = require('../utils/logger');
const jwt = require('jsonwebtoken');

const getTokenFrom = req => {
Expand All @@ -12,14 +11,13 @@ const getTokenFrom = req => {
return null;
};

blogsRouter.get('/', async (req, res) => {
logger.info('fetching data from MongoDB...');

blogsRouter.get('/', async(req, res) => {
// logger.info('fetching data from MongoDB...');
const blogs = await Blog.find({}).populate('user', { username: 1, name: 1});
res.json(blogs);
});

blogsRouter.post('/', async (req, res) => {
blogsRouter.post('/', async(req, res) => {
const body = req.body
const token = getTokenFrom(req);
const decodedToken = jwt.verify(token, process.env.SECRET);
Expand All @@ -40,7 +38,8 @@ blogsRouter.post('/', async (req, res) => {
author: body.author,
url: body.url,
likes: body.hasOwnProperty('likes') ? body.likes : 0,
user: user._id
user: user._id,
comments: []
});

const savedBlog = await blog.save();
Expand All @@ -50,25 +49,45 @@ blogsRouter.post('/', async (req, res) => {
res.status(201).json(savedBlog);
});

blogsRouter.delete('/:id', async (req, res) => {
blogsRouter.delete('/:id', async(req, res) => {
await Blog.findByIdAndDelete(req.params.id);
res.status(204).end();
});

// 更新一条blog
blogsRouter.put('/:id', async (req, res) => {
const body = req.body;

const newBlog = {
title: body.title,
author: body.author,
url: body.url,
likes: body.likes
likes: body.likes,
comments: body.comments
};

const updatedBlog = await Blog.findByIdAndUpdate(req.params.id, newBlog, { new: true });
const updatedBlog = await Blog.findByIdAndUpdate(req.params.id, newBlog, { new: true }).populate('user', { username: 1, name: 1});

// console.log('服务器返回的更新后的对象是', updatedBlog);
res.json(updatedBlog);
});

// 新增一条评论:在当前的评论数组中push一条用户输入的评论
blogsRouter.put('/:id/comments', async (req, res) => {
const body = req.body;
const newComment = [...body.blog.comments, body.comment];

const newBlog = {
title: body.blog.title,
author: body.blog.author,
url: body.blog.url,
likes: body.blog.likes,
comments: newComment
};

const updatedBlog = await Blog.findByIdAndUpdate(req.params.id, newBlog, {new: true}).populate('user', {username: 1, name: 1});

res.json(updatedBlog);
});

module.exports = blogsRouter;
2 changes: 1 addition & 1 deletion controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ loginRouter.post('/', async (req, res) => {
const userForToken = {
username: user.username,
id: user._id
}
};

const token = jwt.sign(userForToken, process.env.SECRET);

Expand Down
3 changes: 2 additions & 1 deletion models/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const blogSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
},
comments: [String]
});

blogSchema.set('toJSON', {
Expand Down
8 changes: 8 additions & 0 deletions requests/create_user.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POST http://localhost:3003/api/users
Content-Type: application/json

{
"username": "ywt",
"name": "ywt",
"password": "123"
}
2 changes: 1 addition & 1 deletion requests/get_all_blogs.rest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GET http://localhost:1333/api/blogs
GET http://localhost:3003/api/blogs
1 change: 1 addition & 0 deletions requests/get_one_blog.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:3003/api/blogs/5f64fa477bb83426602f84f9
11 changes: 6 additions & 5 deletions requests/post_one_blog.rest
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
POST http://localhost:1333/api/blogs
POST http://localhost:3003/api/blogs
Content-Type: application/json
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOiI1ZjU3NDU2ZWUzODRhYTdlYjI4ZDczZjAiLCJpYXQiOjE1OTk1NzIxOTl9.Z7Hq9tBt10ALXTwkWUYr1DhXQhPKhSc2O6899Yx1ur8

{
"title": "How to create your first app using React part 2",
"author": "James Fisher",
"url": "www.google.com",
"likes": 4
"title": "test title1218",
"author": "James Fisher1218",
"url": "www.tommy.com",
"likes": 5,
"comments":[]
}
14 changes: 14 additions & 0 deletions requests/update_blog_comment.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
PUT http://localhost:3003/api/blogs/5f64fa477bb83426602f84f9/comments
Content-Type: application/json
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOiI1ZjU3NDU2ZWUzODRhYTdlYjI4ZDczZjAiLCJpYXQiOjE1OTk1NzIxOTl9.Z7Hq9tBt10ALXTwkWUYr1DhXQhPKhSc2O6899Yx1ur8

{
"blog": {
"title": "编程珠玑",
"author": "Jon Bentley",
"url": "https://book.douban.com/subject/3227098/",
"likes": 10,
"comments": []
},
"comment": "very good"
}
11 changes: 11 additions & 0 deletions requests/update_one_blog.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PUT http://localhost:3003/api/blogs/5f64fa477bb83426602f84f9
Content-Type: application/json
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOiI1ZjU3NDU2ZWUzODRhYTdlYjI4ZDczZjAiLCJpYXQiOjE1OTk1NzIxOTl9.Z7Hq9tBt10ALXTwkWUYr1DhXQhPKhSc2O6899Yx1ur8

{
"title": "编程珠玑",
"author": "Jon Bentley",
"url": "https://book.douban.com/subject/3227098/",
"likes": 10,
"comments": []
}
2 changes: 2 additions & 0 deletions tests/blog_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ describe('删除和更新', () => {
});




afterAll(done => {
mongoose.connection.close();
done();
Expand Down

0 comments on commit 65d347c

Please sign in to comment.