91 lines
2.7 KiB
JavaScript
91 lines
2.7 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
// modules
|
||
|
const express = require('express');
|
||
|
const logger = require('debug')('cc:awardings');
|
||
|
|
||
|
// HTTP status codes by name
|
||
|
const codes = require('./http-codes');
|
||
|
|
||
|
const routerHandling = require('../middleware/router-handling');
|
||
|
|
||
|
// Mongoose Model using mongoDB
|
||
|
const AppUserModel = require('../models/app-user');
|
||
|
|
||
|
const account = express.Router();
|
||
|
|
||
|
|
||
|
account.route('/')
|
||
|
.get((req, res, next) => {
|
||
|
AppUserModel.find({}).populate('squad').exec((err, items) => {
|
||
|
if (err) {
|
||
|
err.status = codes.servererror;
|
||
|
return next(err);
|
||
|
}
|
||
|
res.locals.items = items;
|
||
|
res.locals.processed = true;
|
||
|
next();
|
||
|
})
|
||
|
})
|
||
|
.all(
|
||
|
routerHandling.httpMethodNotAllowed
|
||
|
);
|
||
|
|
||
|
// routes **********************
|
||
|
account.route('/:id')
|
||
|
.patch((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);
|
||
|
err.status = codes.notfound;
|
||
|
next(err);
|
||
|
return; // prevent node to process this function further after next() has finished.
|
||
|
}
|
||
|
|
||
|
// increment version manually as we do not use .save(.)
|
||
|
req.body.updatedAt = new Date();
|
||
|
req.body.$inc = {__v: 1};
|
||
|
|
||
|
// 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.
|
||
|
AppUserModel.findByIdAndUpdate(req.params.id, req.body, {new: true}).populate('squad').exec((err, item) => {
|
||
|
if (err) {
|
||
|
err.status = codes.wrongrequest;
|
||
|
}
|
||
|
else if (!item) {
|
||
|
err = new Error("appUser not found");
|
||
|
err.status = codes.notfound;
|
||
|
}
|
||
|
else {
|
||
|
res.locals.items = item;
|
||
|
}
|
||
|
next(err);
|
||
|
})
|
||
|
})
|
||
|
|
||
|
.delete((req, res, next) => {
|
||
|
AppUserModel.findByIdAndRemove(req.params.id, (err, item) => {
|
||
|
if (err) {
|
||
|
err.status = codes.wrongrequest;
|
||
|
}
|
||
|
else if (!item) {
|
||
|
err = new Error("item not found");
|
||
|
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
|
||
|
});
|
||
|
})
|
||
|
|
||
|
.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
|
||
|
account.use(routerHandling.emptyResponse);
|
||
|
|
||
|
|
||
|
module.exports = account;
|