Add debug trace to understand why corr move 1 isn't recorded
[vchess.git] / server / models / Problem.js
CommitLineData
c5c47010 1const 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
14const ProblemModel =
15{
16 checkProblem: function(p)
17 {
866842c3
BA
18 return (
19 p.id.toString().match(/^[0-9]+$/) &&
20 p.vid.toString().match(/^[0-9]+$/) &&
21 p.fen.match(/^[a-zA-Z0-9, /-]*$/)
22 );
89021f18
BA
23 },
24
25 create: function(p, cb)
26 {
27 db.serialize(function() {
28 const query =
29 "INSERT INTO Problems " +
30 "(added, uid, vid, fen, instruction, solution) " +
31 "VALUES " +
604b951e
BA
32 "(" + Date.now() + "," + p.uid + "," + p.vid + ",'" + p.fen + "',?,?)";
33 db.run(query, [p.instruction,p.solution], function(err) {
866842c3 34 cb(err, {pid: this.lastID});
89021f18
BA
35 });
36 });
37 },
38
39 getAll: function(cb)
40 {
41 db.serialize(function() {
42 const query =
43 "SELECT * " +
44 "FROM Problems";
45 db.all(query, (err,problems) => {
866842c3 46 cb(err, problems);
89021f18
BA
47 });
48 });
49 },
50
51 getOne: function(id, cb)
52 {
53 db.serialize(function() {
54 const query =
55 "SELECT * " +
56 "FROM Problems " +
57 "WHERE id = " + id;
58 db.get(query, (err,problem) => {
866842c3 59 cb(err, problem);
89021f18
BA
60 });
61 });
62 },
63
866842c3 64 safeUpdate: function(prob, uid)
89021f18
BA
65 {
66 db.serialize(function() {
866842c3 67 const query =
89021f18
BA
68 "UPDATE Problems " +
69 "SET " +
70 "vid = " + prob.vid + "," +
604b951e
BA
71 "fen = '" + prob.fen + "'," +
72 "instruction = ?," +
73 "solution = ? " +
866842c3
BA
74 "WHERE id = " + prob.id + " AND uid = " + uid;
75 db.run(query, [prob.instruction,prob.solution]);
89021f18
BA
76 });
77 },
78
866842c3 79 safeRemove: function(id, uid)
89021f18
BA
80 {
81 db.serialize(function() {
82 const query =
83 "DELETE FROM Problems " +
89021f18 84 "WHERE id = " + id + " AND uid = " + uid;
866842c3 85 db.run(query);
89021f18
BA
86 });
87 },
88}
89
90module.exports = ProblemModel;