Skip to content

Commit

Permalink
4.14 Blog list expansions, step2
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 10, 2023
1 parent e7c513d commit 6ac521f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
16 changes: 16 additions & 0 deletions controllers/blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ blogsRouter.post('/', async (request, response) => {
}
})

blogsRouter.put('/:id', async (request, response) => {
const body = request.body

const blog = await Blog.findById(request.params.id)
if (blog) {
for (const key in body) {
blog[key] = body[key]
}
// console.log(blog)
const updatedBlog = await Blog.findByIdAndUpdate(request.params.id, blog, {
new: true,
})
response.status(200).json(updatedBlog)
}
})

module.exports = blogsRouter
9 changes: 9 additions & 0 deletions requests/update_blog_entry.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PUT http://localhost:3003/api/blogs/64fdc53e107f846d39f665a1
Content-Type: application/json

{
"likes": 5
}

###
GET http://localhost:3003/api/blogs/
57 changes: 50 additions & 7 deletions tests/blog_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ describe('viewing a specific blog', () => {
})
})

test('unknown endpoint in api url', async () => {
const response = await api.get('/api/blogs-url-dont-exist')
// console.log(response.body)

expect(response.body.error).toBe('unknown endpoint')
})

describe('deletion of a blog', () => {
test('succeeds with status code 204 if id is valid', async () => {
const blogsAtStart = await helper.blogsInDb()
Expand All @@ -144,6 +137,56 @@ describe('deletion of a blog', () => {
})
})

describe('updating of a blog entry', () => {
test('succeeds with status code 200 with likes update', async () => {
const blogsAtStart = await helper.blogsInDb()
let blogToUpdate = blogsAtStart[0]
blogToUpdate.likes = 100

await api
.put(`/api/blogs/${blogToUpdate.id}`)
.send(blogToUpdate)
.expect(200)

const blogsAtEnd = await helper.blogsInDb()

expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length)

const contents = blogsAtEnd.map((r) => r.likes)

expect(contents).toContain(blogToUpdate.likes)
})

test('succeeds with status code 200 title and url update', async () => {
const blogsAtStart = await helper.blogsInDb()
let blogToUpdate = blogsAtStart[0]
blogToUpdate.title = 'test title'
blogToUpdate.url = 'test url'

await api
.put(`/api/blogs/${blogToUpdate.id}`)
.send(blogToUpdate)
.expect(200)

const blogsAtEnd = await helper.blogsInDb()

expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length)

const contents = blogsAtEnd.map((r) => r.title)
expect(contents).toContain(blogToUpdate.title)

const contents2 = blogsAtEnd.map((r) => r.url)
expect(contents2).toContain(blogToUpdate.url)
})
})

test('unknown endpoint in api url', async () => {
const response = await api.get('/api/blogs-url-dont-exist')
// console.log(response.body)

expect(response.body.error).toBe('unknown endpoint')
})

afterAll(async () => {
await mongoose.connection.close()
})

0 comments on commit 6ac521f

Please sign in to comment.