- getOne: function(id, cb)
- {
- db.serialize(function() {
- let query =
- "SELECT * " +
- "FROM Challenges " +
- "WHERE id = " + id;
- db.get(query, (err,challengeInfo) => {
- if (!!err)
- return cb(err);
- let condition = "";
- if (!!challengeInfo.to)
- condition = "IN (" + challengeInfo.uid + "," + challengeInfo.to + ")";
- else
- condition = "= " + challengeInfo.uid;
- query =
- "SELECT id, name " +
- "FROM Users " +
- "WHERE id " + condition;
- db.run(query, (err2,players) => {
- if (!!err2)
- return cb(err2);
- const challenge = {
- id: id,
- uid: challengeInfo.uid, //sender (but we don't know who ask)
- vid: challengeInfo.vid,
- added: challengeInfo.added,
- players: players, //sender + potential receiver
- fen: challengeInfo.fen,
- timeControl: challengeInfo.timeControl,
- };
- return cb(null, challenge);
- });
- });
- });
- },
+ // All challenges related to user with ID uid
+ getByUser: function(uid, cb) {
+ db.serialize(function() {
+ const query =
+ "SELECT * " +
+ "FROM Challenges " +
+ "WHERE target IS NULL" +
+ " OR uid = " + uid +
+ " OR target = " + uid;
+ db.all(query, (err, challenges) => {
+ cb(err, challenges);
+ });
+ });
+ },