Refactor Problems view: should now handle better long problems lists
[vchess.git] / server / models / Problem.js
index 5c9af0b..2d29520 100644 (file)
@@ -33,12 +33,19 @@ const ProblemModel = {
     });
   },
 
-  getAll: function(cb) {
+  getNext: function(uid, onlyMine, cursor, cb) {
+    let condition = "";
+    if (onlyMine) condition = "AND uid = " + uid + " ";
+    else if (!!uid) condition = "AND uid <> " + uid + " ";
     db.serialize(function() {
       const query =
         "SELECT * " +
-        "FROM Problems";
-      db.all(query, (err,problems) => {
+        "FROM Problems " +
+        "WHERE added < " + cursor + " " +
+        condition +
+        "ORDER BY added DESC " +
+        "LIMIT 20"; //TODO: 20 is arbitrary
+      db.all(query, (err, problems) => {
         cb(err, problems);
       });
     });
@@ -50,7 +57,7 @@ const ProblemModel = {
         "SELECT * " +
         "FROM Problems " +
         "WHERE id = " + id;
-      db.get(query, (err,problem) => {
+      db.get(query, (err, problem) => {
         cb(err, problem);
       });
     });
@@ -66,7 +73,7 @@ const ProblemModel = {
           "instruction = ?," +
           "solution = ? " +
         "WHERE id = " + prob.id + " AND uid = " + uid;
-      db.run(query, [prob.instruction,prob.solution]);
+      db.run(query, [prob.instruction, prob.solution]);
     });
   },