-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
171 lines (154 loc) · 4.38 KB
/
router.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const router = require('express').Router();
const Joi = require('joi');
const userModel = require('./db/models/user');
const repositoryModel = require('./db/models/repository');
const contributionModel = require('./db/models/contribution');
const auth = require('./authenticator');
const userSchema = Joi.object({
id: Joi.number().integer(),
login: Joi.string(),
avatar_url: Joi.string().uri(),
html_url: Joi.string().uri(),
});
const repositorySchema = Joi.object({
id: Joi.number().integer(),
full_name: Joi.string(),
stargazers_count: Joi.number().integer(),
html_url: Joi.string().uri({ scheme: 'https://github.com' }),
description: Joi.string(),
language: Joi.string(),
});
router.get('/hello', (req, res) => res.send('Hello World !'));
// Repository
router.get('/repository/:id', async (req, res, next) => {
const { id } = req.params;
try {
Joi.assert({ id }, userSchema);
} catch (error) {
error.statusCode = 403;
return next(error);
}
try {
const response = await repositoryModel.read({ id });
return res.json(response);
} catch (error) {
return next(error);
}
});
router.get('/repository', async (req, res, next) => {
const { value, error: validationError } = repositorySchema.validate(req.query);
if (validationError) {
validationError.statusCode = 403;
return next(validationError);
}
try {
const response = await repositoryModel.read(value);
return res.json(response);
} catch (error) {
return next(error);
}
});
router.post('/repository', auth, async (req, res, next) => {
const { value, error: validationError } = repositoryModel.schema.validate(req.body);
if (validationError) {
validationError.statusCode = 403;
return next(validationError);
}
try {
const response = await repositoryModel.insert(value);
return res.status(200).json(response);
} catch (error) {
return next(error);
}
});
// User
router.get('/users/:id', async (req, res, next) => {
const { id } = req.params;
try {
Joi.attempt(id, Joi.number().integer());
} catch (error) {
error.statusCode = 403;
return next(error);
}
try {
const response = await repositoryModel.read({ id });
return res.json(response);
} catch (error) {
return next(error);
}
});
router.get('/users', async (req, res, next) => {
const { value, error: validationError } = userSchema.validate(req.query);
if (validationError) {
validationError.statusCode = 403;
return next(validationError);
}
try {
const response = await userModel.read(value);
return res.json(response);
} catch (error) {
return next(error);
}
});
router.post('/user', auth, async (req, res, next) => {
const { value, error: validationError } = userModel.schema.validate(req.body);
if (validationError) {
validationError.statusCode = 403;
return next(validationError);
}
try {
await userModel.insert(value);
return res.status(200).send();
} catch (error) {
return next(error);
}
});
// Contribution
router.get('/contribution', async (req, res, next) => {
const user = { id: req.query.userID, login: req.query.login };
const repository = { id: req.query.repositoryID, full_name: req.query.full_name };
try {
Joi.attempt(user, userSchema);
Joi.attempt(repository, repositorySchema);
} catch (error) {
error.statusCode = 403;
return next(error);
}
try {
const response = await contributionModel.read({ user, repository });
return res.json(response);
} catch (error) {
return next(error);
}
});
router.post('/contribution', auth, async (req, res, next) => {
const { value, validationError } = contributionModel.schema.validate(req.body);
if (validationError) {
validationError.statusCode = 403;
return next(validationError);
}
try {
await contributionModel.insert({
user: value.user,
repository: value.repository,
line_count: value.lineCount,
});
return res.sendStatus(200);
} catch (error) {
return next(error);
}
});
router.put('/contribution', auth, async (req, res, next) => {
const { value, validationError } = contributionModel.schema.validate(req.body);
if (validationError) {
validationError.statusCode = 403;
return next(validationError);
}
try {
await contributionModel.insertOrReplace(value);
return res.sendStatus(200);
} catch (error) {
return next(error);
}
});
module.exports = router;