X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FProblem.js;h=cc7400670ef8852d69b54dfcfacb3e02faba229d;hb=937c24ab2871b31a7e531226603fc75acab7edb8;hp=5c9af0b1803d6ff675d4970804bbfd5f74a4d48a;hpb=0234201fb338fc239d6f613c677fa932c7c3697c;p=vchess.git diff --git a/server/models/Problem.js b/server/models/Problem.js index 5c9af0b1..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,19 @@ const ProblemModel = { }); }, - getAll: function(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"; - db.all(query, (err,problems) => { + "FROM Problems " + + "WHERE added < " + cursor + " " + + condition + + "ORDER BY added DESC " + + "LIMIT 20"; //TODO: 20 is arbitrary + db.all(query, (err, problems) => { cb(err, problems); }); }); @@ -50,14 +58,16 @@ const ProblemModel = { "SELECT * " + "FROM Problems " + "WHERE id = " + id; - db.get(query, (err,problem) => { + db.get(query, (err, problem) => { cb(err, problem); }); }); }, - 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 " + @@ -65,19 +75,22 @@ const ProblemModel = { "fen = '" + prob.fen + "'," + "instruction = ?," + "solution = ? " + - "WHERE id = " + prob.id + " AND uid = " + uid; - db.run(query, [prob.instruction,prob.solution]); + 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;