Step toward a one-page application
[vchess.git] / routes / challenge.js
1 // AJAX methods to get, create, update or delete a challenge
2
3 let router = require("express").Router();
4 const access = require("../utils/access");
5 const ChallengeModel = require("../models/Challenge");
6 const checkChallenge = require("../public/javascripts/shared/challengeCheck.js");
7
8 router.post("/challenges/:vid([0-9]+)", access.logged, access.ajax, (req,res) => {
9 const vid = req.params["vid"];
10 const chall = {
11 uid: req.userId,
12 vid: vid,
13 fen: req.body["fen"],
14 mainTime: req.body["mainTime"],
15 increment: req.body["increment"],
16 nbPlayers: req.body["nbPlayers"],
17 players: req.body["players"],
18 };
19 const error = checkChallenge(chall);
20 ChallengeModel.create(chall, (err,lastId) => {
21 res.json(err || {cid: lastId["rowid"]});
22 });
23 });
24
25 //// index
26 //router.get("/challengesbyplayer", access.logged, access.ajax, (req,res) => {
27 // if (req.query["uid"] != req.user._id)
28 // return res.json({errmsg: "Not your challenges"});
29 // let uid = ObjectID(req.query["uid"]);
30 // ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
31 // res.json(err || {challenges: challengeArray});
32 // });
33 //});
34 //
35 //function createChallenge(vid, from, to, res)
36 //{
37 // ChallengeModel.create(vid, from, to, (err, chall) => {
38 // res.json(err || {
39 // // A challenge can be sent using only name, thus 'to' is returned
40 // to: chall.to,
41 // cid: chall._id
42 // });
43 // });
44 //}
45 //
46 //// from[, to][,nameTo]
47 //router.post("/challenges", access.logged, access.ajax, (req,res) => {
48 // if (req.body.from != req.user._id)
49 // return res.json({errmsg: "Identity usurpation"});
50 // let from = ObjectID(req.body.from);
51 // let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
52 // let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
53 // let vid = ObjectID(req.body.vid);
54 // if (!to && !!nameTo)
55 // {
56 // UserModel.getByName(nameTo, (err,user) => {
57 // access.checkRequest(res, err, user, "Opponent not found", () => {
58 // createChallenge(vid, from, user._id, res);
59 // });
60 // });
61 // }
62 // else if (!!to)
63 // createChallenge(vid, from, to, res);
64 // else
65 // createChallenge(vid, from, undefined, res); //automatch
66 //});
67 //
68 //router.delete("/challenges", access.logged, access.ajax, (req,res) => {
69 // let cid = ObjectID(req.query.cid);
70 // ChallengeModel.getById(cid, (err,chall) => {
71 // access.checkRequest(res, err, chall, "Challenge not found", () => {
72 // if (!chall.from.equals(req.user._id) && !!chall.to && !chall.to.equals(req.user._id))
73 // return res.json({errmsg: "Not your challenge"});
74 // ChallengeModel.remove(cid, err => {
75 // res.json(err || {});
76 // });
77 // });
78 // });
79 //});
80
81 module.exports = router;