X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FProblem.js;h=cc7400670ef8852d69b54dfcfacb3e02faba229d;hb=7a0c1b7e33a346195caebfdfa6489e7c6d0457e6;hp=136fb649d6651caf169256c7df97a7ed9f39126c;hpb=866842c3c310524c034922870234120ed2a16cbf;p=vchess.git diff --git a/server/models/Problem.js b/server/models/Problem.js index 136fb649..cc740067 100644 --- a/server/models/Problem.js +++ b/server/models/Problem.js @@ -11,10 +11,9 @@ const db = require("../utils/database"); * solution: text */ -const ProblemModel = -{ - checkProblem: function(p) - { +const ProblemModel = { + + checkProblem: function(p) { return ( p.id.toString().match(/^[0-9]+$/) && p.vid.toString().match(/^[0-9]+$/) && @@ -22,8 +21,7 @@ const ProblemModel = ); }, - create: function(p, cb) - { + create: function(p, cb) { db.serialize(function() { const query = "INSERT INTO Problems " + @@ -31,39 +29,45 @@ const ProblemModel = "VALUES " + "(" + Date.now() + "," + p.uid + "," + p.vid + ",'" + p.fen + "',?,?)"; db.run(query, [p.instruction,p.solution], function(err) { - cb(err, {pid: this.lastID}); + cb(err, { id: this.lastID }); }); }); }, - 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); }); }); }, - getOne: function(id, cb) - { + getOne: function(id, cb) { db.serialize(function() { const query = "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 " + @@ -71,20 +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;