From 65d347caf53c9bc31b2df3ac3e1ea916b3ea99b2 Mon Sep 17 00:00:00 2001 From: wentao <569845499@qq.com> Date: Sun, 10 Jan 2021 01:48:44 +0800 Subject: [PATCH] update in part 7 --- controllers/blogs.js | 37 +++++++++++++++++++++++-------- controllers/login.js | 2 +- models/blog.js | 3 ++- requests/create_user.rest | 8 +++++++ requests/get_all_blogs.rest | 2 +- requests/get_one_blog.rest | 1 + requests/post_one_blog.rest | 11 ++++----- requests/update_blog_comment.rest | 14 ++++++++++++ requests/update_one_blog.rest | 11 +++++++++ tests/blog_api.test.js | 2 ++ 10 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 requests/create_user.rest create mode 100644 requests/get_one_blog.rest create mode 100644 requests/update_blog_comment.rest create mode 100644 requests/update_one_blog.rest diff --git a/controllers/blogs.js b/controllers/blogs.js index 6f2d5b9..b8f8852 100644 --- a/controllers/blogs.js +++ b/controllers/blogs.js @@ -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 => { @@ -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); @@ -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(); @@ -50,11 +49,12 @@ 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; @@ -62,13 +62,32 @@ blogsRouter.put('/:id', async (req, res) => { 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; diff --git a/controllers/login.js b/controllers/login.js index 2bb1e64..3a7846f 100644 --- a/controllers/login.js +++ b/controllers/login.js @@ -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); diff --git a/models/blog.js b/models/blog.js index 2c04c02..64e527d 100644 --- a/models/blog.js +++ b/models/blog.js @@ -10,7 +10,8 @@ const blogSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' - } + }, + comments: [String] }); blogSchema.set('toJSON', { diff --git a/requests/create_user.rest b/requests/create_user.rest new file mode 100644 index 0000000..573d978 --- /dev/null +++ b/requests/create_user.rest @@ -0,0 +1,8 @@ +POST http://localhost:3003/api/users +Content-Type: application/json + +{ + "username": "ywt", + "name": "ywt", + "password": "123" +} \ No newline at end of file diff --git a/requests/get_all_blogs.rest b/requests/get_all_blogs.rest index 0b95fe8..7d1ed5f 100644 --- a/requests/get_all_blogs.rest +++ b/requests/get_all_blogs.rest @@ -1 +1 @@ -GET http://localhost:1333/api/blogs \ No newline at end of file +GET http://localhost:3003/api/blogs \ No newline at end of file diff --git a/requests/get_one_blog.rest b/requests/get_one_blog.rest new file mode 100644 index 0000000..1be2375 --- /dev/null +++ b/requests/get_one_blog.rest @@ -0,0 +1 @@ +GET http://localhost:3003/api/blogs/5f64fa477bb83426602f84f9 \ No newline at end of file diff --git a/requests/post_one_blog.rest b/requests/post_one_blog.rest index eb08025..eceb145 100644 --- a/requests/post_one_blog.rest +++ b/requests/post_one_blog.rest @@ -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":[] } \ No newline at end of file diff --git a/requests/update_blog_comment.rest b/requests/update_blog_comment.rest new file mode 100644 index 0000000..4c26d38 --- /dev/null +++ b/requests/update_blog_comment.rest @@ -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" +} \ No newline at end of file diff --git a/requests/update_one_blog.rest b/requests/update_one_blog.rest new file mode 100644 index 0000000..06e7ed1 --- /dev/null +++ b/requests/update_one_blog.rest @@ -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": [] +} \ No newline at end of file diff --git a/tests/blog_api.test.js b/tests/blog_api.test.js index 81684fd..9f591a2 100644 --- a/tests/blog_api.test.js +++ b/tests/blog_api.test.js @@ -118,6 +118,8 @@ describe('删除和更新', () => { }); + + afterAll(done => { mongoose.connection.close(); done();