Draft Game and Challenge models
[vchess.git] / models / Problem.js
index 0c80090..8f3a302 100644 (file)
@@ -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,67 @@ var db = require("../utils/database");
  *   solution: text
  */
 
-exports.create = function(vname, fen, instructions, solution)
+exports.create = function(uid, vid, fen, instructions, solution, cb)
 {
        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 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)
+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 uid " + (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)
+// TODO: update fails (but insert is OK)
+exports.update = function(id, uid, fen, instructions, solution, cb)
 {
        db.serialize(function() {
-               db.run(
-                       "UPDATE Problems " +
-                               "fen = " + fen + ", " +
-                               "instructions = " + instructions + ", " +
-                               "solution = " + solution + " " +
-                       "WHERE id = " + id + " AND uid = " + uid);
+               const query =
+                       "UPDATE Problems SET " +
+                               "fen = '" + fen + "', " +
+                               "instructions = ?, " +
+                               "solution = ? " +
+                       "WHERE id = " + id + " AND uid = " + uid;
+               db.run(query, [instructions,solution], cb);
        });
 }
 
 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);
        });
 }