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
);
24 // const stmt = db.prepare(query);
25 // stmt.run(instructions, solution);
30 exports
.getOne = function(id
, callback
)
32 db
.serialize(function() {
37 db
.get(query
, callback
);
41 exports
.fetchN = function(vid
, uid
, type
, directionStr
, lastDt
, MaxNbProblems
, callback
)
43 db
.serialize(function() {
46 typeLine
= "AND uid " + (type
=="others" ? "!=" : "=") + " " + uid
;
48 "SELECT * FROM Problems " +
49 "WHERE vid = " + vid
+
50 " AND added " + directionStr
+ " " + lastDt
+ " " + typeLine
+ " " +
51 "ORDER BY added " + (directionStr
=="<" ? "DESC " : "") +
52 "LIMIT " + MaxNbProblems
;
53 db
.all(query
, callback
);
57 // TODO: update fails (but insert is OK)
58 exports
.update = function(id
, uid
, fen
, instructions
, solution
, cb
)
60 db
.serialize(function() {
62 "UPDATE Problems SET " +
63 "fen = '" + fen
+ "', " +
64 "instructions = ?, " +
66 "WHERE id = " + id
+ " AND uid = " + uid
;
67 db
.run(query
, [instructions
,solution
], cb
);
71 exports
.remove = function(id
, uid
)
73 db
.serialize(function() {
75 "DELETE FROM Problems " +
76 "WHERE id = " + id
+ " AND uid = " + uid
;