-
Notifications
You must be signed in to change notification settings - Fork 33
/
delete.js
35 lines (28 loc) · 901 Bytes
/
delete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
const Joi = require('joi');
const Boom = require('@hapi/boom');
const Helpers = require('../helpers');
const Article = require('../../models/article');
module.exports = Helpers.withDefaults({
method: 'delete',
path: '/articles/{slug}',
options: {
validate: {
params: Joi.object({
slug: Article.field('slug')
})
},
auth: 'jwt',
handler: async (request, h) => {
const { slug } = request.params;
const { articleService } = request.services();
const currentUserId = Helpers.currentUserId(request);
const { id, authorId } = await articleService.findBySlug(slug);
if (authorId !== currentUserId) {
throw Boom.forbidden();
}
await articleService.delete(id);
return null;
}
}
});