Commit | Line | Data |
---|---|---|
c5c47010 | 1 | const db = require("../utils/database"); |
89021f18 BA |
2 | |
3 | /* | |
4 | * Structure: | |
5 | * id: integer | |
6 | * added: datetime | |
7 | * uid: user id (int) | |
8 | * vid: variant id (int) | |
9 | * fen: varchar (optional) | |
10 | * instruction: text | |
11 | * solution: text | |
12 | */ | |
13 | ||
0234201f BA |
14 | const ProblemModel = { |
15 | checkProblem: function(p) { | |
866842c3 BA |
16 | return ( |
17 | p.id.toString().match(/^[0-9]+$/) && | |
18 | p.vid.toString().match(/^[0-9]+$/) && | |
19 | p.fen.match(/^[a-zA-Z0-9, /-]*$/) | |
20 | ); | |
89021f18 BA |
21 | }, |
22 | ||
0234201f | 23 | create: function(p, cb) { |
89021f18 BA |
24 | db.serialize(function() { |
25 | const query = | |
26 | "INSERT INTO Problems " + | |
27 | "(added, uid, vid, fen, instruction, solution) " + | |
28 | "VALUES " + | |
604b951e BA |
29 | "(" + Date.now() + "," + p.uid + "," + p.vid + ",'" + p.fen + "',?,?)"; |
30 | db.run(query, [p.instruction,p.solution], function(err) { | |
0234201f | 31 | cb(err, { id: this.lastID }); |
89021f18 BA |
32 | }); |
33 | }); | |
34 | }, | |
35 | ||
84fc0f02 BA |
36 | getNext: function(uid, onlyMine, cursor, cb) { |
37 | let condition = ""; | |
38 | if (onlyMine) condition = "AND uid = " + uid + " "; | |
39 | else if (!!uid) condition = "AND uid <> " + uid + " "; | |
89021f18 BA |
40 | db.serialize(function() { |
41 | const query = | |
42 | "SELECT * " + | |
68e19a44 BA |
43 | "FROM Problems " + |
44 | "WHERE added < " + cursor + " " + | |
84fc0f02 | 45 | condition + |
68e19a44 BA |
46 | "ORDER BY added DESC " + |
47 | "LIMIT 20"; //TODO: 20 is arbitrary | |
48 | db.all(query, (err, problems) => { | |
866842c3 | 49 | cb(err, problems); |
89021f18 BA |
50 | }); |
51 | }); | |
52 | }, | |
53 | ||
0234201f | 54 | getOne: function(id, cb) { |
89021f18 BA |
55 | db.serialize(function() { |
56 | const query = | |
57 | "SELECT * " + | |
58 | "FROM Problems " + | |
59 | "WHERE id = " + id; | |
68e19a44 | 60 | db.get(query, (err, problem) => { |
866842c3 | 61 | cb(err, problem); |
89021f18 BA |
62 | }); |
63 | }); | |
64 | }, | |
65 | ||
a9e79351 | 66 | safeUpdate: function(prob, uid, devs) { |
89021f18 | 67 | db.serialize(function() { |
a9e79351 BA |
68 | let whereClause = "WHERE id = " + prob.id; |
69 | if (!devs.includes(uid)) whereClause += " AND uid = " + uid; | |
866842c3 | 70 | const query = |
89021f18 BA |
71 | "UPDATE Problems " + |
72 | "SET " + | |
73 | "vid = " + prob.vid + "," + | |
604b951e BA |
74 | "fen = '" + prob.fen + "'," + |
75 | "instruction = ?," + | |
76 | "solution = ? " + | |
a9e79351 | 77 | whereClause; |
68e19a44 | 78 | db.run(query, [prob.instruction, prob.solution]); |
89021f18 BA |
79 | }); |
80 | }, | |
81 | ||
a9e79351 | 82 | safeRemove: function(id, uid, devs) { |
89021f18 | 83 | db.serialize(function() { |
0705a80c | 84 | let whereClause = "WHERE id = " + id; |
a9e79351 | 85 | if (!devs.includes(uid)) whereClause += " AND uid = " + uid; |
89021f18 BA |
86 | const query = |
87 | "DELETE FROM Problems " + | |
a9e79351 | 88 | whereClause; |
866842c3 | 89 | db.run(query); |
89021f18 BA |
90 | }); |
91 | }, | |
92 | } | |
93 | ||
94 | module.exports = ProblemModel; |