X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fmodels%2FProblem.js;h=9978d76f108c97c25ca1ef6adc9bc372c6ed86c2;hp=75c2e146965853222abc86f86daff08c8dac680e;hb=a9e7935190d8fc112e674add05e86b8d0152e8df;hpb=89021f181ac0689bbc785ce0ebd9a910e66352b0 diff --git a/server/models/Problem.js b/server/models/Problem.js index 75c2e146..9978d76f 100644 --- a/server/models/Problem.js +++ b/server/models/Problem.js @@ -1,4 +1,4 @@ -var db = require("../utils/database"); +const db = require("../utils/database"); /* * Structure: @@ -11,96 +11,84 @@ var db = require("../utils/database"); * solution: text */ -const ProblemModel = -{ - checkProblem: function(p) - { - if (!p.vid.toString().match(/^[0-9]+$/)) - return "Wrong variant ID"; - if (!p.fen.match(/^[a-zA-Z0-9, /-]*$/)) - return "Bad FEN string"; - return ""; +const ProblemModel = { + checkProblem: function(p) { + return ( + p.id.toString().match(/^[0-9]+$/) && + p.vid.toString().match(/^[0-9]+$/) && + p.fen.match(/^[a-zA-Z0-9, /-]*$/) + ); }, - create: function(p, cb) - { + create: function(p, cb) { db.serialize(function() { const query = "INSERT INTO Problems " + "(added, uid, vid, fen, instruction, solution) " + "VALUES " + - "(" + Date.now() + "," + p.uid + ",'" + p.fen + "',?,?)"; - db.run(query, p.instruction, p.solution, function(err) { - return cb(err, {pid: this.lastID}); + "(" + Date.now() + "," + p.uid + "," + p.vid + ",'" + p.fen + "',?,?)"; + db.run(query, [p.instruction,p.solution], function(err) { + 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) => { - return cb(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) => { - return cb(err, problem); + db.get(query, (err, problem) => { + cb(err, problem); }); }); }, - update: function(id, prob) - { + safeUpdate: function(prob, uid, devs) { db.serialize(function() { - let query = + let whereClause = "WHERE id = " + prob.id; + if (!devs.includes(uid)) whereClause += " AND uid = " + uid; + const query = "UPDATE Problems " + "SET " + "vid = " + prob.vid + "," + - "fen = " + prob.fen + "," + - "instruction = " + prob.instruction + "," + - "solution = " + prob.solution + " " + - "WHERE id = " + id; - db.run(query); + "fen = '" + prob.fen + "'," + + "instruction = ?," + + "solution = ? " + + whereClause; + db.run(query, [prob.instruction, prob.solution]); }); }, - remove: function(id) - { + safeRemove: function(id, uid, devs) { db.serialize(function() { + let whereClause = "WHERE id = " + prob.id; + if (!devs.includes(uid)) whereClause += " AND uid = " + uid; const query = "DELETE FROM Problems " + - "WHERE id = " + id; + whereClause; db.run(query); }); }, - - safeRemove: function(id, uid, cb) - { - db.serialize(function() { - const query = - "SELECT 1 " + - "FROM Problems " + - "WHERE id = " + id + " AND uid = " + uid; - db.get(query, (err,prob) => { - if (!prob) - return cb({errmsg: "Not your problem"}); - ProvlemModel.remove(id); - cb(null); - }); - }); - }, } module.exports = ProblemModel;