1 // AJAX methods to get, create, update or delete a challenge
3 let router
= require("express").Router();
4 const access
= require("../utils/access");
5 const ChallengeModel
= require("../models/Challenge");
6 const UserModel
= require("../models/User"); //for name check
8 router
.delete("/testtest", access
.ajax
, (req
,res
) => {
10 ChallengeModel
.testfunc();
13 router
.post("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
14 const error
= ChallengeModel
.checkChallenge(req
.body
.chall
);
16 return res
.json({errmsg:error
});
19 fen: req
.body
.chall
.fen
,
20 timeControl: req
.body
.chall
.timeControl
,
21 vid: req
.body
.chall
.vid
,
23 nbPlayers: req
.body
.chall
.to
.length
,
25 ChallengeModel
.create(challenge
, (err
,lastId
) => {
28 if (!!req
.body
.chall
.to
[0])
30 UserModel
.getByName(req
.body
.chall
.to
, (err
,users
) => {
33 if (users
.length
< req
.body
.chall
.to
.length
)
34 return res
.json({errmsg: "Typo in player(s) name(s)"});
35 ChallengeModel
.initializeWillPlay(
41 res
.json({cid: lastId
["rowid"]});
47 res
.json({cid: lastId
["rowid"]});
52 //router.get("/challenges", access.logged, access.ajax, (req,res) => {
53 // if (req.query["uid"] != req.user._id)
54 // return res.json({errmsg: "Not your challenges"});
55 // let uid = ObjectID(req.query["uid"]);
56 // ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
57 // res.json(err || {challenges: challengeArray});
61 //function createChallenge(vid, from, to, res)
63 // ChallengeModel.create(vid, from, to, (err, chall) => {
65 // // A challenge can be sent using only name, thus 'to' is returned
72 router
.delete("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
73 const cid
= req
.query
.cid
;
74 ChallengeModel
.remove(cid
, req
.userId
, err
=> {
79 module
.exports
= router
;