X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FProblem.js;h=cc7400670ef8852d69b54dfcfacb3e02faba229d;hb=9f9e9a0588d4b28a1825a56ae53553d89bf0c65f;hp=bcbefc5f251300c0d2ea64a2be658f62434d97fe;hpb=68e19a449db7a12e0a168e99cd750d985c983ba1;p=vchess.git diff --git a/server/models/Problem.js b/server/models/Problem.js index bcbefc5f..cc740067 100644 --- a/server/models/Problem.js +++ b/server/models/Problem.js @@ -12,6 +12,7 @@ const db = require("../utils/database"); */ const ProblemModel = { + checkProblem: function(p) { return ( p.id.toString().match(/^[0-9]+$/) && @@ -33,12 +34,16 @@ const ProblemModel = { }); }, - getNext: function(cursor, cb) { + getNext: function(uid, onlyMine, cursor, cb) { + let condition = ""; + if (onlyMine) condition = "AND uid = " + uid + " "; + else if (!!uid) condition = "AND uid <> " + uid + " "; db.serialize(function() { const query = "SELECT * " + "FROM Problems " + "WHERE added < " + cursor + " " + + condition + "ORDER BY added DESC " + "LIMIT 20"; //TODO: 20 is arbitrary db.all(query, (err, problems) => { @@ -59,8 +64,10 @@ const ProblemModel = { }); }, - safeUpdate: function(prob, uid) { + safeUpdate: function(prob, uid, devs) { db.serialize(function() { + let whereClause = "WHERE id = " + prob.id; + if (!devs.includes(uid)) whereClause += " AND uid = " + uid; const query = "UPDATE Problems " + "SET " + @@ -68,19 +75,22 @@ const ProblemModel = { "fen = '" + prob.fen + "'," + "instruction = ?," + "solution = ? " + - "WHERE id = " + prob.id + " AND uid = " + uid; + whereClause; db.run(query, [prob.instruction, prob.solution]); }); }, - safeRemove: function(id, uid) { + safeRemove: function(id, uid, devs) { db.serialize(function() { + let whereClause = "WHERE id = " + id; + if (!devs.includes(uid)) whereClause += " AND uid = " + uid; const query = "DELETE FROM Problems " + - "WHERE id = " + id + " AND uid = " + uid; + whereClause; db.run(query); }); - }, -} + } + +}; module.exports = ProblemModel;