44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
|
"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;
|