Refactor Games structure on server: no longer use an extra 'Players' table
[vchess.git] / server / routes / problems.js
CommitLineData
89021f18
BA
1let router = require("express").Router();
2const access = require("../utils/access");
3const ProblemModel = require("../models/Problem");
4const sanitizeHtml = require('sanitize-html');
5
866842c3 6router.post("/problems", access.logged, access.ajax, (req,res) => {
0234201f
BA
7 if (ProblemModel.checkProblem(req.body.prob)) {
8 const problem = {
866842c3
BA
9 vid: req.body.prob.vid,
10 fen: req.body.prob.fen,
11 uid: req.userId,
12 instruction: sanitizeHtml(req.body.prob.instruction),
13 solution: sanitizeHtml(req.body.prob.solution),
14 };
0234201f
BA
15 ProblemModel.create(problem, (err, ret) => {
16 res.json(err || ret);
866842c3
BA
17 });
18 }
19 else
20 res.json({});
21});
22
e57c4de4 23router.get("/problems", access.ajax, (req,res) => {
89021f18 24 const probId = req.query["pid"];
0234201f 25 if (probId && probId.match(/^[0-9]+$/)) {
89021f18 26 ProblemModel.getOne(req.query["pid"], (err,problem) => {
866842c3 27 res.json(err || {problem: problem});
89021f18 28 });
0234201f 29 } else {
89021f18 30 ProblemModel.getAll((err,problems) => {
0234201f 31 res.json(err || { problems: problems });
89021f18
BA
32 });
33 }
34});
35
89021f18 36router.put("/problems", access.logged, access.ajax, (req,res) => {
604b951e 37 let obj = req.body.prob;
0234201f 38 if (ProblemModel.checkProblem(obj)) {
866842c3
BA
39 obj.instruction = sanitizeHtml(obj.instruction);
40 obj.solution = sanitizeHtml(obj.solution);
41 ProblemModel.safeUpdate(obj, req.userId);
42 }
43 res.json({});
89021f18
BA
44});
45
46router.delete("/problems", access.logged, access.ajax, (req,res) => {
47 const pid = req.query.id;
866842c3
BA
48 if (pid.toString().match(/^[0-9]+$/))
49 ProblemModel.safeRemove(pid, req.userId);
50 res.json({});
89021f18
BA
51});
52
53module.exports = router;