Remove 'var' keyword from server code
[vchess.git] / server / models / Problem.js
index a1f9903..8460fec 100644 (file)
-var db = require("../utils/database");
+const db = require("../utils/database");
 
 /*
  * Structure:
- *   id: problem number (int)
+ *   id: integer
+ *   added: datetime
  *   uid: user id (int)
  *   vid: variant id (int)
- *   added: timestamp
- *   instructions: text
+ *   fen: varchar (optional)
+ *   instruction: text
  *   solution: text
  */
 
 const ProblemModel =
 {
-       create: function(uid, vid, fen, instructions, solution, cb)
-       {
-               db.serialize(function() {
-                       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);
-                       });
-               });
-       },
+  checkProblem: function(p)
+  {
+    if (!p.id.toString().match(/^[0-9]+$/))
+      return "Wrong problem ID";
+    if (!p.vid.toString().match(/^[0-9]+$/))
+      return "Wrong variant ID";
+    if (!p.fen.match(/^[a-zA-Z0-9, /-]*$/))
+      return "Bad FEN string";
+    return "";
+  },
 
-       getOne: function(id, callback)
-       {
-               db.serialize(function() {
-                       const query =
-                               "SELECT * " +
-                               "FROM Problems " +
-                               "WHERE id = " + id;
-                       db.get(query, callback);
-               });
-       },
+  create: function(p, cb)
+  {
+    db.serialize(function() {
+      const query =
+        "INSERT INTO Problems " +
+        "(added, uid, vid, fen, instruction, solution) " +
+          "VALUES " +
+        "(" + Date.now() + "," + p.uid + "," + p.vid + ",'" + p.fen  + "',?,?)";
+      db.run(query, [p.instruction,p.solution], function(err) {
+        return cb(err, {pid: this.lastID});
+      });
+    });
+  },
 
-       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);
-               });
-       },
+  getAll: function(cb)
+  {
+    db.serialize(function() {
+      const query =
+        "SELECT * " +
+        "FROM Problems";
+      db.all(query, (err,problems) => {
+        return cb(err, problems);
+      });
+    });
+  },
 
-       // 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);
-               });
-       },
+  getOne: function(id, cb)
+  {
+    db.serialize(function() {
+      const query =
+        "SELECT * " +
+        "FROM Problems " +
+        "WHERE id = " + id;
+      db.get(query, (err,problem) => {
+        return cb(err, problem);
+      });
+    });
+  },
 
-       remove: function(id, uid)
-       {
-               db.serialize(function() {
-                       const query =
-                               "DELETE FROM Problems " +
-                               "WHERE id = " + id + " AND uid = " + uid;
-                       db.run(query);
-               });
-       },
+  update: function(prob, cb)
+  {
+    db.serialize(function() {
+      let query =
+        "UPDATE Problems " +
+        "SET " +
+          "vid = " + prob.vid + "," +
+          "fen = '" + prob.fen + "'," +
+          "instruction = ?," +
+          "solution = ? " +
+        "WHERE id = " + prob.id;
+      db.run(query, [prob.instruction,prob.solution], cb);
+    });
+  },
+
+  remove: function(id)
+  {
+    db.serialize(function() {
+      const query =
+        "DELETE FROM Problems " +
+        "WHERE id = " + id;
+      db.run(query);
+    });
+  },
+
+  safeRemove: function(id, uid, cb)
+  {
+    db.serialize(function() {
+      const query =
+        "SELECT 1 " +
+        "FROM Problems " +
+        "WHERE id = " + id + " AND uid = " + uid;
+      db.get(query, (err,prob) => {
+        if (!prob)
+          return cb({errmsg: "Not your problem"});
+        ProblemModel.remove(id);
+        cb(null);
+      });
+    });
+  },
 }
 
 module.exports = ProblemModel;