summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-09-24 12:33:51 -0400
committerJules Laplace <jules@okfoc.us>2015-09-24 12:33:51 -0400
commitb817d4472ca905a0d380cf686ffa62f62297574a (patch)
treee97c895a4d9275069e7ac5fda37dd899addae63a /lib
parentf2215b36f3e1844ac058e7e7cfef6a53083efef9 (diff)
db calls
Diffstat (limited to 'lib')
-rw-r--r--lib/db/index.js16
-rw-r--r--lib/index.js9
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