X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Froutes%2Fproblems.js;h=2db81bbc6161afceb2582311c2591345351be694;hb=84fc0f02d3d399af66c40b3e9994f67b415ffd0e;hp=64c173a1db2abf9259783f974c65906ed6eb5f8d;hpb=866842c3c310524c034922870234120ed2a16cbf;p=vchess.git diff --git a/server/routes/problems.js b/server/routes/problems.js index 64c173a1..2db81bbc 100644 --- a/server/routes/problems.js +++ b/server/routes/problems.js @@ -4,44 +4,41 @@ const ProblemModel = require("../models/Problem"); const sanitizeHtml = require('sanitize-html'); router.post("/problems", access.logged, access.ajax, (req,res) => { - if (ProblemModel.checkProblem(req.body.prob)) - { - const problem = - { + 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 || {id:ret.pid}); + ProblemModel.create(problem, (err, ret) => { + res.json(err || ret); }); } else res.json({}); }); -router.get("/problems", (req,res) => { - const probId = req.query["pid"]; - if (probId && probId.match(/^[0-9]+$/)) - { - ProblemModel.getOne(req.query["pid"], (err,problem) => { +router.get("/problems", access.ajax, (req,res) => { + const probId = req.query["id"]; + const cursor = req.query["cursor"]; + if (!!probId && !!probId.match(/^[0-9]+$/)) { + ProblemModel.getOne(probId, (err, problem) => { res.json(err || {problem: problem}); }); - } - else - { - ProblemModel.getAll((err,problems) => { - res.json(err || {problems:problems}); + } else if (!!cursor && !!cursor.match(/^[0-9]+$/)) { + const onlyMine = (req.query["mode"] == "mine"); + const uid = parseInt(req.query["uid"]); + ProblemModel.getNext(uid, onlyMine, cursor, (err, problems) => { + res.json(err || { problems: problems }); }); } }); router.put("/problems", access.logged, access.ajax, (req,res) => { let obj = req.body.prob; - if (ProblemModel.checkProblem(obj)) - { + if (ProblemModel.checkProblem(obj)) { obj.instruction = sanitizeHtml(obj.instruction); obj.solution = sanitizeHtml(obj.solution); ProblemModel.safeUpdate(obj, req.userId);