forked from salihozdemir/stackoverflow-clone
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutes.js
95 lines (83 loc) · 3.05 KB
/
routes.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
const {
validateUser,
signup,
authenticate,
googlelogin,
listUsers,
search,
find
} = require('./controllers/users');
const {
loadQuestions,
questionValidate,
createQuestion,
show,
listQuestions,
listByTags,
listByUser,
removeQuestion,
editQuestion
} = require('./controllers/questions');
const {
loadAnswers,
answerValidate,
createAnswer,
removeAnswer
} = require('./controllers/answers');
const { listPopulerTags, searchTags, listTags } = require('./controllers/tags');
const { upvote, downvote, unvote } = require('./controllers/votes');
const { loadComments, validate, createComment, removeComment, editComment } = require('./controllers/comments');
const requireAuth = require('./middlewares/requireAuth');
const questionAuth = require('./middlewares/questionAuth');
const commentAuth = require('./middlewares/commentAuth');
const answerAuth = require('./middlewares/answerAuth');
const router = require('express').Router();
//authentication
router.post('/signup', validateUser, signup);
router.post('/authenticate', validateUser, authenticate);
router.post('/googlelogin', googlelogin);
//users
router.get('/users', listUsers);
router.get('/users/:search', search);
router.get('/user/:username', find);
//questions
router.param('question', loadQuestions);
router.post('/questions', [requireAuth, questionValidate], createQuestion);
router.get('/question/:question', show);
router.get('/question', listQuestions);
router.get('/questions/:tags', listByTags);
router.get('/question/user/:username', listByUser);
router.delete('/question/:question', [requireAuth, questionAuth], removeQuestion);
router.put('/question/:question', [requireAuth, questionAuth, questionValidate], editQuestion);
//tags
router.get('/tags/populertags', listPopulerTags);
router.get('/tags/:tag', searchTags);
router.get('/tags', listTags);
//answers
router.param('answer', loadAnswers);
router.post('/answer/:question', [requireAuth, answerValidate], createAnswer);
router.delete('/answer/:question/:answer', [requireAuth, answerAuth], removeAnswer);
//votes
router.get('/votes/upvote/:question/:answer?', requireAuth, upvote);
router.get('/votes/downvote/:question/:answer?', requireAuth, downvote);
router.get('/votes/unvote/:question/:answer?', requireAuth, unvote);
//comments
router.param('comment', loadComments);
router.post('/comment/:question/:answer?', [requireAuth, validate], createComment);
router.delete('/comment/:question/:comment', [requireAuth, commentAuth], removeComment);
router.delete('/comment/:question/:answer/:comment', [requireAuth, commentAuth], removeComment);
router.put('/comment/:question/:comment', [requireAuth, commentAuth, validate], editComment);
router.put('/comment/:question/:answer/:comment', [requireAuth, commentAuth, validate], editComment);
module.exports = (app) => {
app.use('/api', router);
app.use((req, res, next) => {
const error = new Error('Not found');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500).json({
message: error.message
});
});
};