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