746be9aac124faf108ec3b7d5328de901254b701
1 let router
= require("express").Router();
2 const access
= require("../utils/access");
3 const params
= require("../config/parameters");
4 const ProblemModel
= require("../models/Problem");
5 const sanitizeHtml
= require('sanitize-html');
7 router
.post("/problems", access
.logged
, access
.ajax
, (req
,res
) => {
8 if (ProblemModel
.checkProblem(req
.body
.prob
)) {
10 vid: req
.body
.prob
.vid
,
11 fen: req
.body
.prob
.fen
,
13 instruction: sanitizeHtml(req
.body
.prob
.instruction
),
14 solution: sanitizeHtml(req
.body
.prob
.solution
),
16 ProblemModel
.create(problem
, (err
, ret
) => {
24 router
.get("/problems", access
.ajax
, (req
,res
) => {
25 const probId
= req
.query
["id"];
26 const cursor
= req
.query
["cursor"];
27 if (!!probId
&& !!probId
.match(/^[0-9]+$/)) {
28 ProblemModel
.getOne(probId
, (err
, problem
) => {
29 res
.json(err
|| {problem: problem
});
31 } else if (!!cursor
&& !!cursor
.match(/^[0-9]+$/)) {
32 const onlyMine
= (req
.query
["mode"] == "mine");
33 const uid
= parseInt(req
.query
["uid"]);
34 ProblemModel
.getNext(uid
, onlyMine
, cursor
, (err
, problems
) => {
35 res
.json(err
|| { problems: problems
});
40 router
.put("/problems", access
.logged
, access
.ajax
, (req
,res
) => {
41 let obj
= req
.body
.prob
;
42 if (ProblemModel
.checkProblem(obj
)) {
43 obj
.instruction
= sanitizeHtml(obj
.instruction
);
44 obj
.solution
= sanitizeHtml(obj
.solution
);
45 ProblemModel
.safeUpdate(obj
, req
.userId
, params
.devs
);
50 router
.delete("/problems", access
.logged
, access
.ajax
, (req
,res
) => {
51 const pid
= req
.query
.id
;
52 if (pid
.toString().match(/^[0-9]+$/))
53 ProblemModel
.safeRemove(pid
, req
.userId
, params
.devs
);
57 module
.exports
= router
;