X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=routes%2Fproblems.js;h=777543b109b65bb2c3896f072bdb120883975123;hb=b955c65b942d09d24b5c3bed0d755d4f2f8f71f1;hp=adb75dae5460e4c22cdb365852dab5f7f58d1598;hpb=936dc463c969f648ae0bc81074ff3272c7c99697;p=vchess.git diff --git a/routes/problems.js b/routes/problems.js index adb75dae..777543b1 100644 --- a/routes/problems.js +++ b/routes/problems.js @@ -6,11 +6,27 @@ const ProblemModel = require("../models/Problem"); const sanitizeHtml = require('sanitize-html'); const MaxNbProblems = 20; -// Get one problem -router.get("/problems/:vname([a-zA-Z0-9]+)/:pnum([0-9]+)", access.ajax, (req,res) => { - const vname = req.params["vname"]; - const pnum = req.params["pnum"]; - ProblemModel.getOne(vname, pnum, (err,problem) => { +function sanitizeUserInput(fen, instructions, solution) +{ + if (!fen.match(/^[a-zA-Z0-9, /-]*$/)) + return "Bad characters in FEN string"; + instructions = sanitizeHtml(instructions); + solution = sanitizeHtml(solution); + if (instructions.length == 0) + return "Empty instructions"; + if (solution.length == 0) + return "Empty solution"; + return { + fen: fen, + instructions: instructions, + solution: 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}); @@ -18,8 +34,8 @@ router.get("/problems/:vname([a-zA-Z0-9]+)/:pnum([0-9]+)", access.ajax, (req,res }); // Fetch N previous or next problems -router.get("/problems/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => { - const vname = req.params["vname"]; +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; @@ -27,7 +43,7 @@ router.get("/problems/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => { return res.json({errmsg: "Bad timestamp"}); if (!["others","mine"].includes(type)) return res.json({errmsg: "Bad type"}); - ProblemModel.fetchN(vname, req.userId, type, directionStr, lastDt, MaxNbProblems, + ProblemModel.fetchN(vid, req.userId, type, directionStr, lastDt, MaxNbProblems, (err,problems) => { if (!!err) return res.json(err); @@ -36,47 +52,42 @@ router.get("/problems/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => { ); }); -function sanitizeUserInput(fen, instructions, solution) -{ - if (!fen.match(/^[a-zA-Z0-9, /-]*$/)) - return "Bad characters in FEN string"; - instructions = sanitizeHtml(instructions); - solution = sanitizeHtml(solution); - if (instructions.length == 0) - return "Empty instructions"; - if (solution.length == 0) - return "Empty solution"; - return { - fen: fen, - instructions: instructions, - solution: solution - }; -} - // Upload a problem (sanitize inputs) -router.post("/problems/:vname([a-zA-Z0-9]+)", access.logged, access.ajax, (req,res) => { - const vname = req.params["vname"]; - const s = sanitizeUserInput(req.body["fen"], req.body["instructions"], req.body["solution"]); +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); - res.json({}); + ProblemModel.create(req.userId, vid, s.fen, s.instructions, s.solution, + (err,pid) => { + if (!!err) + return res.json(err); + res.json({id: pid["rowid"]}); + } + ); }); // Update a problem (also sanitize inputs) router.put("/problems/:id([0-9]+)", access.logged, access.ajax, (req,res) => { const pid = req.params["id"]; //problem ID - const s = sanitizeUserInput(req.body["fen"], req.body["instructions"], req.body["solution"]); + const s = sanitizeUserInput( + req.body["fen"], req.body["instructions"], req.body["solution"]); if (typeof s === "string") return res.json({errmsg: s}); - ProblemModel.update(pid, req.userId, fen, instructions, solution); - res.json({}); + ProblemModel.update(pid, req.userId, s.fen, s.instructions, s.solution, + err => { + if (!!err) + return res.json(err); + res.json({}); + } + ); }); // Delete a problem router.delete("/problems/:id([0-9]+)", access.logged, access.ajax, (req,res) => { const pid = req.params["id"]; //problem ID - ProblemModel.delete(pid, req.userId); + ProblemModel.remove(pid, req.userId); res.json({}); });