aboutsummaryrefslogtreecommitdiff
path: root/src/guff/tag-model.cr
diff options
context:
space:
mode:
Diffstat (limited to 'src/guff/tag-model.cr')
-rw-r--r--src/guff/tag-model.cr28
1 files changed, 19 insertions, 9 deletions
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