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