-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArticleController.js
97 lines (71 loc) · 3 KB
/
ArticleController.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const { ArticleService } = require('../services')
const { errorWrapper, errorGenerator } = require('../errors')
const { validateFields } = require('../utils')
const getArticles = errorWrapper(async (req, res) => {
const articles = await ArticleService.findArticles(req.query)
res.status(200).json({ articles })
})
const getOneArticle = errorWrapper(async (req, res) => {
const { articleId } = req.params
const article = await ArticleService.findArticle({ id: articleId })
if (article.deleted_at) return res.status(200).json({ message: 'deleted ' })
res.status(200).json({ article })
})
const postOneArticle = errorWrapper(async (req, res) => {
const { id: userId } = req.foundUser
const { title, body } = req.body
if (!title || !body) errorGenerator({ statusCode: 400, message: 'invalid key error' })
const createdArticle = await ArticleService.createArticle({
userId,
title,
body,
})
res.status(201).json({ createdArticle })
})
const updateOneArticle = errorWrapper(async (req, res) => {
const { id: userIdFromToken } = req.foundUser
const { articleId } = req.params
const requestedFields = req.body
const allowedFields = ['title', 'body']
const isValidFields = validateFields(requestedFields, allowedFields)
if (!isValidFields) errorGenerator({ statusCode: 400, message: 'invalid requested fields' })
const foundArticle = await ArticleService.findArticle({ id: articleId })
const { user_id: userIdFromArticle } = foundArticle
if (userIdFromToken !== userIdFromArticle)
errorGenerator({ statusCode: 403, message: 'unauthorized' })
const updatedArticle = await ArticleService.updateArticle({
articleId,
requestedFields,
})
res.status(201).json({ updatedArticle })
})
const publishOneArticle = errorWrapper(async (req, res) => {
const { id: userIdFromToken } = req.foundUser
const { articleId } = req.params
const foundArticle = await ArticleService.findArticle({ id: articleId })
if (!foundArticle) errorGenerator({ statusCode: 404, message: 'article not found' })
const { user_id: userIdFromArticle } = foundArticle
if (userIdFromToken !== userIdFromArticle)
errorGenerator({ statusCode: 403, message: 'unauthorized' })
const publishedArticle = await ArticleService.publishArticle(articleId)
res.status(201).json({ publishedArticle })
})
const deleteOneArticle = errorWrapper(async (req, res) => {
const { id: userIdFromToken } = req.foundUser
const { articleId } = req.params
const foundArticle = await ArticleService.findArticle({ id: articleId })
if (!foundArticle) errorGenerator({ statusCode: 404, message: 'article not found' })
const { user_id: userIdFromArticle } = foundArticle
if (userIdFromToken !== userIdFromArticle)
errorGenerator({ statusCode: 403, message: 'unauthorized' })
const deletedArticle = await ArticleService.deleteArticle(articleId)
res.status(201).json({ deletedArticle })
})
module.exports = {
getArticles,
getOneArticle,
postOneArticle,
updateOneArticle,
publishOneArticle,
deleteOneArticle,
}