Upload 4.14

This commit is contained in:
Andrew Trieu
2023-05-31 21:27:58 +03:00
parent 0988a44fe4
commit 4b88f3958f
3 changed files with 54 additions and 0 deletions

View File

@@ -31,4 +31,17 @@ blogsRouter.delete('/:id', async (request, response) => {
response.status(204).end()
})
blogsRouter.put('/:id', async (request, response) => {
const body = request.body
const blog = await Blog.findByIdAndUpdate(request.params.id, {
title: body.title,
author: body.author,
url: body.url,
likes: body.likes
}, { new: true })
response.status(200).json(blog)
})
module.exports = blogsRouter

View File

@@ -0,0 +1,7 @@
PUT http://localhost:3001/api/blogs/6477906c6320872422793b7a
content-type: application/json
{
"title": "Updated blog", "author": "Hans", "url": "www.andrew.eu", "likes": 5000
}

View File

@@ -84,6 +84,40 @@ describe('adding blog', () => {
})
})
describe('deleting blog', () => {
test('a blog can be deleted', async () => {
const blogsAtStart = await helper.blogsInDb()
const blogToDelete = blogsAtStart[0]
await api.delete(`/api/blogs/${blogToDelete.id}`).expect(204)
const blogsAtEnd = await helper.blogsInDb()
expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length - 1)
const contents = blogsAtEnd.map((r) => r.title)
expect(contents).not.toContain(blogToDelete.title)
})
})
describe('updating blog', () => {
test('a blog can be updated', async () => {
const blogsAtStart = await helper.blogsInDb()
const blogToUpdate = blogsAtStart[0]
const newBlog = {
title: 'Updated blog',
author: blogToUpdate.author,
url: blogToUpdate.url,
likes: blogToUpdate.likes
}
await api.put(`/api/blogs/${blogToUpdate.id}`).send(newBlog).expect(200)
const blogsAtEnd = await helper.blogsInDb()
expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length)
const contents = blogsAtEnd.map((r) => r.title)
expect(contents).toContain('Updated blog')
})
})
describe('blog id check', () => {
test('blog id is defined', async () => {