aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-08 00:30:41 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-08 00:30:41 -0500
commit172304157f582a99fd2bba32fad7ed1018bf0b49 (patch)
treef55c23ee7c08cef2bb33465ed63f3c262c2fa1ce /src
parent2d6635b4ff0393af6d97b93772bc566c331de08e (diff)
downloadold-guff-172304157f582a99fd2bba32fad7ed1018bf0b49.tar.bz2
old-guff-172304157f582a99fd2bba32fad7ed1018bf0b49.zip
post json fixes
Diffstat (limited to 'src')
-rw-r--r--src/guff/post-model.cr31
-rw-r--r--src/guff/post.cr21
-rw-r--r--src/guff/results.cr7
3 files changed, 38 insertions, 21 deletions
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr
index 4a20db8..4788ba3 100644
--- a/src/guff/post-model.cr
+++ b/src/guff/post-model.cr
@@ -119,25 +119,21 @@ module Guff
"site_id": (site_id || @models.site.get_default).to_s,
} of String => String
- # verify sort column
- raise "unknown sort column" unless COLUMNS.has_key?(sort)
- raise "column is not sortable" unless COLUMNS[sort][:sortable]
-
# build tmpl args
tmpl_args = {
# TODO
"filter": "1 = 1", # true
- "sort": COLUMNS[sort][:col],
+ "sort": get_sort_clause(sort),
"dir": dir,
"tags": get_tags_filter(tags),
"cols": get_columns_clause(cols),
- } of String => String
+ }
# count number of matching rows
num_rows = (one(:count_posts, sql_args, tmpl_args).to_s || 0).to_i
# TODO: add Post class?
- rows = [] of Hash(String, ::SQLite3::Value)
+ rows = [] of Post
if num_rows > 0
all(:get_posts, sql_args.merge({
":offset": ((page - 1) * limit).to_s,
@@ -157,9 +153,22 @@ module Guff
)
end
- private def get_columns_clause(cols : Array(String)?)
+ private def get_sort_clause(
+ sort : String
+ ) : String
+ # verify sort column
+ raise "unknown sort column" unless COLUMNS.has_key?(sort)
+ raise "column is not sortable" unless COLUMNS[sort][:sortable]
+
+ # return result
+ COLUMNS[sort][:clause] as String
+ end
+
+ private def get_columns_clause(
+ cols : Array(String)?
+ ) : String
(cols || COLUMNS.keys.select { |col|
- COLUMNS[key][:default]
+ COLUMNS[col][:default]
}).map { |col|
"%s AS %s" % [COLUMNS[col][:clause], col]
}.join(", ")
@@ -171,7 +180,9 @@ module Guff
LIMIT 0
"
- private def get_tags_filter(tag_sets : Array(Array(String)))
+ private def get_tags_filter(
+ tag_sets : Array(Array(String))
+ ) : String
(tag_sets.size > 0) ? template(:tags, {
"sql": tag_sets.map { |tags|
template(:tags, {
diff --git a/src/guff/post.cr b/src/guff/post.cr
index 7247e15..735682c 100644
--- a/src/guff/post.cr
+++ b/src/guff/post.cr
@@ -1,10 +1,21 @@
-class Guff:Post
+class Guff::Post
def initialize(@row : Hash(String, ::SQLite3::Value))
end
- def to_json
- @row.merge({
- "tags": @row["tags"].split('|')
- }).to_json
+ def to_json(io)
+ @row.reduce({} of String => String | Array(String)) do |r, k, v|
+ r[k] = v.to_s
+ r
+ end.merge({
+ "tags": tags
+ }).to_json(io)
+ end
+
+ private def tags : Array(String)
+ @tags ||= if @row.has_key?("tags") && (@row["tags"] as String).size > 0
+ (@row["tags"] as String).split('|')
+ else
+ [] of String
+ end
end
end
diff --git a/src/guff/results.cr b/src/guff/results.cr
index 0dbc351..3fdc6e4 100644
--- a/src/guff/results.cr
+++ b/src/guff/results.cr
@@ -22,12 +22,7 @@ module Guff
num_rows: @num_rows,
},
- rows: @rows.map { |row|
- row.reduce({} of String => String) do |r, k, v|
- r[k] = v.to_s
- r
- end
- },
+ rows: @rows,
}.to_json
end
end