Skip to content

Commit

Permalink
Merge branch 'MilosKozak-profile-api' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Mar 19, 2015
2 parents 0af24b6 + 90e2bfc commit 5eb51bc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
48 changes: 47 additions & 1 deletion lib/api/profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,59 @@ function configure (app, wares, profile) {
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));

// List settings available
// List profiles available
api.get('/profile/', function(req, res) {
profile.list(function (err, attribute) {
return res.json(attribute);
});
});

// List current active record (in current state LAST record is current active)
api.get('/profile/current', function(req, res) {
profile.last( function(err, records) {
return res.json(records.length > 0 ? records[0] : null);
});
});

function config_authed (app, api, wares, profile) {

// create new record
api.post('/profile/', wares.verifyAuthorization, function(req, res) {
var data = req.body;
profile.create(data, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
console.log('Error creating profile');
console.log(err);
} else {
res.json(created);
console.log('Profile created');
}

});
});

// update record
api.put('/profile/', wares.verifyAuthorization, function(req, res) {
var data = req.body;
profile.save(data, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
console.log('Error saving profile');
console.log(err);
} else {
res.json(created);
console.log('Profile saved');
}

});
});
}

if (app.enabled('api')) {
config_authed(app, api, wares, profile);
}

return api;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ function boot (env) {
ctx.pebble = require('./pebble');
console.info("Ensuring indexes");

console.log(ctx.entries, ctx.entries.indexedFields);
console.log(">>>>>", ctx.profiles, ctx.profiles.indexedFields);
store.ensureIndexes(ctx.entries( ), ctx.entries.indexedFields);
store.ensureIndexes(ctx.treatments( ), ctx.treatments.indexedFields);
store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);
store.ensureIndexes(ctx.profiles( ), ctx.profiles.indexedFields);

next( );
})
Expand Down
2 changes: 1 addition & 1 deletion lib/devicestatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function storage (collection, storage) {
api.indexedFields = indexedFields;
return api;
}

var indexedFields = ['created_at'];
storage.indexedFields = indexedFields;

module.exports = storage;
32 changes: 26 additions & 6 deletions lib/profile.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
'use strict';

function configure (collection, storage) {

function storage (collection, storage) {
var ObjectID = require('mongodb').ObjectID;

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
api( ).insert(obj, function (err, doc) {
api().insert(obj, function (err, doc) {
fn(null, doc);
});
}

function save (obj, fn) {
obj._id = new ObjectID(obj._id);
obj.created_at = (new Date( )).toISOString( );
api().save(obj, function (err, doc) {
fn(err, doc);
});
}

function list (fn) {
return api( ).find({ }).sort({created_at: -1}).toArray(fn);
return api( ).find({ }).sort({validfrom: -1}).toArray(fn);
}

function api ( ) {
return storage.pool.db.collection(collection);
function last (fn) {
return api().find().sort({validfrom: -1}).limit(1).toArray(fn);
}

function api () {
return storage.pool.db.collection(collection);
}

api.list = list;
api.create = create;
api.save = save;
api.last = last;
api.indexedFields = indexedFields;
return api;
}
module.exports = configure;

var indexedFields = ['validfrom'];

module.exports = storage;
2 changes: 1 addition & 1 deletion lib/treatments.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ function storage (collection, storage, pushover) {
api.indexedFields = indexedFields;
return api;
}

var indexedFields = ['created_at', 'eventType'];
storage.indexedFields = indexedFields;

module.exports = storage;

0 comments on commit 5eb51bc

Please sign in to comment.