X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Froutes%2Fproblems.js;h=6cebb8f0ebee43805e466f8d34e5d0b289a5a36d;hp=c45a1baccd43f0aa5925d4bfb8c65393d76d382d;hb=0234201fb338fc239d6f613c677fa932c7c3697c;hpb=89021f181ac0689bbc785ce0ebd9a910e66352b0 diff --git a/server/routes/problems.js b/server/routes/problems.js index c45a1bac..6cebb8f0 100644 --- a/server/routes/problems.js +++ b/server/routes/problems.js @@ -1,70 +1,53 @@ -// AJAX methods to get, create, update or delete a problem - let router = require("express").Router(); const access = require("../utils/access"); const ProblemModel = require("../models/Problem"); const sanitizeHtml = require('sanitize-html'); -router.get("/problems", (req,res) => { - const probId = req.query["pid"]; - if (!!probId) - { - if (!probId.match(/^[0-9]+$/)) - return res.json({errmsg: "Wrong problem ID"}); - ProblemModel.getOne(req.query["pid"], (err,problem) => { - access.checkRequest(res, err, problem, "Problem not found", () => { - res.json({problem: problem}); - }); +router.post("/problems", access.logged, access.ajax, (req,res) => { + if (ProblemModel.checkProblem(req.body.prob)) { + const problem = { + vid: req.body.prob.vid, + fen: req.body.prob.fen, + uid: req.userId, + instruction: sanitizeHtml(req.body.prob.instruction), + solution: sanitizeHtml(req.body.prob.solution), + }; + ProblemModel.create(problem, (err, ret) => { + res.json(err || ret); }); } else - { + res.json({}); +}); + +router.get("/problems", access.ajax, (req,res) => { + const probId = req.query["pid"]; + if (probId && probId.match(/^[0-9]+$/)) { + ProblemModel.getOne(req.query["pid"], (err,problem) => { + res.json(err || {problem: problem}); + }); + } else { ProblemModel.getAll((err,problems) => { - res.json(err || {problems:problems}); + res.json(err || { problems: problems }); }); } }); -router.post("/problems", access.logged, access.ajax, (req,res) => { - const error = ProblemModel.checkProblem(req.body.prob); - if (!!error) - return res.json({errmsg:error}); - const problem = - { - vid: req.body.prob.vid, - fen: req.body.prob.fen, - uid: req.userId, - instruction: sanitizeHtml(req.body.prob.instruction), - solution: sanitizeHtml(req.body.prob.solution), - }; - ProblemModel.create(problem, (err,ret) => { - return res.json(err || {pid:ret.pid}); - }); -}); - router.put("/problems", access.logged, access.ajax, (req,res) => { - const pid = req.body.pid; - let error = ""; - if (!pid.toString().match(/^[0-9]+$/)) - error = "Wrong problem ID"; - let obj = req.body.newProb; - error = ProblemModel.checkProblem(obj); - obj.instruction = sanitizeHtml(obj.instruction); - obj.solution = sanitizeHtml(obj.solution); - if (!!error) - return res.json({errmsg: error}); - ProblemModel.update(pid, obj, (err) => { - res.json(err || {}); - }); + let obj = req.body.prob; + if (ProblemModel.checkProblem(obj)) { + obj.instruction = sanitizeHtml(obj.instruction); + obj.solution = sanitizeHtml(obj.solution); + ProblemModel.safeUpdate(obj, req.userId); + } + res.json({}); }); router.delete("/problems", access.logged, access.ajax, (req,res) => { const pid = req.query.id; - if (!pid.match(/^[0-9]+$/)) - res.json({errmsg: "Bad problem ID"}); - ProblemModel.safeRemove(pid, req.userId, err => { - res.json(err || {}); - }); + if (pid.toString().match(/^[0-9]+$/)) + ProblemModel.safeRemove(pid, req.userId); + res.json({}); }); module.exports = router;