From 8138926e1c15fed5dee93861467083ecadcc8f02 Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Sun, 1 Oct 2017 13:12:55 +0200 Subject: [PATCH] Add players endpoint --- api/config/api-url.js | 1 + api/package.json | 2 +- api/routes/players.js | 56 ++++++++++++++++++++++++++++++++++++ api/server.js | 2 ++ static/src/app/app.config.ts | 1 + 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 api/routes/players.js diff --git a/api/config/api-url.js b/api/config/api-url.js index 4e68b70..87a179e 100644 --- a/api/config/api-url.js +++ b/api/config/api-url.js @@ -9,6 +9,7 @@ module.exports = { command: rootRoute + '/cmd', decorations: rootRoute + '/decorations', overview: rootRoute + '/overview', + players: rootRoute + '/players', ranks: rootRoute + '/ranks', request: rootRoute + '/request', signatures: '/signatures', diff --git a/api/package.json b/api/package.json index 9cfdf82..b1c4c3f 100644 --- a/api/package.json +++ b/api/package.json @@ -17,7 +17,7 @@ "cors": "^2.8.3", "cron": "^1.2.1", "debug": "~2.2.0", - "express": "~4.13.1", + "express": "^4.16.1", "imagemin": "^5.2.2", "imagemin-pngquant": "^5.0.0", "jimp": "^0.2.27", diff --git a/api/routes/players.js b/api/routes/players.js new file mode 100644 index 0000000..c296f8d --- /dev/null +++ b/api/routes/players.js @@ -0,0 +1,56 @@ +"use strict"; + +// modules +const express = require('express'); +const logger = require('debug')('cc:wars'); + +// HTTP status codes by name +const codes = require('./http-codes'); + +const routerHandling = require('../middleware/router-handling'); + +// Mongoose Model using mongoDB +const CampaignModel = require('../models/campaign'); +const PlayerModel = require('../models/player'); +const WarModel = require('../models/war'); + +const campaignPlayer = express.Router(); + +// routes ********************** +campaignPlayer.route('/:campaignId/:playerName') + .get((req, res, next) => { + CampaignModel.findById(req.params.campaignId, (err, campaign) => { + if (err) return next(err); + WarModel.find({campaign: req.params.campaignId}, '_id', (err, wars) => { + if (err) return next(err); + const warIds = wars.map((obj) => { + return obj._id; + }); + PlayerModel.find({name: req.params.playerName, warId: {"$in": warIds}}) + .populate('warId') + .exec((err, items) => { + if (err) return next(err); + if (!items || items.length === 0) { + const err = new Error('unknown player name'); + err.status = codes.notfound; + return next(err) + } + res.locals.items = { + name: req.params.playerName, + campaign: campaign, + players: items + }; + next(); + }) + }) + }) + }) + + .all( + routerHandling.httpMethodNotAllowed + ); + + +campaignPlayer.use(routerHandling.emptyResponse); + +module.exports = campaignPlayer; diff --git a/api/server.js b/api/server.js index 9c203a9..b961de2 100644 --- a/api/server.js +++ b/api/server.js @@ -30,6 +30,7 @@ const rankRouter = require('./routes/ranks'); const decorationRouter = require('./routes/decorations'); const awardingRouter = require('./routes/awardings'); const requestRouter = require('./routes/request'); +const playerRouter = require('./routes/players'); const signatureRouter = require('./routes/signatures'); const commandRouter = require('./routes/command'); const campaignRouter = require('./routes/campaigns'); @@ -80,6 +81,7 @@ app.use(urls.decorations, decorationRouter); app.use(urls.request, requestRouter); app.use(urls.awards, awardingRouter); app.use(urls.wars, warRouter); +app.use(urls.players, playerRouter); app.use(urls.campaigns,campaignRouter); app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter); app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter); diff --git a/static/src/app/app.config.ts b/static/src/app/app.config.ts index 2eb172f..868fc3a 100644 --- a/static/src/app/app.config.ts +++ b/static/src/app/app.config.ts @@ -28,6 +28,7 @@ export const RouteConfig = { statsPath: 'stats', userPath: 'users', overviewPath: 'overview', + playersPath: 'players', request: 'request', requestAwardPath: 'award', requestPromotionPath: 'promotion',