X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=models%2FProblem.js;h=8f3a302c8d61d6515997ab5a9be5c915cb892265;hb=00f2759e16ec73fa1ecd0254a9c9018530d71892;hp=78586761ced8ca548706a341504b570cf85ec13d;hpb=fd08ab2c5b8931bb8c95cf7e9f2f95122647f991;p=vchess.git diff --git a/models/Problem.js b/models/Problem.js index 78586761..8f3a302c 100644 --- a/models/Problem.js +++ b/models/Problem.js @@ -10,59 +10,40 @@ var db = require("../utils/database"); * solution: text */ -// TODO: callback ? -exports.create = function(vname, fen, instructions, solution) +exports.create = function(uid, vid, fen, instructions, solution, cb) { db.serialize(function() { - const vidQuery = - "SELECT id " + - "FROM Variants " + - "WHERE name = '" + vname + "'"; - db.get(vidQuery, (err,variant) => { - const insertQuery = - "INSERT INTO Problems (added, vid, fen, instructions, solution) VALUES " + - "(" + - Date.now() + "," + - variant.id + "," + - fen + "," + - instructions + "," + - solution + - ")"; - db.run(insertQuery); + const insertQuery = + "INSERT INTO Problems (added, uid, vid, fen, instructions, solution) " + + "VALUES (" + Date.now() + "," + uid + "," + vid + ",'" + fen + "',?,?)"; + db.run(insertQuery, [instructions, solution], err => { + if (!!err) + return cb(err); + db.get("SELECT last_insert_rowid() AS rowid", cb); }); }); } -exports.getById = function(id, callback) -{ - db.serialize(function() { - const query = - "SELECT * FROM Problems " + - "WHERE id ='" + id + "'"; - db.get(query, callback); - }); -} - -exports.getOne = function(vname, pid, callback) +exports.getOne = function(id, callback) { db.serialize(function() { const query = "SELECT * " + "FROM Problems " + - "WHERE id = " + pid; + "WHERE id = " + id; db.get(query, callback); }); } -exports.fetchN = function(vname, uid, type, directionStr, lastDt, MaxNbProblems, callback) +exports.fetchN = function(vid, uid, type, directionStr, lastDt, MaxNbProblems, callback) { db.serialize(function() { let typeLine = ""; if (uid > 0) - typeLine = "AND id " + (type=="others" ? "!=" : "=") + " " + uid; + typeLine = "AND uid " + (type=="others" ? "!=" : "=") + " " + uid; const query = "SELECT * FROM Problems " + - "WHERE vid = (SELECT id FROM Variants WHERE name = '" + vname + "') " + + "WHERE vid = " + vid + " AND added " + directionStr + " " + lastDt + " " + typeLine + " " + "ORDER BY added " + (directionStr=="<" ? "DESC " : "") + "LIMIT " + MaxNbProblems; @@ -70,16 +51,17 @@ exports.fetchN = function(vname, uid, type, directionStr, lastDt, MaxNbProblems, }); } -exports.update = function(id, uid, fen, instructions, solution) +// TODO: update fails (but insert is OK) +exports.update = function(id, uid, fen, instructions, solution, cb) { db.serialize(function() { const query = - "UPDATE Problems " + - "fen = " + fen + ", " + - "instructions = " + instructions + ", " + - "solution = " + solution + " " + + "UPDATE Problems SET " + + "fen = '" + fen + "', " + + "instructions = ?, " + + "solution = ? " + "WHERE id = " + id + " AND uid = " + uid; - db.run(query); + db.run(query, [instructions,solution], cb); }); }