1 let router
= require("express").Router();
2 const access
= require("../utils/access");
3 const ProblemModel
= require("../models/Problem");
4 const sanitizeHtml
= require('sanitize-html');
6 router
.post("/problems", access
.logged
, access
.ajax
, (req
,res
) => {
7 if (ProblemModel
.checkProblem(req
.body
.prob
))
11 vid: req
.body
.prob
.vid
,
12 fen: req
.body
.prob
.fen
,
14 instruction: sanitizeHtml(req
.body
.prob
.instruction
),
15 solution: sanitizeHtml(req
.body
.prob
.solution
),
17 ProblemModel
.create(problem
, (err
,ret
) => {
18 res
.json(err
|| {id:ret
.pid
});
25 router
.get("/problems", (req
,res
) => {
26 const probId
= req
.query
["pid"];
27 if (probId
&& probId
.match(/^[0-9]+$/))
29 ProblemModel
.getOne(req
.query
["pid"], (err
,problem
) => {
30 res
.json(err
|| {problem: problem
});
35 ProblemModel
.getAll((err
,problems
) => {
36 res
.json(err
|| {problems:problems
});
41 router
.put("/problems", access
.logged
, access
.ajax
, (req
,res
) => {
42 let obj
= req
.body
.prob
;
43 if (ProblemModel
.checkProblem(obj
))
45 obj
.instruction
= sanitizeHtml(obj
.instruction
);
46 obj
.solution
= sanitizeHtml(obj
.solution
);
47 ProblemModel
.safeUpdate(obj
, req
.userId
);
52 router
.delete("/problems", access
.logged
, access
.ajax
, (req
,res
) => {
53 const pid
= req
.query
.id
;
54 if (pid
.toString().match(/^[0-9]+$/))
55 ProblemModel
.safeRemove(pid
, req
.userId
);
59 module
.exports
= router
;