1 var db
= require("../utils/database");
8 * target: recipient id (optional)
9 * vid: variant id (int)
10 * fen: varchar (optional)
11 * timeControl: string (3m+2s, 7d+1d ...)
14 const ChallengeModel
=
16 checkChallenge: function(c
)
18 if (!c
.vid
.match(/^[0-9]+$/))
19 return "Wrong variant ID";
21 if (!c
.timeControl
.match(/^[0-9dhms
+]+$/))
22 return "Wrong characters in time control";
24 if (!c
.fen
.match(/^[a-zA-Z0-9, /-]+$/))
25 return "Bad FEN string";
28 // fen cannot be undefined
29 create: function(c
, cb
)
31 db
.serialize(function() {
33 "INSERT INTO Challenges " +
34 "(added, uid, " + (!!c
.to
? "target, " : "") +
35 "vid, fen, timeControl) VALUES " +
36 "(" + Date
.now() + "," + c
.uid
+ "," + (!!c
.to
? c
.to
+ "," : "") +
37 c
.vid
+ ",'" + c
.fen
+ "','" + c
.timeControl
+ "')";
38 db
.run(query
, err
=> {
44 getOne: function(id
, cb
)
46 db
.serialize(function() {
51 db
.get(query
, (err
,challenge
) => {
52 return cb(err
, challenge
);
57 // all challenges except where target is defined and not me
58 getByUser: function(uid
, cb
)
60 db
.serialize(function() {
64 "WHERE target IS NULL OR target = " + uid
;
65 db
.run(query
, (err
,challenges
) => {
66 return cb(err
, challenges
);
71 remove: function(id
, uid
)
73 db
.serialize(function() {
77 "WHERE id = " + id
+ " AND uid = " + uid
;
78 db
.run(query
, (err
,rows
) => {
80 return res
.json({errmsg: "Not your challenge"});
82 "DELETE FROM Challenges " +
90 module
.exports
= ChallengeModel
;