Commit | Line | Data |
---|---|---|
8d7e2786 BA |
1 | var db = require("../utils/database"); |
2 | ||
3 | /* | |
4 | * Structure: | |
c018b304 | 5 | * id: problem number (int) |
8d7e2786 BA |
6 | * uid: user id (int) |
7 | * vid: variant id (int) | |
8 | * added: timestamp | |
9 | * instructions: text | |
10 | * solution: text | |
11 | */ | |
12 | ||
badeb466 | 13 | exports.create = function(uid, vid, fen, instructions, solution, cb) |
8d7e2786 BA |
14 | { |
15 | db.serialize(function() { | |
badeb466 BA |
16 | const insertQuery = |
17 | "INSERT INTO Problems (added, uid, vid, fen, instructions, solution) " + | |
18 | "VALUES (" + Date.now() + "," + uid + "," + vid + ",'" + fen + "',?,?)"; | |
19 | db.run(insertQuery, [instructions, solution], err => { | |
20 | if (!!err) | |
21 | return cb(err); | |
22 | db.get("SELECT last_insert_rowid() AS rowid", cb); | |
23 | }); | |
24 | // const stmt = db.prepare(query); | |
25 | // stmt.run(instructions, solution); | |
26 | // stmt.finalize(); | |
8d7e2786 BA |
27 | }); |
28 | } | |
29 | ||
582df349 | 30 | exports.getOne = function(id, callback) |
8d7e2786 BA |
31 | { |
32 | db.serialize(function() { | |
936dc463 BA |
33 | const query = |
34 | "SELECT * " + | |
35 | "FROM Problems " + | |
582df349 | 36 | "WHERE id = " + id; |
936dc463 BA |
37 | db.get(query, callback); |
38 | }); | |
39 | } | |
40 | ||
582df349 | 41 | exports.fetchN = function(vid, uid, type, directionStr, lastDt, MaxNbProblems, callback) |
936dc463 BA |
42 | { |
43 | db.serialize(function() { | |
44 | let typeLine = ""; | |
45 | if (uid > 0) | |
badeb466 | 46 | typeLine = "AND uid " + (type=="others" ? "!=" : "=") + " " + uid; |
936dc463 | 47 | const query = |
8d7e2786 | 48 | "SELECT * FROM Problems " + |
582df349 | 49 | "WHERE vid = " + vid + |
936dc463 | 50 | " AND added " + directionStr + " " + lastDt + " " + typeLine + " " + |
8d7e2786 | 51 | "ORDER BY added " + (directionStr=="<" ? "DESC " : "") + |
fd08ab2c | 52 | "LIMIT " + MaxNbProblems; |
936dc463 | 53 | db.all(query, callback); |
8d7e2786 BA |
54 | }); |
55 | } | |
56 | ||
badeb466 BA |
57 | // TODO: update fails (but insert is OK) |
58 | exports.update = function(id, uid, fen, instructions, solution, cb) | |
8d7e2786 BA |
59 | { |
60 | db.serialize(function() { | |
936dc463 | 61 | const query = |
badeb466 BA |
62 | "UPDATE Problems SET " + |
63 | "fen = '" + fen + "', " + | |
64 | "instructions = ?, " + | |
65 | "solution = ? " + | |
936dc463 | 66 | "WHERE id = " + id + " AND uid = " + uid; |
badeb466 | 67 | db.run(query, [instructions,solution], cb); |
8d7e2786 BA |
68 | }); |
69 | } | |
70 | ||
71 | exports.remove = function(id, uid) | |
72 | { | |
73 | db.serialize(function() { | |
936dc463 | 74 | const query = |
8d7e2786 | 75 | "DELETE FROM Problems " + |
936dc463 BA |
76 | "WHERE id = " + id + " AND uid = " + uid; |
77 | db.run(query); | |
8d7e2786 BA |
78 | }); |
79 | } |