aboutsummaryrefslogtreecommitdiff
path: root/src/guff/post-model.cr
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/guff/post-model.cr
parent2d6635b4ff0393af6d97b93772bc566c331de08e (diff)
downloadold-guff-172304157f582a99fd2bba32fad7ed1018bf0b49.tar.bz2
old-guff-172304157f582a99fd2bba32fad7ed1018bf0b49.zip
post json fixes
Diffstat (limited to 'src/guff/post-model.cr')
-rw-r--r--src/guff/post-model.cr31
1 files changed, 21 insertions, 10 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, {