1 const db
= require("../utils/database");
8 * vid: variant id (int)
9 * fen: varchar (optional)
14 const ProblemModel
= {
15 checkProblem: function(p
) {
17 p
.id
.toString().match(/^[0-9]+$/) &&
18 p
.vid
.toString().match(/^[0-9]+$/) &&
19 p
.fen
.match(/^[a-zA-Z0-9, /-]*$/)
23 create: function(p
, cb
) {
24 db
.serialize(function() {
26 "INSERT INTO Problems " +
27 "(added, uid, vid, fen, instruction, solution) " +
29 "(" + Date
.now() + "," + p
.uid
+ "," + p
.vid
+ ",'" + p
.fen
+ "',?,?)";
30 db
.run(query
, [p
.instruction
,p
.solution
], function(err
) {
31 cb(err
, { id: this.lastID
});
36 getNext: function(uid
, onlyMine
, cursor
, cb
) {
38 if (onlyMine
) condition
= "AND uid = " + uid
+ " ";
39 else if (!!uid
) condition
= "AND uid <> " + uid
+ " ";
40 db
.serialize(function() {
44 "WHERE added < " + cursor
+ " " +
46 "ORDER BY added DESC " +
47 "LIMIT 20"; //TODO: 20 is arbitrary
48 db
.all(query
, (err
, problems
) => {
54 getOne: function(id
, cb
) {
55 db
.serialize(function() {
60 db
.get(query
, (err
, problem
) => {
66 safeUpdate: function(prob
, uid
, devs
) {
67 db
.serialize(function() {
68 let whereClause
= "WHERE id = " + prob
.id
;
69 if (!devs
.includes(uid
)) whereClause
+= " AND uid = " + uid
;
73 "vid = " + prob
.vid
+ "," +
74 "fen = '" + prob
.fen
+ "'," +
78 db
.run(query
, [prob
.instruction
, prob
.solution
]);
82 safeRemove: function(id
, uid
, devs
) {
83 db
.serialize(function() {
84 let whereClause
= "WHERE id = " + id
;
85 if (!devs
.includes(uid
)) whereClause
+= " AND uid = " + uid
;
87 "DELETE FROM Problems " +
94 module
.exports
= ProblemModel
;