1 var db
= require("../utils/database");
8 * vid: variant id (int)
9 * fen: varchar (optional)
16 checkProblem: function(p
)
18 if (!p
.id
.toString().match(/^[0-9]+$/))
19 return "Wrong problem ID";
20 if (!p
.vid
.toString().match(/^[0-9]+$/))
21 return "Wrong variant ID";
22 if (!p
.fen
.match(/^[a-zA-Z0-9, /-]*$/))
23 return "Bad FEN string";
27 create: function(p
, cb
)
29 db
.serialize(function() {
31 "INSERT INTO Problems " +
32 "(added, uid, vid, fen, instruction, solution) " +
34 "(" + Date
.now() + "," + p
.uid
+ "," + p
.vid
+ ",'" + p
.fen
+ "',?,?)";
35 db
.run(query
, [p
.instruction
,p
.solution
], function(err
) {
36 return cb(err
, {pid: this.lastID
});
43 db
.serialize(function() {
47 db
.all(query
, (err
,problems
) => {
48 return cb(err
, problems
);
53 getOne: function(id
, cb
)
55 db
.serialize(function() {
60 db
.get(query
, (err
,problem
) => {
61 return cb(err
, problem
);
66 update: function(prob
, cb
)
68 db
.serialize(function() {
72 "vid = " + prob
.vid
+ "," +
73 "fen = '" + prob
.fen
+ "'," +
76 "WHERE id = " + prob
.id
;
77 db
.run(query
, [prob
.instruction
,prob
.solution
], cb
);
83 db
.serialize(function() {
85 "DELETE FROM Problems " +
91 safeRemove: function(id
, uid
, cb
)
93 db
.serialize(function() {
97 "WHERE id = " + id
+ " AND uid = " + uid
;
98 db
.get(query
, (err
,prob
) => {
100 return cb({errmsg: "Not your problem"});
101 ProblemModel
.remove(id
);
108 module
.exports
= ProblemModel
;