forked from syzoj/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtodo_list.js
49 lines (43 loc) · 1.21 KB
/
todo_list.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
const User = syzoj.model('user');
const Problem = syzoj.model('problem');
const TodoList = syzoj.model('todo-list');
app.post('/api/todo_list/:action/:problem_id', async (req, res) => {
try {
if (!res.locals.user) throw new ErrorMessage("请先登录");
let problem_id = parseInt(req.params.problem_id);
if (!Number.isSafeInteger(problem_id)) throw new ErrorMessage("你确定这是个题目编号?");
let data = {
user_id: res.locals.user.id,
problem_id: problem_id
};
switch (req.params.action) {
case 'add': {
let count = await Problem.count({ where: { id: problem_id } });
if (!count) throw new ErrorMessage("题目不存在");
let item = await TodoList.findOne(data);
if (!item) {
item = await TodoList.create(data);
await item.save();
}
break;
}
case 'remove': {
let item = await TodoList.findOne(data);
if (item) {
await item.destroy();
}
break;
}
default:
throw new ErrorMessage("参数错误");
}
res.send({
error: null
});
} catch (e) {
syzoj.log(e);
res.send({
error: e.message
});
}
});