X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=models%2FProblem.js;h=a1f99031297f962ebdbf3b59cec20353bc966f7f;hb=b955c65b942d09d24b5c3bed0d755d4f2f8f71f1;hp=6184eafcf1c947f20c56a5455c8290038f8a3b5f;hpb=936dc463c969f648ae0bc81074ff3272c7c99697;p=vchess.git diff --git a/models/Problem.js b/models/Problem.js index 6184eafc..a1f99031 100644 --- a/models/Problem.js +++ b/models/Problem.js @@ -10,85 +10,72 @@ var db = require("../utils/database"); * solution: text */ -// TODO: callback ? -exports.create = function(vname, fen, instructions, solution) +const ProblemModel = { - db.serialize(function() { - const vidQuery = - "SELECT id " + - "FROM Variants " + - "WHERE name = '" + vname + "'"; - db.get(vidQuery, (err,variant) => { + create: function(uid, vid, fen, instructions, solution, cb) + { + db.serialize(function() { const insertQuery = - "INSERT INTO Problems (added, vid, fen, instructions, solution) VALUES " + - "(" + - Date.now() + "," + - variant.id + "," + - fen + "," + - instructions + "," + - solution + - ")"; - db.run(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); - }); -} + getOne: function(id, callback) + { + db.serialize(function() { + const query = + "SELECT * " + + "FROM Problems " + + "WHERE id = " + id; + db.get(query, callback); + }); + }, -exports.getOne = function(vname, pid, callback) -{ - db.serialize(function() { - const query = - "SELECT * " + - "FROM Problems " + - "WHERE id = " + pid; - db.get(query, callback); - }); -} + fetchN: function(vid, uid, type, directionStr, lastDt, MaxNbProblems, callback) + { + db.serialize(function() { + let typeLine = ""; + if (uid > 0) + typeLine = "AND uid " + (type=="others" ? "!=" : "=") + " " + uid; + const query = + "SELECT * FROM Problems " + + "WHERE vid = " + vid + + " AND added " + directionStr + " " + lastDt + " " + typeLine + " " + + "ORDER BY added " + (directionStr=="<" ? "DESC " : "") + + "LIMIT " + MaxNbProblems; + db.all(query, callback); + }); + }, -exports.fetchN = function(vname, uid, type, directionStr, lastDt, MaxNbProblems, callback) -{ - db.serialize(function() { - 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 + " " + typeLine + " " + - "ORDER BY added " + (directionStr=="<" ? "DESC " : "") + - "LIMIT " + MaxNbProblems, - db.all(query, callback); - }); -} + // TODO: update fails (but insert is OK) + update: function(id, uid, fen, instructions, solution, cb) + { + db.serialize(function() { + const query = + "UPDATE Problems SET " + + "fen = '" + fen + "', " + + "instructions = ?, " + + "solution = ? " + + "WHERE id = " + id + " AND uid = " + uid; + db.run(query, [instructions,solution], cb); + }); + }, -exports.update = function(id, uid, fen, instructions, solution) -{ - db.serialize(function() { - const query = - "UPDATE Problems " + - "fen = " + fen + ", " + - "instructions = " + instructions + ", " + - "solution = " + solution + " " + - "WHERE id = " + id + " AND uid = " + uid; - db.run(query); - }); + remove: function(id, uid) + { + db.serialize(function() { + const query = + "DELETE FROM Problems " + + "WHERE id = " + id + " AND uid = " + uid; + db.run(query); + }); + }, } -exports.remove = function(id, uid) -{ - db.serialize(function() { - const query = - "DELETE FROM Problems " + - "WHERE id = " + id + " AND uid = " + uid; - db.run(query); - }); -} +module.exports = ProblemModel;