X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=routes%2Fproblems.js;h=3434f0cc2c6afc6ba737e79feabf07da3974e6ba;hb=582df3497b0f91dd4b645386a059eac9e98da1bb;hp=43258a0cb0b20eec57d174ea7220b7f725c49475;hpb=c018b304ba439ca92348dcb65715707f5cfcee05;p=vchess.git diff --git a/routes/problems.js b/routes/problems.js index 43258a0c..3434f0cc 100644 --- a/routes/problems.js +++ b/routes/problems.js @@ -6,20 +6,6 @@ const ProblemModel = require("../models/Problem"); const sanitizeHtml = require('sanitize-html'); const MaxNbProblems = 20; -// Fetch N previous or next problems -router.get("/problems/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => { - const vname = req.params["vname"]; - const directionStr = (req.query.direction == "forward" ? ">" : "<"); - const lastDt = req.query.last_dt; - if (!lastDt.match(/[0-9]+/)) - return res.json({errmsg: "Bad timestamp"}); - ProblemModel.fetchN(vname, directionStr, lastDt, MaxNbProblems, (err,problems) => { - if (!!err) - return res.json(err); - return res.json({problems: problems}); - }); -}); - function sanitizeUserInput(fen, instructions, solution) { if (!fen.match(/^[a-zA-Z0-9, /-]*$/)) @@ -37,13 +23,42 @@ function sanitizeUserInput(fen, instructions, solution) }; } +// Get one problem (TODO: vid unused, here for URL de-ambiguification) +router.get("/problems/:vid([0-9]+)/:id([0-9]+)", access.ajax, (req,res) => { + const pid = req.params["id"]; + ProblemModel.getOne(pid, (err,problem) => { + if (!!err) + return res.json(err); + return res.json({problem: problem}); + }); +}); + +// Fetch N previous or next problems +router.get("/problems/:vid([0-9]+)", access.ajax, (req,res) => { + const vid = req.params["vid"]; + const directionStr = (req.query.direction == "forward" ? ">" : "<"); + const lastDt = req.query.last_dt; + const type = req.query.type; + if (!lastDt.match(/[0-9]+/)) + return res.json({errmsg: "Bad timestamp"}); + if (!["others","mine"].includes(type)) + return res.json({errmsg: "Bad type"}); + ProblemModel.fetchN(vid, req.userId, type, directionStr, lastDt, MaxNbProblems, + (err,problems) => { + if (!!err) + return res.json(err); + return res.json({problems: problems}); + } + ); +}); + // Upload a problem (sanitize inputs) -router.post("/problems/:vname([a-zA-Z0-9]+)", access.logged, access.ajax, (req,res) => { - const vname = req.params["vname"]; +router.post("/problems/:vid([0-9]+)", access.logged, access.ajax, (req,res) => { + const vid = req.params["vid"]; const s = sanitizeUserInput(req.body["fen"], req.body["instructions"], req.body["solution"]); if (typeof s === "string") return res.json({errmsg: s}); - ProblemModel.create(vname, s.fen, s.instructions, s.solution); + ProblemModel.create(vid, s.fen, s.instructions, s.solution); res.json({}); });