1 var db
= require("../utils/database");
5 * id: problem number (int)
7 * vid: variant id (int)
13 exports
.create = function(uid
, vid
, fen
, instructions
, solution
, cb
)
15 db
.serialize(function() {
17 "INSERT INTO Problems (added, uid, vid, fen, instructions, solution) " +
18 "VALUES (" + Date
.now() + "," + uid
+ "," + vid
+ ",'" + fen
+ "',?,?)";
19 db
.run(insertQuery
, [instructions
, solution
], err
=> {
22 db
.get("SELECT last_insert_rowid() AS rowid", cb
);
27 exports
.getOne = function(id
, callback
)
29 db
.serialize(function() {
34 db
.get(query
, callback
);
38 exports
.fetchN = function(vid
, uid
, type
, directionStr
, lastDt
, MaxNbProblems
, callback
)
40 db
.serialize(function() {
43 typeLine
= "AND uid " + (type
=="others" ? "!=" : "=") + " " + uid
;
45 "SELECT * FROM Problems " +
46 "WHERE vid = " + vid
+
47 " AND added " + directionStr
+ " " + lastDt
+ " " + typeLine
+ " " +
48 "ORDER BY added " + (directionStr
=="<" ? "DESC " : "") +
49 "LIMIT " + MaxNbProblems
;
50 db
.all(query
, callback
);
54 // TODO: update fails (but insert is OK)
55 exports
.update = function(id
, uid
, fen
, instructions
, solution
, cb
)
57 db
.serialize(function() {
59 "UPDATE Problems SET " +
60 "fen = '" + fen
+ "', " +
61 "instructions = ?, " +
63 "WHERE id = " + id
+ " AND uid = " + uid
;
64 db
.run(query
, [instructions
,solution
], cb
);
68 exports
.remove = function(id
, uid
)
70 db
.serialize(function() {
72 "DELETE FROM Problems " +
73 "WHERE id = " + id
+ " AND uid = " + uid
;