aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-07 03:50:09 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-07 03:50:09 -0500
commitb8a4926107f273c1606a026687ebfcc74da147d4 (patch)
treebfa27b3ee1c3731ec9e36cc2e0b96c48b61232f0
parentfbb979af779341ed15c0c4d9d08ec52eaebb1037 (diff)
downloadold-guff-b8a4926107f273c1606a026687ebfcc74da147d4.tar.bz2
old-guff-b8a4926107f273c1606a026687ebfcc74da147d4.zip
refactor get_posts
-rw-r--r--src/guff/api-methods.cr24
-rw-r--r--src/guff/post-model.cr98
2 files changed, 93 insertions, 29 deletions
diff --git a/src/guff/api-methods.cr b/src/guff/api-methods.cr
index 859e7ce..d8d1c44 100644
--- a/src/guff/api-methods.cr
+++ b/src/guff/api-methods.cr
@@ -36,6 +36,12 @@ module Guff
required: false,
default: "date,desc",
},
+
+ "cols": {
+ text: "Column list",
+ type: :text,
+ required: false,
+ },
},
},
@@ -296,17 +302,21 @@ module Guff
# get value
val = if arg_data[:required]
params.fetch(arg_name)
- else
+ elsif arg_data[:default]
params.fetch(arg_name, arg_data[:default] as String)
+ else
+ nil
end
- # check value
- if !TYPE_CHECKS[arg_data[:type]].match(val)
- raise "invalid parameter format: %s" % [arg_name]
- end
+ if val
+ # check value
+ if !TYPE_CHECKS[arg_data[:type]].match(val)
+ raise "invalid parameter format: %s" % [arg_name]
+ end
- # add value to result
- r[arg_name] = val
+ # add value to result
+ r[arg_name] = val
+ end
# return result
r
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr
index 4a3f406..46e2dca 100644
--- a/src/guff/post-model.cr
+++ b/src/guff/post-model.cr
@@ -30,19 +30,7 @@ module Guff
",
get_posts: "
- SELECT a.post_id,
- a.site_id,
- a.name,
- a.body,
- a.html,
-
- (SELECT group_agg(d.name, '|')
-
- FROM post_tags c
- JOIN tags d
- ON (d.tag_id = c.tag_id)
-
- WHERE d.post_id = a.post_id) AS tags
+ SELECT %{cols}
FROM posts a
JOIN (%{tags}) b
@@ -60,11 +48,64 @@ module Guff
super(models, SQL)
end
- SORTS = {
- "posted_at": "a.posted_at",
+ COLUMNS = {
+ "post_id": {
+ default: true,
+ sortable: true,
+ clause: "a.post_id",
+ },
+
+ "name": {
+ default: true,
+ sortable: true,
+ clause: "a.name",
+ },
+
+ "body": {
+ default: false,
+ sortable: true,
+ clause: "a.body",
+ },
+
+ "html": {
+ default: true,
+ sortable: true,
+ clause: "a.html",
+ },
+
+ "posted_at": {
+ default: true,
+ sortable: true,
+ clause: "a.posted_at",
+ },
+
+ "created_at": {
+ default: true,
+ sortable: true,
+ clause: "a.created_at",
+ },
+
+ "tags": {
+ default: true,
+ sortable: false,
+ clause: "
+ (SELECT group_agg(d.name, '|')
+
+ FROM post_tags c
+ JOIN tags d
+ ON (d.tag_id = c.tag_id)
+
+ WHERE d.post_id = a.post_id)
+ ",
+ },
}
+ #####################
+ # get_posts methods #
+ #####################
+
def get_posts(
+ cols = nil : Array(String)?,
site_id = nil : Int?,
q = "" : String,
tags = [] of Array(String) : Array(Array(String)),
@@ -78,15 +119,18 @@ module Guff
"site_id": (site_id || @models.site.get_default).to_s,
} of String => String
- p tags
+ # 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": SORTS[sort],
+ "sort": COLUMNS[sort][:col],
"dir": dir,
"tags": get_tags_filter(tags),
+ "cols": get_columns_clause(cols),
} of String => String
# count number of matching rows
@@ -113,6 +157,20 @@ module Guff
)
end
+ private def get_columns_clause(cols : Array(String)?)
+ (cols || COLUMNS.keys.select { |col|
+ COLUMNS[key][:default]
+ }).map { |col|
+ "%s AS %s" % [COLUMNS[col][:clause], col]
+ }.join(", ")
+ end
+
+ NO_POSTS = "
+ SELECT post_id
+ FROM posts
+ LIMIT 0
+ "
+
private def get_tags_filter(tag_sets : Array(Array(String)))
p tag_sets
(tag_sets.size > 0) ? template(:tags, {
@@ -125,11 +183,7 @@ module Guff
}.join(" INTERSECTS "),
})
}.join(" UNION "),
- }) : "
- SELECT post_id
- FROM posts
- LIMIT 0
- "
+ }) : NO_POSTS
end
def add_post(req)