Skip to content

Commit

Permalink
solution 4.13 Blog list expansions, step1
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 10, 2023
1 parent bc0c311 commit d6c0565
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions controllers/blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ blogsRouter.get('/', async (request, response) => {
response.json(blogs)
})

blogsRouter.delete('/:id', async (request, response) => {
await Blog.findByIdAndRemove(request.params.id)
response.status(204).end()
})

blogsRouter.post('/', async (request, response) => {
request.body.likes = request.body.likes || 0
if (!request.body.title || !request.body.url) {
Expand Down
5 changes: 5 additions & 0 deletions requests/delete_note.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DELETE http://localhost:3003/api/blogs/64fdd3005e8ccd4fa2a5ac24

###

GET http://localhost:3003/api/blogs/
23 changes: 13 additions & 10 deletions tests/blog_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,22 @@ test('unknown endpoint in api url', async () => {
expect(response.body.error).toBe('unknown endpoint')
})

// test('blog without title is not added', async () => {
// const newBlog = {
// author: 'A. Pacheco',
// url: 'https://unlibroenmimochila.blogspot.com/2017/12/fugas-o-la-ansiedad-de-sentirse-vivo.html',
// likes: 4,
// }
describe('deletion of a blog', () => {
test('succeeds with status code 204 if id is valid', async () => {
const blogsAtStart = await helper.blogsInDb()
const blogToDelete = blogsAtStart[0]

// await api.post('/api/blogs').send(newBlog).expect(400)
await api.delete(`/api/blogs/${blogToDelete.id}`).expect(204)

// const response = await api.get('/api/blogs')
const blogsAtEnd = await helper.blogsInDb()

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

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

// expect(response.body).toHaveLength(initialBlogs.length)
// })
expect(contents).not.toContain(blogToDelete.title)
})
})

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

0 comments on commit d6c0565

Please sign in to comment.