aboutsummaryrefslogtreecommitdiff
path: root/src/guff
diff options
context:
space:
mode:
Diffstat (limited to 'src/guff')
-rw-r--r--src/guff/api-methods.cr16
-rw-r--r--src/guff/database.cr11
-rw-r--r--src/guff/post-model.cr22
-rw-r--r--src/guff/tag-model.cr28
4 files changed, 51 insertions, 26 deletions
diff --git a/src/guff/api-methods.cr b/src/guff/api-methods.cr
index c29b324..14c71c2 100644
--- a/src/guff/api-methods.cr
+++ b/src/guff/api-methods.cr
@@ -25,7 +25,8 @@ module Guff
"tags": {
text: "Comma-separated list of tags (union)",
- type: :tag_list,
+ # type: :tag_list,
+ type: :json,
required: false,
default: "",
},
@@ -68,7 +69,8 @@ module Guff
"tags": {
text: "Post tags.",
- type: :tag_list,
+ # type: :tag_list,
+ type: :json,
required: false,
default: "",
},
@@ -88,7 +90,8 @@ module Guff
"tags": {
text: "Post tags.",
- type: :tag_list,
+ # type: :tag_list,
+ type: :json,
required: false,
default: "",
},
@@ -171,7 +174,8 @@ module Guff
args: {
"tags": {
text: "Tags to remove.",
- type: :tag_list,
+ # type: :tag_list,
+ type: :json,
required: false,
default: "",
},
@@ -268,10 +272,10 @@ module Guff
slug: /^[a-z0-9\.-]+$/,
int: /^\d+$/,
int_list: /^\d+(?:,\d+)*$/,
- tag_list: /^(?:[a-z0-9_\/ -]+(?:,[a-z0-9_\/ -]+)*|)$/,
sort: /^[a-z0-9_]+,(?:asc|desc)$/,
- # FIXME: lock this down more
+ # FIXME: lock these down more
+ json: /.*/,
path: /^[^\/].*[^\/]$/,
}
diff --git a/src/guff/database.cr b/src/guff/database.cr
index 0e41b75..9201a38 100644
--- a/src/guff/database.cr
+++ b/src/guff/database.cr
@@ -123,9 +123,14 @@ module Guff
end
def transaction(&block)
- query("BEGIN")
- block.call
- query("COMMIT")
+ begin
+ query("BEGIN")
+ block.call
+ query("COMMIT")
+ rescue e
+ query("ROLLBACK")
+ raise e
+ end
end
private def run(
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr
index 0a78458..40e92fb 100644
--- a/src/guff/post-model.cr
+++ b/src/guff/post-model.cr
@@ -40,7 +40,12 @@ module Guff
AND %{filter}
ORDER BY %{sort} %{dir}
- OFFSET :offset LIMIT :limit
+ LIMIT :limit OFFSET :offset
+ ",
+
+ add_post: "
+ INSERT INTO posts(site_id, slug, name, raw_body, body)
+ VALUES (:site_id, :slug, :name, :raw_body, :body)
",
remove_posts: "
@@ -68,6 +73,7 @@ module Guff
CROSS JOIN tags b
WHERE a.site_id = :site_id
+ AND a.post_id = :post_id
AND b.name IN (%{tags})
",
})
@@ -95,10 +101,10 @@ module Guff
clause: "a.body",
},
- "html": {
- default: true,
+ "raw_body": {
+ default: false,
sortable: true,
- clause: "a.html",
+ clause: "a.raw_body",
},
"posted_at": {
@@ -117,13 +123,13 @@ module Guff
default: true,
sortable: false,
clause: "
- (SELECT group_agg(d.name, '|')
+ (SELECT group_concat(d.name, '|')
FROM post_tags c
JOIN tags d
ON (d.tag_id = c.tag_id)
- WHERE d.post_id = a.post_id)
+ WHERE c.post_id = a.post_id)
",
},
}
@@ -164,8 +170,8 @@ module Guff
rows = [] of Post
if num_rows > 0
all(:get_posts, sql_args.merge({
- ":offset": ((page - 1) * limit).to_s,
- ":limit": limit.to_s,
+ "offset": ((page - 1) * limit).to_s,
+ "limit": limit.to_s,
}), tmpl_args) do |row|
rows << Post.new(row)
nil
diff --git a/src/guff/tag-model.cr b/src/guff/tag-model.cr
index 2aa9d89..997cdfd 100644
--- a/src/guff/tag-model.cr
+++ b/src/guff/tag-model.cr
@@ -2,7 +2,7 @@ module Guff
class TagModel < Model
SQL = TemplateCache.new({
add_tags: "
- INSERT INTO tags(name) VALUES(%{tags})
+ INSERT INTO tags(name) VALUES %{tags}
",
get_tags: "
@@ -20,16 +20,25 @@ module Guff
end
def add_tags(tags : Array(String))
+ missing_tags = get_missing_tags(tags)
+
+ if missing_tags.size > 0
+ query(:add_tags, nil, {
+ "tags": missing_tags.map { |tag|
+ "('" + @db.quote(tag) + "')"
+ }.join(','),
+ })
+ end
+ end
+
+ private def get_missing_tags(
+ tags : Array(String)
+ ) : Array(String)
# get ids of existing tags
ids = get_ids(tags)
- query(:add_tags, nil, {
- "tags": tags.reject { |tag|
- ids[tag]?
- }.map { |tag|
- "'" + @db.quote(tag) + "'"
- }.join(','),
- })
+ # return missing tags
+ tags.reject { |tag| ids[tag]? }
end
private def get_ids(
@@ -42,7 +51,8 @@ module Guff
"'" + @db.quote(tag) + "'"
}.join(','),
}) do |row|
- r[row["name"] as String] = (row["tag_id"] as String).to_i
+ # add to results
+ r[row["name"] as String] = row["tag_id"].to_s.to_i
end
# return result