Commit | Line | Data |
---|---|---|
89021f18 BA |
1 | let router = require("express").Router(); |
2 | const access = require("../utils/access"); | |
a9e79351 | 3 | const params = require("../config/parameters"); |
89021f18 BA |
4 | const ProblemModel = require("../models/Problem"); |
5 | const sanitizeHtml = require('sanitize-html'); | |
6 | ||
866842c3 | 7 | router.post("/problems", access.logged, access.ajax, (req,res) => { |
0234201f BA |
8 | if (ProblemModel.checkProblem(req.body.prob)) { |
9 | const problem = { | |
866842c3 BA |
10 | vid: req.body.prob.vid, |
11 | fen: req.body.prob.fen, | |
12 | uid: req.userId, | |
13 | instruction: sanitizeHtml(req.body.prob.instruction), | |
14 | solution: sanitizeHtml(req.body.prob.solution), | |
15 | }; | |
0234201f BA |
16 | ProblemModel.create(problem, (err, ret) => { |
17 | res.json(err || ret); | |
866842c3 BA |
18 | }); |
19 | } | |
20 | else | |
21 | res.json({}); | |
22 | }); | |
23 | ||
e57c4de4 | 24 | router.get("/problems", access.ajax, (req,res) => { |
84fc0f02 | 25 | const probId = req.query["id"]; |
68e19a44 BA |
26 | const cursor = req.query["cursor"]; |
27 | if (!!probId && !!probId.match(/^[0-9]+$/)) { | |
84fc0f02 | 28 | ProblemModel.getOne(probId, (err, problem) => { |
866842c3 | 29 | res.json(err || {problem: problem}); |
89021f18 | 30 | }); |
68e19a44 | 31 | } else if (!!cursor && !!cursor.match(/^[0-9]+$/)) { |
84fc0f02 BA |
32 | const onlyMine = (req.query["mode"] == "mine"); |
33 | const uid = parseInt(req.query["uid"]); | |
34 | ProblemModel.getNext(uid, onlyMine, cursor, (err, problems) => { | |
0234201f | 35 | res.json(err || { problems: problems }); |
89021f18 BA |
36 | }); |
37 | } | |
38 | }); | |
39 | ||
89021f18 | 40 | router.put("/problems", access.logged, access.ajax, (req,res) => { |
604b951e | 41 | let obj = req.body.prob; |
0234201f | 42 | if (ProblemModel.checkProblem(obj)) { |
866842c3 BA |
43 | obj.instruction = sanitizeHtml(obj.instruction); |
44 | obj.solution = sanitizeHtml(obj.solution); | |
a9e79351 | 45 | ProblemModel.safeUpdate(obj, req.userId, params.devs); |
866842c3 BA |
46 | } |
47 | res.json({}); | |
89021f18 BA |
48 | }); |
49 | ||
50 | router.delete("/problems", access.logged, access.ajax, (req,res) => { | |
51 | const pid = req.query.id; | |
866842c3 | 52 | if (pid.toString().match(/^[0-9]+$/)) |
a9e79351 | 53 | ProblemModel.safeRemove(pid, req.userId, params.devs); |
866842c3 | 54 | res.json({}); |
89021f18 BA |
55 | }); |
56 | ||
57 | module.exports = router; |