1 const db
= require("../utils/database");
8 * vid: variant id (int)
9 * fen: varchar (optional)
16 checkProblem: function(p
)
19 p
.id
.toString().match(/^[0-9]+$/) &&
20 p
.vid
.toString().match(/^[0-9]+$/) &&
21 p
.fen
.match(/^[a-zA-Z0-9, /-]*$/)
25 create: function(p
, cb
)
27 db
.serialize(function() {
29 "INSERT INTO Problems " +
30 "(added, uid, vid, fen, instruction, solution) " +
32 "(" + Date
.now() + "," + p
.uid
+ "," + p
.vid
+ ",'" + p
.fen
+ "',?,?)";
33 db
.run(query
, [p
.instruction
,p
.solution
], function(err
) {
34 cb(err
, {pid: this.lastID
});
41 db
.serialize(function() {
45 db
.all(query
, (err
,problems
) => {
51 getOne: function(id
, cb
)
53 db
.serialize(function() {
58 db
.get(query
, (err
,problem
) => {
64 safeUpdate: function(prob
, uid
)
66 db
.serialize(function() {
70 "vid = " + prob
.vid
+ "," +
71 "fen = '" + prob
.fen
+ "'," +
74 "WHERE id = " + prob
.id
+ " AND uid = " + uid
;
75 db
.run(query
, [prob
.instruction
,prob
.solution
]);
79 safeRemove: function(id
, uid
)
81 db
.serialize(function() {
83 "DELETE FROM Problems " +
84 "WHERE id = " + id
+ " AND uid = " + uid
;
90 module
.exports
= ProblemModel
;