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
.get("/challenges", (req
,res
) => {
9 ChallengeModel
.getByUser(req
.query
["uid"], (err
,challenges
) => {
10 res
.json(err
|| {challenges:challenges
});
14 router
.post("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
15 const error
= ChallengeModel
.checkChallenge(req
.body
.chall
);
17 return res
.json({errmsg:error
});
20 fen: req
.body
.chall
.fen
,
21 timeControl: req
.body
.chall
.timeControl
,
22 vid: req
.body
.chall
.vid
,
24 to: req
.body
.chall
.to
, //string: user name (may be empty)
26 const insertChallenge
= () => {
27 ChallengeModel
.create(challenge
, (err
) => {
32 if (!!req
.body
.chall
.to
)
34 UserModel
.getOne("name", challenge
.to
, (err
,user
) => {
36 return res
.json(err
| {errmsg: "Typo in player name"});
37 challenge
.to
= user
.id
; //ready now to insert challenge
45 // TODO: either like that, or remove challenge in /games POST ?
46 // "Challenge update" --> someone accepted a challenge
47 router
.put("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
48 // launchGame(cid, uid) //req.body.chall
49 // TODO: gather challenge infos
50 // Then create game, and remove challenge:
51 ChallengeModel
.remove(cid
, req
.userId
, err
=> {
56 router
.delete("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
57 const cid
= req
.query
.id
;
58 ChallengeModel
.remove(cid
, req
.userId
, err
=> {
59 res
.json(err
|| {}); //TODO: just "return err" because is empty if no errors
63 module
.exports
= router
;