diff --git a/lib/api/entries/index.js b/lib/api/entries/index.js index 79642cb0f..0ae412cdb 100644 --- a/lib/api/entries/index.js +++ b/lib/api/entries/index.js @@ -142,14 +142,13 @@ function configure (app, wares, entries) { } // Fetch one entry by id - api.get('/entries/:id', function(req, res) { - entries.getEntry(function(err, entry) { - if (err) - res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); - else - return res.json(entry); - }, req.params.id); - }); + api.get('/entries/:id', function(req, res, next) { + entries.getEntry(req.params.id, function(err, entry) { + res.entries = [entry]; + res.entries_err = err; + next() + }); + }, format_entries); return api; diff --git a/lib/entries.js b/lib/entries.js index 77d5a4542..1820c340a 100644 --- a/lib/entries.js +++ b/lib/entries.js @@ -2,6 +2,7 @@ var es = require('event-stream'); var sgvdata = require('sgvdata'); +var ObjectID = require('mongodb').ObjectID; var TEN_MINS = 10 * 60 * 1000; @@ -152,8 +153,7 @@ function storage(name, storage, pushover) { } } - function getEntry(fn, id) { - console.info("trying to find entry for id: " + id); + function getEntry(id, fn) { with_collection(function(err, collection) { if (err) fn(err); @@ -167,20 +167,6 @@ function storage(name, storage, pushover) { }); } - function getEntries(fn, count) { - with_collection(function(err, collection) { - if (err) - fn(err); - else - collection.find({ }).sort({"date": -1}).limit(count).toArray(function (err, entries) { - if (err) - fn(err); - else - fn(null, entries); - }); - }); - } - // closure to represent the API function api ( ) { // obtain handle usable for querying the collection associated @@ -194,7 +180,6 @@ function storage(name, storage, pushover) { api.map = map; api.create = create; api.persist = persist; - api.getEntries = getEntries; api.getEntry = getEntry; api.indexedFields = indexedFields; return api; diff --git a/tests/api.entries.test.js b/tests/api.entries.test.js index f3e368da7..b65ec2200 100644 --- a/tests/api.entries.test.js +++ b/tests/api.entries.test.js @@ -66,6 +66,22 @@ describe('Entries REST api', function ( ) { }); }); + it('/entries/ID', function (done) { + var app = this.app; + this.archive.list({count: 1}, function(err, records) { + var currentId = records.pop()._id.toString(); + request(app) + .get('/entries/'+currentId+'.json') + .expect(200) + .end(function (err, res) { + res.body.length.should.equal(1); + res.body[0]._id.should.equal(currentId); + done( ); + }); + }); + + }); + it('/entries/preview', function (done) { request(this.app) .post('/entries/preview.json') diff --git a/tests/fixtures/example.json b/tests/fixtures/example.json index aa3309379..0f3b6802c 100644 --- a/tests/fixtures/example.json +++ b/tests/fixtures/example.json @@ -1,5 +1,6 @@ [ { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:49:15 AM", "date": 1405792155000, @@ -7,6 +8,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:44:15 AM", "date": 1405791855000, @@ -14,6 +16,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:39:15 AM", "date": 1405791555000, @@ -21,6 +24,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:34:15 AM", "date": 1405791255000, @@ -28,6 +32,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:29:15 AM", "date": 1405790955000, @@ -35,6 +40,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:24:15 AM", "date": 1405790655000, @@ -42,6 +48,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:19:15 AM", "date": 1405790355000, @@ -49,6 +56,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:14:15 AM", "date": 1405790055000, @@ -56,6 +64,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:09:15 AM", "date": 1405789755000, @@ -63,6 +72,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 10:04:15 AM", "date": 1405789455000, @@ -70,6 +80,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 09:59:15 AM", "date": 1405789155000, @@ -77,6 +88,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "5", "dateString": "07\/19\/2014 09:54:15 AM", "date": 1405788855000, @@ -84,6 +96,7 @@ "direction": "NOT COMPUTABLE" }, { + "type": "sgv", "sgv": "178", "dateString": "07\/19\/2014 03:59:15 AM", "date": 1405767555000, @@ -91,6 +104,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "179", "dateString": "07\/19\/2014 03:54:15 AM", "date": 1405767255000, @@ -98,6 +112,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "178", "dateString": "07\/19\/2014 03:49:15 AM", "date": 1405766955000, @@ -105,6 +120,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "177", "dateString": "07\/19\/2014 03:44:15 AM", "date": 1405766655000, @@ -112,6 +128,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "176", "dateString": "07\/19\/2014 03:39:15 AM", "date": 1405766355000, @@ -119,6 +136,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "176", "dateString": "07\/19\/2014 03:34:15 AM", "date": 1405766055000, @@ -126,6 +144,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "175", "dateString": "07\/19\/2014 03:29:16 AM", "date": 1405765756000, @@ -133,6 +152,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "174", "dateString": "07\/19\/2014 03:24:15 AM", "date": 1405765455000, @@ -140,6 +160,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "174", "dateString": "07\/19\/2014 03:19:15 AM", "date": 1405765155000, @@ -147,6 +168,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "175", "dateString": "07\/19\/2014 03:14:15 AM", "date": 1405764855000, @@ -154,6 +176,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "176", "dateString": "07\/19\/2014 03:09:15 AM", "date": 1405764555000, @@ -161,6 +184,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "176", "dateString": "07\/19\/2014 03:04:15 AM", "date": 1405764255000, @@ -168,6 +192,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "173", "dateString": "07\/19\/2014 02:59:15 AM", "date": 1405763955000, @@ -175,6 +200,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "171", "dateString": "07\/19\/2014 02:54:15 AM", "date": 1405763655000, @@ -182,6 +208,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "170", "dateString": "07\/19\/2014 02:49:15 AM", "date": 1405763355000, @@ -189,6 +216,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "171", "dateString": "07\/19\/2014 02:44:15 AM", "date": 1405763055000, @@ -196,6 +224,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "169", "dateString": "07\/19\/2014 02:39:15 AM", "date": 1405762755000, @@ -203,6 +232,7 @@ "direction": "Flat" }, { + "type": "sgv", "sgv": "169", "dateString": "07\/19\/2014 02:34:15 AM", "date": 1405762455000,