From be4515c8288786fe65d25cd3b36ff2d38d66d44e Mon Sep 17 00:00:00 2001 From: Savy Date: Mon, 15 May 2023 21:33:04 +0530 Subject: [PATCH] Finished Ex 7.17 & 7.18 --- Part_4/blog-list/controllers/blogs.js | 13 +++++++++++++ Part_4/blog-list/models/blog.js | 5 ++++- Part_7/bloglist-frontend/src/components/BlogView.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Part_4/blog-list/controllers/blogs.js b/Part_4/blog-list/controllers/blogs.js index 1a162c1..93ad6c9 100644 --- a/Part_4/blog-list/controllers/blogs.js +++ b/Part_4/blog-list/controllers/blogs.js @@ -34,6 +34,19 @@ blogsRouter.post('/', userExtracter, async (request, response) => { response.status(201).json(savedBlog) }) +blogsRouter.post('/:id/comments', userExtracter, async (request, response) => { + const { comment } = request.body + const blogToAddComment = await Blog.findById(request.params.id) + + console.log(blogToAddComment) + blogToAddComment.comments = blogToAddComment.comments.concat(comment) + await blogToAddComment.save() + + console.log(blogToAddComment) + console.log(comment) + response.status(200).end() +}) + blogsRouter.delete('/:id', userExtracter, async (request, response) => { const blogToDel = await Blog.findById(request.params.id) diff --git a/Part_4/blog-list/models/blog.js b/Part_4/blog-list/models/blog.js index 94fc223..1a25e73 100644 --- a/Part_4/blog-list/models/blog.js +++ b/Part_4/blog-list/models/blog.js @@ -8,7 +8,10 @@ const blogSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' - } + }, + comments : [ + { type: String } + ] }) blogSchema.set('toJSON', { diff --git a/Part_7/bloglist-frontend/src/components/BlogView.js b/Part_7/bloglist-frontend/src/components/BlogView.js index 291fd97..dbec252 100644 --- a/Part_7/bloglist-frontend/src/components/BlogView.js +++ b/Part_7/bloglist-frontend/src/components/BlogView.js @@ -97,6 +97,16 @@ const BlogView = () => { {user.username === blog.user.username ? ( ) : null} +

Comments

+ ) } -- 2.51.2