1 let router
= require("express").Router();
2 const access
= require("../utils/access");
3 const ChallengeModel
= require("../models/Challenge");
4 const UserModel
= require("../models/User"); //for name check
5 const params
= require("../config/parameters");
7 router
.post("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
8 if (ChallengeModel
.checkChallenge(req
.body
.chall
))
12 fen: req
.body
.chall
.fen
,
13 cadence: req
.body
.chall
.cadence
,
14 randomness: req
.body
.chall
.randomness
,
15 vid: req
.body
.chall
.vid
,
17 to: req
.body
.chall
.to
, //string: user name (may be empty)
19 const insertChallenge
= () => {
20 ChallengeModel
.create(challenge
, (err
,ret
) => {
21 res
.json(err
|| {cid:ret
.cid
});
24 if (req
.body
.chall
.to
)
26 UserModel
.getOne("name", challenge
.to
, (err
,user
) => {
28 res
.json(err
|| {errmsg: "Typo in player name"});
31 challenge
.to
= user
.id
; //ready now to insert challenge
36 "New challenge: " + params
.siteURL
+ "/#/?disp=corr");
45 router
.get("/challenges", access
.ajax
, (req
,res
) => {
46 const uid
= req
.query
.uid
;
47 if (uid
.match(/^[0-9]+$/))
49 ChallengeModel
.getByUser(uid
, (err
,challenges
) => {
50 res
.json(err
|| {challenges:challenges
});
55 router
.delete("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
56 const cid
= req
.query
.id
;
57 if (cid
.match(/^[0-9]+$/))
59 ChallengeModel
.safeRemove(cid
, req
.userId
);
64 module
.exports
= router
;