Add DELETE /users/:id

This commit is contained in:
Adrian Obelmejias
2016-03-16 20:21:16 -04:00
parent 2d8e171d07
commit b08cef1ffc
2 changed files with 28 additions and 0 deletions
+19
View File
@@ -65,3 +65,22 @@ export async function updateUser(ctx) {
ctx.throw(500)
}
}
export async function deleteUser(ctx) {
try {
const user = await User.findById(ctx.params.id)
if (!user) {
ctx.throw(404)
}
await user.remove()
ctx.body = 200
} catch (err) {
if (err === 404 || err.name === 'CastError') {
ctx.throw(404)
}
ctx.throw(500)
}
}
+9
View File
@@ -37,5 +37,14 @@ export default {
ensureUser,
user.updateUser
]
},
'Delete user': {
method: 'DELETE',
route: '/:id',
handlers: [
ensureUser,
user.deleteUser
]
}
}