forgot to git add some profile api files

This commit is contained in:
Jason Calabrese
2015-02-25 22:45:53 -08:00
committed by Jason Calabrese
parent d616b6c074
commit ea1dbccdb8
2 changed files with 71 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
'use strict';
var consts = require('../../constants');
function configure (app, wares, profile) {
var express = require('express'),
api = express.Router( );
// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw( ));
// json body types get handled as parsed json
api.use(wares.bodyParser.json( ));
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));
// List settings available
api.get('/profile/', function(req, res) {
profile.list(function (err, attribute) {
return res.json(attribute);
});
});
function config_authed (app, api, wares, profile) {
api.post('/profile/', /*TODO: auth disabled for now, need to get login figured out... wares.verifyAuthorization, */ function(req, res) {
var attribute = req.body;
profile.create(attribute, function (err, created) {
if (err)
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
res.json(created);
});
});
}
if (app.enabled('api')) {
config_authed(app, api, wares, profile);
}
return api;
}
module.exports = configure;
+24
View File
@@ -0,0 +1,24 @@
'use strict';
function configure (collection, storage) {
function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
api( ).insert(obj, function (err, doc) {
fn(null, doc);
});
}
function list (fn) {
return api( ).find({ }).sort({created_at: -1}).toArray(fn);
}
function api ( ) {
return storage.pool.db.collection(collection);
}
api.list = list;
api.create = create;
return api;
}
module.exports = configure;