diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/db/index.js | 16 | ||||
| -rw-r--r-- | lib/index.js | 9 |
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/db/index.js b/lib/db/index.js index 33beae9..b7b1b06 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -11,3 +11,19 @@ var Image = db.Image = bookshelf.Model.extend({ tableName: 'images', hasTimestamps: false, }) + +db.getImage = function(id) { + var model = new Image({'id': id}) + return model.fetch() +} +db.getLatest = function () { + return Image.query(function(qb){ + qb.orderBy("id", "desc").limit(1) + }).fetch() +} +db.getRandom = function () { + return Image.query(function(qb){ + qb.orderBy(knex.raw('RANDOM()')).limit(1) + }).fetch() +} + diff --git a/lib/index.js b/lib/index.js index 0cc00a9..fd226e5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -38,9 +38,18 @@ site.init = function(){ app.get("/p/:id", function(req, res){}) app.get("/get/random", function(req, res){ + db.getRandom().then(function(img){ + res.json(img) + }) }) app.get("/get/latest", function(req, res){ + db.getLatest().then(function(img){ + res.json(img) + }) }) app.get("/get/:id", function(req, res){ + db.getImage(id).then(function(img){ + res.json(img) + }) }) }
\ No newline at end of file |
