X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=models%2FProblem.js;h=99e5b620119e66fd79c78780a48c6f1a1e8e6805;hb=badeb466c977ed9a8e1b464a2236001126decb9e;hp=7ac92f7898a15d406575ab75c5f2cb7764ce3384;hpb=60d9063fdfcd4b7628fbc0e0fc594f083bda8761;p=vchess.git diff --git a/models/Problem.js b/models/Problem.js index 7ac92f78..99e5b620 100644 --- a/models/Problem.js +++ b/models/Problem.js @@ -10,20 +10,20 @@ var db = require("../utils/database"); * solution: text */ -// TODO: callback ? -exports.create = function(vid, fen, instructions, solution) +exports.create = function(uid, vid, fen, instructions, solution, cb) { db.serialize(function() { - const query = - "INSERT INTO Problems (added, vid, fen, instructions, solution) VALUES " + - "(" + - Date.now() + "," + - vid + "," + - fen + "," + - instructions + "," + - solution + - ")"; - db.run(query); + 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); + }); +// const stmt = db.prepare(query); +// stmt.run(instructions, solution); +// stmt.finalize(); }); } @@ -43,7 +43,7 @@ exports.fetchN = function(vid, uid, type, directionStr, lastDt, MaxNbProblems, c 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 = " + vid + @@ -54,16 +54,17 @@ exports.fetchN = function(vid, uid, type, directionStr, lastDt, MaxNbProblems, c }); } -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); }); }