summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-09-04 23:24:31 -0400
committerJules Laplace <jules@okfoc.us>2015-09-04 23:24:31 -0400
commitb2d2bc1c592f5263ed30c26b6a8ea5f48e230e90 (patch)
treedcaf5ddec25e2877694051610b46af9bfac409b8
parentc609f54f161901098cdcd3739b3591e486ceb238 (diff)
db calls
-rw-r--r--lib/bucky.js45
-rw-r--r--lib/db/index.js9
-rw-r--r--lib/index.js3
3 files changed, 45 insertions, 12 deletions
diff --git a/lib/bucky.js b/lib/bucky.js
index 9ec28f4..ef822c4 100644
--- a/lib/bucky.js
+++ b/lib/bucky.js
@@ -7,43 +7,70 @@ var bucky = module.exports = {
ensureLatestThreads: function (req, res, next){
db.getLatestThreads().then(function(threads){
res.threads = threads
+ res.threads_ids = res.threads.pluck("id").sort()
next()
})
},
ensureCommentCountsForThreads: function (req, res, next){
- var ids = res.threads.pluck("id")
- db.getCommentCounts(ids).then(function(counts){
+ db.getCommentCounts(res.threads_ids).then(function(counts){
var lookup = {}
- console.log(counts)
- counts.forEach(function(count){
+ counts.forEach(function(c,i){
+ res.threads.at(i).set("comment_count", c.count)
})
next()
})
},
ensureFileCountsForThreads: function (req, res, next){
- db.getFileCounts(ids).then(function(counts){
+ db.getFileCounts(res.threads_ids).then(function(counts){
+ var lookup = {}
+ counts.forEach(function(c,i){
+ res.threads.at(i).set("file_count", c.count)
+ })
next()
})
},
-
-
+ ensureFileSizeForThreads: function (req, res, next){
+ db.getFileSizes(res.threads_ids).then(function(sizes){
+ var lookup = {}
+ sizes.forEach(function(c,i){
+ res.threads.at(i).set("file_size", c.size)
+ })
+ next()
+ })
+ },
+
/* DETAILS */
ensureThread: function (req, res, next){
db.getThread(req.param.id).then(function(thread){
if (thread) {
res.thread = thread
+ next()
}
else {
res.sendCode(404)
}
})
},
+ ensureKeywordForThread: function (req, res, next){
+ var keyword = res.thread.get('keyword')
+ if (! keyword) return next()
+ db.getKeyword(keyword).then(function(keyword){
+ res.keyword = keyword
+ next()
+ })
+ },
ensureCommentsForThread: function (req, res, next){
- return db.getCommentsForThread(id).fetch()
+ db.getCommentsForThread(id).then(function(comments){
+ res.comments = comments
+ next()
+ })
},
ensureFilesForThread: function (req, res, next){
- return db.getFilesForThread(id).fetch()
+ db.getCommentsForThread(id).then(function(files){
+ res.files = files
+ next()
+ })
},
} \ No newline at end of file
diff --git a/lib/db/index.js b/lib/db/index.js
index 3761417..8e72d34 100644
--- a/lib/db/index.js
+++ b/lib/db/index.js
@@ -67,13 +67,16 @@ db.getFilesForThread = function (id){
return File.query("where", "thread", "=", id).fetchAll()
}
db.getFileCounts = function(ids){
- return knex.column('thread', 'count(*)').select().from('comments').where('thread', 'in', ids).groupBy('thread')
+ return knex.column('thread').count('* as count').select().from('files').where('thread', 'in', ids).groupBy('thread')
+}
+db.getFileSizes = function(ids){
+ return knex.column('thread').sum('size as size').select().from('files').where('thread', 'in', ids).groupBy('thread')
}
/* COMMENTS */
db.getCommentsForThread = function (id){
return Comment.query("where", "thread", "=", id).fetchAll()
}
-db.getCommentCounts = function(){
- return knex.column('thread', 'count(*)').select().from('files').where('thread', 'in', ids).groupBy('thread')
+db.getCommentCounts = function(ids){
+ return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread')
}
diff --git a/lib/index.js b/lib/index.js
index 20ccce5..f987077 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -82,6 +82,7 @@ site.route = function(){
bucky.ensureLatestThreads,
bucky.ensureCommentCountsForThreads,
bucky.ensureFileCountsForThreads,
+ bucky.ensureFileSizeForThreads,
function(req, res){
res.json({
threads: res.threads,
@@ -91,6 +92,7 @@ site.route = function(){
app.get("/api/thread/:id", function(req, res){
bucky.ensureThread,
+ bucky.ensureKeywordForThread,
bucky.ensureCommentsForThread,
bucky.ensureFilesForThread,
function(req, res){
@@ -98,6 +100,7 @@ site.route = function(){
thread: res.thread,
comments: res.comments,
files: res.files,
+ keyword: res.keyword,
})
}
})