aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-07 00:00:32 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-07 00:00:32 -0500
commitfbb979af779341ed15c0c4d9d08ec52eaebb1037 (patch)
tree9cf11f1cbc9d1a9ae4fc1579aeb865770ec44d27
parentf0f87c439037715cac407004e2220c678d96757a (diff)
downloadold-guff-fbb979af779341ed15c0c4d9d08ec52eaebb1037.tar.bz2
old-guff-fbb979af779341ed15c0c4d9d08ec52eaebb1037.zip
add site-model, misc fixes
-rw-r--r--src/guff/api-methods.cr12
-rw-r--r--src/guff/database-updater.cr2
-rw-r--r--src/guff/models.cr11
-rw-r--r--src/guff/post-model.cr11
-rw-r--r--src/guff/site-model.cr25
5 files changed, 45 insertions, 16 deletions
diff --git a/src/guff/api-methods.cr b/src/guff/api-methods.cr
index 598f050..859e7ce 100644
--- a/src/guff/api-methods.cr
+++ b/src/guff/api-methods.cr
@@ -27,7 +27,7 @@ module Guff
text: "Comma-separated list of tags (union)",
type: :tag_list,
required: false,
- default: "1",
+ default: "",
},
"sort": {
@@ -263,7 +263,7 @@ module Guff
slug: /^[a-z0-9\.-]+$/,
int: /^\d+$/,
int_list: /^\d+(?:,\d+)*$/,
- tag_list: /^[a-z0-9_\/ -]+(?:,[a-z0-9_\/ -]+)*$/,
+ tag_list: /^(?:[a-z0-9_\/ -]+(?:,[a-z0-9_\/ -]+)*|)$/,
sort: /^[a-z0-9_]+,(?:asc|desc)$/,
# FIXME: lock this down more
@@ -317,15 +317,17 @@ module Guff
# post methods #
################
+ NO_TAGS = [] of Array(String)
+
private def do_post_get_posts(
context : HTTP::Server::Context,
args : Hash(String, String)
)
@models.post.get_posts(
- site_id: get_site_id(context.request.headers["host"]?),
+ site_id: @models.site.to_site(context.request.headers["host"]?),
q: args["q"]? || "",
- page: args["page"]? ? args["page"].to_i : 1,
- tags: args["tags"]? ? [args["tags"].split(/,/)] : [] of Array(String),
+ tags: (args.has_key?("tags") && args["tags"].size > 0) ? [args["tags"].split(',')] : NO_TAGS,
+ page: args.has_key?("page") ? args["page"].to_i : 1,
).to_json
end
diff --git a/src/guff/database-updater.cr b/src/guff/database-updater.cr
index daf77fd..d472c50 100644
--- a/src/guff/database-updater.cr
+++ b/src/guff/database-updater.cr
@@ -46,6 +46,8 @@ module Guff
is_default BOOLEAN NOT NULL DEFAULT false
)
}, %{
+ INSERT INTO sites(name, is_default) VALUES ('Default', 1)
+ }, %{
CREATE TABLE posts (
-- unique id
post_id INTEGER PRIMARY KEY,
diff --git a/src/guff/models.cr b/src/guff/models.cr
index 24cc9f8..0e09752 100644
--- a/src/guff/models.cr
+++ b/src/guff/models.cr
@@ -1,10 +1,11 @@
require "./database"
require "./database-updater"
+require "./site-model"
private macro define_model_getters(hash)
{% for name, klass in hash %}
- def {{ name.id }}
- @cache[{{ name }}] ||= {{ klass.id }}.new(self)
+ def {{ name.id }} : {{ klass.id }}
+ (@cache[{{ name }}] ||= {{ klass.id }}.new(self)) as {{ klass.id }}
end
{% end %}
end
@@ -31,11 +32,7 @@ module Guff
define_model_getters({
post: PostModel,
tag: TagModel,
+ site: SiteModel,
})
-
- def default_site_id
- # TODO
- 0
- end
end
end
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr
index 4e2bf63..4a3f406 100644
--- a/src/guff/post-model.cr
+++ b/src/guff/post-model.cr
@@ -65,19 +65,21 @@ module Guff
}
def get_posts(
- site_id = nil : Int32?,
+ site_id = nil : Int?,
q = "" : String,
tags = [] of Array(String) : Array(Array(String)),
- page = 1 : Int32,
- limit = 20 : Int32,
+ page = 1 : Int,
+ limit = 20 : Int,
sort = "posted_at" : String,
dir = "desc" : String,
)
# build sql args
sql_args = {
- "site_id": "0" # (site_id || @models.default_site_id).to_s,
+ "site_id": (site_id || @models.site.get_default).to_s,
} of String => String
+ p tags
+
# build tmpl args
tmpl_args = {
# TODO
@@ -112,6 +114,7 @@ module Guff
end
private def get_tags_filter(tag_sets : Array(Array(String)))
+ p tag_sets
(tag_sets.size > 0) ? template(:tags, {
"sql": tag_sets.map { |tags|
template(:tags, {
diff --git a/src/guff/site-model.cr b/src/guff/site-model.cr
new file mode 100644
index 0000000..ecde831
--- /dev/null
+++ b/src/guff/site-model.cr
@@ -0,0 +1,25 @@
+module Guff
+ class SiteModel < Model
+ SQL = TemplateCache.new({
+ get_default: "
+ SELECT site_id
+ FROM sites
+ WHERE is_default
+ LIMIT 1
+ ",
+ })
+
+ def initialize(models : Models)
+ super(models, SQL)
+ end
+
+ def get_default
+ one(:get_default, nil, {} of String => String)
+ end
+
+ def to_site(host : String?)
+ # TODO
+ get_default
+ end
+ end
+end