1 const db
= require("../utils/database");
8 * vid: variant id (int)
9 * fen: varchar (optional)
14 const ProblemModel
= {
16 checkProblem: function(p
) {
18 p
.id
.toString().match(/^[0-9]+$/) &&
19 p
.vid
.toString().match(/^[0-9]+$/) &&
20 p
.fen
.match(/^[a-zA-Z0-9, /-]*$/)
24 create: function(p
, cb
) {
25 db
.serialize(function() {
27 "INSERT INTO Problems " +
28 "(added, uid, vid, fen, instruction, solution) " +
30 "(" + Date
.now() + "," + p
.uid
+ "," + p
.vid
+ ",'" + p
.fen
+ "',?,?)";
31 db
.run(query
, [p
.instruction
,p
.solution
], function(err
) {
32 cb(err
, { id: this.lastID
});
37 getNext: function(uid
, onlyMine
, cursor
, cb
) {
39 if (onlyMine
) condition
= "AND uid = " + uid
+ " ";
40 else if (!!uid
) condition
= "AND uid <> " + uid
+ " ";
41 db
.serialize(function() {
45 "WHERE added < " + cursor
+ " " +
47 "ORDER BY added DESC " +
48 "LIMIT 20"; //TODO: 20 is arbitrary
49 db
.all(query
, (err
, problems
) => {
55 getOne: function(id
, cb
) {
56 db
.serialize(function() {
61 db
.get(query
, (err
, problem
) => {
67 safeUpdate: function(prob
, uid
, devs
) {
68 db
.serialize(function() {
69 let whereClause
= "WHERE id = " + prob
.id
;
70 if (!devs
.includes(uid
)) whereClause
+= " AND uid = " + uid
;
74 "vid = " + prob
.vid
+ "," +
75 "fen = '" + prob
.fen
+ "'," +
79 db
.run(query
, [prob
.instruction
, prob
.solution
]);
83 safeRemove: function(id
, uid
, devs
) {
84 db
.serialize(function() {
85 let whereClause
= "WHERE id = " + id
;
86 if (!devs
.includes(uid
)) whereClause
+= " AND uid = " + uid
;
88 "DELETE FROM Problems " +
96 module
.exports
= ProblemModel
;