a1f99031297f962ebdbf3b59cec20353bc966f7f
1 var db
= require("../utils/database");
5 * id: problem number (int)
7 * vid: variant id (int)
15 create: function(uid
, vid
, fen
, instructions
, solution
, cb
)
17 db
.serialize(function() {
19 "INSERT INTO Problems (added, uid, vid, fen, instructions, solution) " +
20 "VALUES (" + Date
.now() + "," + uid
+ "," + vid
+ ",'" + fen
+ "',?,?)";
21 db
.run(insertQuery
, [instructions
, solution
], err
=> {
24 db
.get("SELECT last_insert_rowid() AS rowid", cb
);
29 getOne: function(id
, callback
)
31 db
.serialize(function() {
36 db
.get(query
, callback
);
40 fetchN: function(vid
, uid
, type
, directionStr
, lastDt
, MaxNbProblems
, callback
)
42 db
.serialize(function() {
45 typeLine
= "AND uid " + (type
=="others" ? "!=" : "=") + " " + uid
;
47 "SELECT * FROM Problems " +
48 "WHERE vid = " + vid
+
49 " AND added " + directionStr
+ " " + lastDt
+ " " + typeLine
+ " " +
50 "ORDER BY added " + (directionStr
=="<" ? "DESC " : "") +
51 "LIMIT " + MaxNbProblems
;
52 db
.all(query
, callback
);
56 // TODO: update fails (but insert is OK)
57 update: function(id
, uid
, fen
, instructions
, solution
, cb
)
59 db
.serialize(function() {
61 "UPDATE Problems SET " +
62 "fen = '" + fen
+ "', " +
63 "instructions = ?, " +
65 "WHERE id = " + id
+ " AND uid = " + uid
;
66 db
.run(query
, [instructions
,solution
], cb
);
70 remove: function(id
, uid
)
72 db
.serialize(function() {
74 "DELETE FROM Problems " +
75 "WHERE id = " + id
+ " AND uid = " + uid
;
81 module
.exports
= ProblemModel
;