mirror of
https://gitlab.silvrtree.co.uk/martind2000/censis-archive.git
synced 2025-01-30 17:30:14 +00:00
114 lines
3.0 KiB
JavaScript
114 lines
3.0 KiB
JavaScript
/*jshint node:true*/
|
|
'use strict';
|
|
var db = require('../units/db-connector').dbConnection;
|
|
var dbAccounts = require('../units/db-accounts')(db);
|
|
module.exports = function(app) {
|
|
var express = require('express');
|
|
var profilesRouter = express.Router();
|
|
var moment = require('moment');
|
|
|
|
profilesRouter.get('/:uid', function(req, res) {
|
|
if (/Bearer .+/.test(req.headers.authorization)) {
|
|
|
|
dbAccounts.sqlGetProfile(req.params.uid)
|
|
.then(function(data) {
|
|
let response = {data: {}};
|
|
if (data === null) {
|
|
// No record yet, return an empty one.
|
|
response.data = {
|
|
type: 'profile', id: req.params.uid, attributes: {
|
|
forename: '', surname: '', gender: 0, dob: '', bio: ''
|
|
|
|
}};
|
|
} else {
|
|
response.data = {
|
|
type: 'profile', id: data.uid, attributes: {
|
|
forename: data.forename,
|
|
surname: data.surname,
|
|
gender: data.gender,
|
|
dob: moment(data.dob).format('YYYY-MM-DD'),
|
|
bio: data.bio
|
|
}
|
|
};
|
|
|
|
}
|
|
res.status(200).send(response);
|
|
})
|
|
.catch(function(e) {
|
|
'use strict';
|
|
console.error(e);
|
|
res.status(401).end();
|
|
});
|
|
};
|
|
});
|
|
|
|
profilesRouter.post('/', function(req, res) {
|
|
res.status(201).end();
|
|
});
|
|
|
|
profilesRouter.get('/:id', function(req, res) {
|
|
res.send({
|
|
profiles: {
|
|
id: req.params.id
|
|
}
|
|
});
|
|
});
|
|
|
|
profilesRouter.put('/:id', function(req, res) {
|
|
res.send({
|
|
profiles: {
|
|
id: req.params.id
|
|
}
|
|
});
|
|
});
|
|
|
|
profilesRouter.patch('/:id', function(req, res) {
|
|
var data = req.body.data;
|
|
var attr = data.attributes;
|
|
var updateData = {
|
|
uid: data.id,
|
|
forename: attr.forename,
|
|
surname: attr.surname,
|
|
gender: attr.gender,
|
|
dob: attr.dob,
|
|
bio: attr.bio
|
|
};
|
|
dbAccounts.addInsertProfile(updateData)
|
|
.then(function() {
|
|
let response = {
|
|
data: {
|
|
type: 'profile', id: req.params.id, attributes: data.attributes
|
|
}
|
|
};
|
|
|
|
res.status(200).send(response);
|
|
})
|
|
.catch(function(err) {
|
|
'use strict';
|
|
console.error(err);
|
|
res.status(401).end();
|
|
});
|
|
|
|
});
|
|
|
|
profilesRouter.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
|
|
|
|
// After installing, you need to `use` the body-parser for
|
|
// this mock uncommenting the following line:
|
|
//
|
|
//App.use('/api/profiles', require('body-parser').json());
|
|
app.use('/api/profiles', require('body-parser').json());
|
|
app.use('/api/profiles',
|
|
require('body-parser').json({type: 'application/vnd.api+json'}));
|
|
|
|
app.use('/api/profiles', profilesRouter);
|
|
};
|