X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=models%2FProblem.js;h=7ac92f7898a15d406575ab75c5f2cb7764ce3384;hb=582df3497b0f91dd4b645386a059eac9e98da1bb;hp=0c800901c4256014b546a9ee7e3e4af1ddf25f08;hpb=8d7e2786f5a67a1b9a77c742d7951e0efbe8747d;p=vchess.git diff --git a/models/Problem.js b/models/Problem.js index 0c800901..7ac92f78 100644 --- a/models/Problem.js +++ b/models/Problem.js @@ -2,7 +2,7 @@ var db = require("../utils/database"); /* * Structure: - * _id: problem number (int) + * id: problem number (int) * uid: user id (int) * vid: variant id (int) * added: timestamp @@ -10,63 +10,69 @@ var db = require("../utils/database"); * solution: text */ -exports.create = function(vname, fen, instructions, solution) +// TODO: callback ? +exports.create = function(vid, fen, instructions, solution) { db.serialize(function() { - db.get("SELECT id FROM Variants WHERE name = '" + vname + "'", (err,variant) => { - db.run( - "INSERT INTO Problems (added, vid, fen, instructions, solution) VALUES " + - "(" + - Date.now() + "," + - variant._id + "," + - fen + "," + - instructions + "," + - solution + - ")"); - }); + const query = + "INSERT INTO Problems (added, vid, fen, instructions, solution) VALUES " + + "(" + + Date.now() + "," + + vid + "," + + fen + "," + + instructions + "," + + solution + + ")"; + db.run(query); }); } -exports.getById = function(id, callback) +exports.getOne = function(id, callback) { db.serialize(function() { - db.get( - "SELECT * FROM Problems " + - "WHERE id ='" + id + "'", - callback); + const query = + "SELECT * " + + "FROM Problems " + + "WHERE id = " + id; + db.get(query, callback); }); } -exports.fetchN = function(vname, directionStr, lastDt, MaxNbProblems, callback) +exports.fetchN = function(vid, uid, type, directionStr, lastDt, MaxNbProblems, callback) { db.serialize(function() { - db.all( + let typeLine = ""; + if (uid > 0) + typeLine = "AND id " + (type=="others" ? "!=" : "=") + " " + uid; + const query = "SELECT * FROM Problems " + - "WHERE vid = (SELECT id FROM Variants WHERE name = '" + vname + "') " + - " AND added " + directionStr + " " + lastDt + " " + + "WHERE vid = " + vid + + " AND added " + directionStr + " " + lastDt + " " + typeLine + " " + "ORDER BY added " + (directionStr=="<" ? "DESC " : "") + - "LIMIT " + MaxNbProblems, - callback); + "LIMIT " + MaxNbProblems; + db.all(query, callback); }); } exports.update = function(id, uid, fen, instructions, solution) { db.serialize(function() { - db.run( + const query = "UPDATE Problems " + "fen = " + fen + ", " + "instructions = " + instructions + ", " + "solution = " + solution + " " + - "WHERE id = " + id + " AND uid = " + uid); + "WHERE id = " + id + " AND uid = " + uid; + db.run(query); }); } exports.remove = function(id, uid) { db.serialize(function() { - db.run( + const query = "DELETE FROM Problems " + - "WHERE id = " + id + " AND uid = " + uid); + "WHERE id = " + id + " AND uid = " + uid; + db.run(query); }); }