opt-cc/server/routes/ranks.js

169 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
2017-05-10 11:04:06 +02:00
// modules
const fs = require('fs');
const express = require('express');
const multer = require('multer');
const storage = multer.memoryStorage();
const upload = multer({storage: storage});
// HTTP status codes by name
const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
2017-06-08 13:14:53 +02:00
const checkHl = require('../middleware/permission-check').checkHl;
2017-05-10 11:04:06 +02:00
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
const resourceLocation = require('../middleware/resource-location').resourceLocation().concat('/rank/');
2017-05-10 11:04:06 +02:00
// Mongoose Model using mongoDB
const RankModel = require('../models/rank');
2018-05-04 20:49:45 +02:00
// util
const genericGetById = require('./_generic').genericGetById;
const imageDimensionValidator = require('../middleware/validators').imageDimensionValidator;
const MAX_IMAGE_WIDTH = 120;
const MAX_IMAGE_HEIGHT = 120;
2018-05-04 20:49:45 +02:00
const ranks = new express.Router();
2017-05-10 11:04:06 +02:00
// routes **********************
ranks.route('/')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {};
if (req.query.fractFilter) {
2018-03-12 09:26:44 +01:00
filter.fraction = req.query.fractFilter.toUpperCase();
2018-02-26 09:04:27 +01:00
}
if (req.query.q) {
2018-03-12 09:26:44 +01:00
filter.name = {$regex: req.query.q, $options: 'i'};
2018-02-26 09:04:27 +01:00
}
RankModel.find(filter, {}, {sort: {fraction: 'asc', level: 'asc'}}, (err, items) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
if (items && items.length > 0) {
res.locals.items = items;
} else {
res.locals.items = [];
}
res.locals.processed = true;
next();
});
})
.post(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
if (req.file) {
const imageFileBuffer = req.file.buffer;
const err = imageDimensionValidator(imageFileBuffer, MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT);
2018-02-26 09:04:27 +01:00
if (err) {
return next(err);
2018-02-26 09:04:27 +01:00
}
const rank = new RankModel(req.body);
// timestamp and default are set automatically by Mongoose Schema Validation
rank.save((err) => {
if (err) {
err.status = codes.wrongrequest;
2018-03-31 00:14:30 +02:00
next(err);
}
res.status(codes.created);
res.locals.items = rank;
fs.appendFile(resourceLocation + rank._id + '.png', new Buffer(imageFileBuffer),
(err) => {
next(err);
});
});
} else {
const err = new Error('no image file provided');
err.status = codes.wrongmediasend;
next(err);
}
2018-02-26 09:04:27 +01:00
})
.all(
routerHandling.httpMethodNotAllowed
);
2017-05-10 11:04:06 +02:00
ranks.route('/:id')
2018-02-26 09:04:27 +01:00
.get(idValidator, (req, res, next) => {
2018-05-04 20:49:45 +02:00
return genericGetById(req, res, next, RankModel);
2018-02-26 09:04:27 +01:00
})
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
const err = new Error('id of PATCH resource and send JSON body are not equal ' + req.params.id + ' ' +
req.body._id);
2018-02-26 09:04:27 +01:00
err.status = codes.notfound;
next(err);
return; // prevent node to process this function further after next() has finished.
}
// optional task 3: increment version manually as we do not use .save(.)
req.body.updatedAt = new Date();
req.body.$inc = {__v: 1};
if (req.file) {
const imageFileBuffer = req.file.buffer;
const err = imageDimensionValidator(imageFileBuffer, MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT);
if (err) {
return next(err);
}
2018-03-31 00:14:30 +02:00
const file = resourceLocation + req.params.id + '.png';
2018-02-26 09:04:27 +01:00
fs.unlink(file, (err) => {
if (err) next(err);
fs.appendFile(file, new Buffer(imageFileBuffer),
2018-02-26 09:04:27 +01:00
(err) => {
if (err) next(err);
});
});
}
// PATCH is easier with mongoose than PUT. You simply update by all data that comes from outside. no need to
// reset attributes that are missing.
RankModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
2018-03-12 09:26:44 +01:00
} else if (!item) {
err = new Error('item not found');
2018-02-26 09:04:27 +01:00
err.status = codes.notfound;
2018-03-12 09:26:44 +01:00
} else {
2018-02-26 09:04:27 +01:00
res.locals.items = item;
}
next(err);
2018-03-12 09:26:44 +01:00
});
2018-02-26 09:04:27 +01:00
})
.delete(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
RankModel.findByIdAndRemove(req.params.id, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
2018-03-12 09:26:44 +01:00
} else if (!item) {
err = new Error('item not found');
2018-02-26 09:04:27 +01:00
err.status = codes.notfound;
}
// we don't set res.locals.items and thus it will send a 204 (no content) at the end. see last handler
// user.use(..)
res.locals.processed = true;
next(err); // this works because err is in normal case undefined and that is the same as no parameter
2018-03-12 09:26:44 +01:00
});
2018-02-26 09:04:27 +01:00
})
.all(
routerHandling.httpMethodNotAllowed
);
2017-05-10 11:04:06 +02:00
// 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
ranks.use(routerHandling.emptyResponse);
module.exports = ranks;