Add campaign model and endpoint

pull/6/head
Florian Hartwich 2017-08-12 16:28:18 +02:00
parent 9eb5b5cf03
commit 8319b79e1f
9 changed files with 100 additions and 13 deletions

View File

@ -1,18 +1,19 @@
const rootRoute = '/api';
module.exports = {
account: rootRoute + '/account',
auth: rootRoute + '/authenticate',
signUp: rootRoute + '/authenticate/signup',
awards: rootRoute + '/awardings',
command: rootRoute + '/cmd',
campaigns: rootRoute + '/campaigns',
cmdCreateSig: rootRoute + '/cmd/createSignature',
command: rootRoute + '/cmd',
decorations: rootRoute + '/decorations',
overview: rootRoute + '/overview',
ranks: rootRoute + '/ranks',
request: rootRoute + '/request',
signatures: '/signatures',
signUp: rootRoute + '/authenticate/signup',
squads: rootRoute + '/squads',
users: rootRoute + '/users',
account: rootRoute + '/account',
request: rootRoute + '/request',
wars: rootRoute + '/wars'
};

18
api/models/campaign.js Normal file
View File

@ -0,0 +1,18 @@
"use strict";
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CampaignSchema = new Schema({
title: {
type: String,
required: true
}
}, {
collection: 'campaign',
timestamps: {createdAt: 'timestamp'}
});
// optional more indices
CampaignSchema.index({timestamp: 1});
module.exports = mongoose.model('Campaign', CampaignSchema);

View File

@ -35,6 +35,10 @@ const WarSchema = new Schema({
get: v => Math.round(v),
set: v => Math.round(v),
default: 0
},
campaign: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Campaign'
}
}, {
collection: 'war',

43
api/routes/campaigns.js Normal file
View File

@ -0,0 +1,43 @@
"use strict";
// modules
const express = require('express');
const logger = require('debug')('cc:campaigns');
// 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 campaigns = express.Router();
// routes **********************
campaigns.route('/')
.post((req, res, next) => {
const campaign = new CampaignModel(req.body);
// timestamp and default are set automatically by Mongoose Schema Validation
campaign.save((err) => {
if (err) {
err.status = codes.wrongrequest;
err.message += ' in fields: ' + Object.getOwnPropertyNames(err.errors);
return next(err);
}
res.status(codes.created);
res.locals.items = campaign;
next();
});
})
.all(
routerHandling.httpMethodNotAllowed
);
// this middleware function can be used, if you like or remove it
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
campaigns.use(routerHandling.emptyResponse);
module.exports = campaigns;

View File

@ -18,6 +18,7 @@ const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
// Mongoose Model using mongoDB
const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war');
const PlayerModel = require('../models/player');
@ -26,20 +27,37 @@ const wars = express.Router();
// routes **********************
wars.route('/')
.get((req, res, next) => {
const filter = {};
WarModel.find(filter, {}, {sort: {date: 'desc'}}, (err, items) => {
let result = [];
CampaignModel.find({}, {}, {sort: {timestamp: 'desc'}}, (err, campaigns) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
if (items) {
res.locals.items = items;
} else {
res.locals.items = [];
if (campaigns) {
WarModel.find({}, {}, {sort: {date: 'desc'}}, (err, wars) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
if (wars) {
campaigns.forEach(campaign => {
let entry = {title: campaign.title, wars: []};
wars.forEach((war) => {
if (String(campaign._id) === String(war.campaign)) {
entry.wars.push(war);
}
});
result.push(entry);
});
res.locals.items = result;
}
res.locals.processed = true;
next();
}
)
;
}
res.locals.processed = true;
next();
});
})
})
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {

View File

@ -15,6 +15,7 @@ const urls = require('./config/api-url');
const restAPIchecks = require('./middleware/request-checks.js');
const errorResponseWare = require('./middleware/error-response');
const apiAuthenticationMiddleware = require('./middleware/auth-middleware');
const checkMT = require('./middleware/permission-check').checkMT;
const checkAdmin = require('./middleware/permission-check').checkAdmin;
const signatureCronJob = require('./cron-job/cron').cronJobSignature;
const backupCronJob = require('./cron-job/cron').cronJobBackup;
@ -32,6 +33,7 @@ const awardingRouter = require('./routes/awardings');
const requestRouter = require('./routes/request');
const signatureRouter = require('./routes/signatures');
const commandRouter = require('./routes/command');
const campaignRouter = require('./routes/campaigns');
const warRouter = require('./routes/wars');
// Configuration ***********************************
@ -79,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.campaigns, apiAuthenticationMiddleware, checkMT, campaignRouter);
app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter);
app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter);

BIN
static/src/assets/award.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB