censis-obrand/dev-archive/censis/obrand/obrand-admin/server/mocks/companies.js
2016-03-29 12:15:56 +01:00

87 lines
2.3 KiB
JavaScript

/*jshint node:true*/
'use strict';
var db = require('../units/db-connector').dbConnection;
var dbCompany = require('../units/db-company')(db);
var $U = require('md-utils');
module.exports = function(app) {
var express = require('express');
var companiesRouter = express.Router();
companiesRouter.get('/:id', function(req, res) {
var id = req.params.id;
dbCompany.sqlGetSimpleCompany(id)
.then(function(data) {
const response = {
data: {
type: 'company', id: id, attributes: $U.reDashObject(data)
}
};
console.log(response);
res.status(200).send(response);
})
.catch(function(err) {
console.error(err);
res.status(401).end();
});
});
companiesRouter.post('/', function(req, res) {
res.status(201).end();
});
companiesRouter.get('/:id', function(req, res) {
res.send({
companies: {
id: req.params.id
}
});
});
companiesRouter.patch('/:id', function(req, res) {
if (/Bearer .+/.test(req.headers.authorization)) {
console.log('Patching: ' + req.params.id);
console.log(req.body.data);
var updateData = $U.unDashObject(req.body.data.attributes);
dbCompany.addNewCompany(updateData)
.then(function(d) {
let response = {
data: {
type: 'company', id: d.cid, attributes: d
}
};
res.status(200).send(response);
})
.catch(function(e) {
console.error(e);
res.status(401).end();
});
}
});
companiesRouter.delete('/:id', function(req, res) {
res.status(204).end();
});
// The POST and PUT call will not contain a request body
// because the body-parser is not included by default.
// To use req.body, run:
// Npm install --save-dev body-parser
app.use('/api/companies', require('body-parser').json({type: 'application/vnd.api+json'}));
// After installing, you need to `use` the body-parser for
// this mock uncommenting the following line:
//
//App.use('/api/companies', require('body-parser').json());
app.use('/api/companies', companiesRouter);
};