Skip to content

Commit

Permalink
Merge pull request #1264 from MilosKozak/mc/boluscalc
Browse files Browse the repository at this point in the history
Mc/boluscalc
  • Loading branch information
MilosKozak committed Oct 25, 2015
2 parents aad93cc + a8a0a6c commit 17a8e90
Show file tree
Hide file tree
Showing 23 changed files with 2,135 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `MONGO_COLLECTION` (`entries`) - The collection used to store SGV, MBG, and CAL records from your CGM device
* `MONGO_TREATMENTS_COLLECTION` (`treatments`) -The collection used to store treatments entered in the Care Portal, see the `ENABLE` env var above
* `MONGO_DEVICESTATUS_COLLECTION`(`devicestatus`) - The collection used to store device status information such as uploader battery
* `MONGO_PROFILE_COLLECTION`(`profile`) - The collection used to store your profiles
* `MONGO_FOOD_COLLECTION`(`food`) - The collection used to store your food database
* `PORT` (`1337`) - The port that the node.js application will listen on.
* `SSL_KEY` - Path to your ssl key file, so that ssl(https) can be enabled directly in node.js
* `SSL_CERT` - Path to your ssl cert file, so that ssl(https) can be enabled directly in node.js
Expand Down
1 change: 1 addition & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function setMongo() {
env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');
env.food_collection = readENV('MONGO_FOOD_COLLECTION', 'food');

// TODO: clean up a bit
// Some people prefer to use a json configuration file instead.
Expand Down
84 changes: 84 additions & 0 deletions lib/api/food/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

var consts = require('../../constants');

function configure (app, wares, ctx) {
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 foods available
api.get('/food/', function(req, res) {
ctx.food.list(function (err, attribute) {
return res.json(attribute);
});
});

api.get('/food/quickpicks', function(req, res) {
ctx.food.listquickpicks(function (err, attribute) {
return res.json(attribute);
});
});

api.get('/food/regular', function(req, res) {
ctx.food.listregular(function (err, attribute) {
return res.json(attribute);
});
});

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

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

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

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

return api;
}

module.exports = configure;

2 changes: 1 addition & 1 deletion lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function create (env, ctx) {
app.use('/', require('./devicestatus/')(app, wares, ctx));
app.use('/', require('./notifications-api')(app, wares, ctx));
app.use('/', require('./verifyauth')(app, env));

app.use('/', require('./food/')(app, wares, ctx));
// Status
app.use('/', require('./status')(app, wares, env));
return app;
Expand Down
1 change: 1 addition & 0 deletions lib/api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function configure (app, wares, env) {
, serverTime: new Date().toISOString()
, apiEnabled: app.enabled('api')
, careportalEnabled: app.enabled('api') && env.settings.enable.indexOf('careportal') > -1
, boluscalcEnabled: app.enabled('api') && env.settings.enable.indexOf('boluscalc') > -1
, head: wares.get_head( )
, settings: env.settings
, extendedSettings: app.extendedClientSettings
Expand Down
2 changes: 2 additions & 0 deletions lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function boot (env) {
ctx.treatments = require('./treatments')(env, ctx);
ctx.devicestatus = require('./devicestatus')(env.devicestatus_collection, ctx);
ctx.profile = require('./profile')(env.profile_collection, ctx);
ctx.food = require('./food')(env, ctx);
ctx.pebble = require('./pebble')(env, ctx);
ctx.bus = require('./bus')(env, ctx);
ctx.data = require('./data')(env, ctx);
Expand All @@ -44,6 +45,7 @@ function boot (env) {
ctx.store.ensureIndexes(ctx.treatments( ), ctx.treatments.indexedFields);
ctx.store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);
ctx.store.ensureIndexes(ctx.profile( ), ctx.profile.indexedFields);
ctx.store.ensureIndexes(ctx.food( ), ctx.food.indexedFields);

next( );
}
Expand Down

0 comments on commit 17a8e90

Please sign in to comment.