bcbefc5f251300c0d2ea64a2be658f62434d97fe
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(cursor
, cb
) {
37 db
.serialize(function() {
41 "WHERE added < " + cursor
+ " " +
42 "ORDER BY added DESC " +
43 "LIMIT 20"; //TODO: 20 is arbitrary
44 db
.all(query
, (err
, problems
) => {
50 getOne: function(id
, cb
) {
51 db
.serialize(function() {
56 db
.get(query
, (err
, problem
) => {
62 safeUpdate: function(prob
, uid
) {
63 db
.serialize(function() {
67 "vid = " + prob
.vid
+ "," +
68 "fen = '" + prob
.fen
+ "'," +
71 "WHERE id = " + prob
.id
+ " AND uid = " + uid
;
72 db
.run(query
, [prob
.instruction
, prob
.solution
]);
76 safeRemove: function(id
, uid
) {
77 db
.serialize(function() {
79 "DELETE FROM Problems " +
80 "WHERE id = " + id
+ " AND uid = " + uid
;
86 module
.exports
= ProblemModel
;