aboutsummaryrefslogtreecommitdiff
path: root/src/guff/post-model.cr
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-08 03:27:56 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-08 03:27:56 -0500
commit623292eb16593a02e11786116ab6efd5a14047fe (patch)
tree1fa00d849c6fe304c93bcb1afdeb19ca56e7429e /src/guff/post-model.cr
parent4a5da91ee4df8b6ca886e4c2433fb04bc6499041 (diff)
downloadold-guff-623292eb16593a02e11786116ab6efd5a14047fe.tar.bz2
old-guff-623292eb16593a02e11786116ab6efd5a14047fe.zip
lots of fixes
Diffstat (limited to 'src/guff/post-model.cr')
-rw-r--r--src/guff/post-model.cr133
1 files changed, 124 insertions, 9 deletions
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr
index 4788ba3..0a78458 100644
--- a/src/guff/post-model.cr
+++ b/src/guff/post-model.cr
@@ -42,6 +42,34 @@ module Guff
ORDER BY %{sort} %{dir}
OFFSET :offset LIMIT :limit
",
+
+ remove_posts: "
+ UPDATE posts
+ SET is_active = 0
+ WHERE site_id = :site_id
+ AND post_id IN (%{post_ids})
+ ",
+
+ set_tags_delete: "
+ DELETE FROM post_tags WHERE post_id = (
+ SELECT post_id
+ FROM posts
+ WHERE site_id = :site_id
+ AND post_id = :post_id
+ )
+ ",
+
+ set_tags_insert: "
+ INSERT INTO post_tags(post_id, tag_id)
+ SELECT a.post_id,
+ b.tag_id
+
+ FROM posts a
+ CROSS JOIN tags b
+
+ WHERE a.site_id = :site_id
+ AND b.name IN (%{tags})
+ ",
})
def initialize(models : Models)
@@ -196,19 +224,106 @@ module Guff
}) : NO_POSTS
end
- def add_post(req)
- # TODO: return post id
- {ok: true}
+ ####################
+ # add_post methods #
+ ####################
+
+ def add_post(
+ site_id = nil : Int?,
+ slug = "" : String,
+ name = "" : String,
+ body = "" : String,
+ tags = [] of String : Array(String),
+ ) : Int64
+ post_id = -1_i64
+
+ # check slug, name, and body
+ { "slug": slug, "name": name, "body": body }.each do |name, text|
+ raise "invalid %s" % [name] unless text.size > 0
+ end
+
+ transaction do
+ query(:add_post, {
+ "site_id": (site_id || @models.site.get_default).to_s,
+ "slug": slug,
+ "name": name,
+ "raw_body": body,
+ "body": body,
+ }, nil)
+
+ # get post id
+ post_id = last_insert_row_id
+
+ # set post tags
+ set_tags(
+ site_id: site_id,
+ post_id: post_id,
+ tags: tags,
+ use_transaction: false,
+ )
+ end
+
+ # return post id
+ post_id
end
- def remove_posts(req)
- # TODO
- {ok: true}
+ def remove_posts(
+ site_id = nil : Int?,
+ post_ids = [] of Int : Array(Int)
+ )
+ query(:remove_posts, {
+ "site_id": (site_id || @models.site.get_default).to_s,
+ }, {
+ "post_ids": post_ids.map { |post_id|
+ "'" + @db.quote(post_id.to_s) + "'"
+ }.join(',')
+ })
+
+ # no return value
+ nil
end
- def set_tags(req)
- # TODO
- {ok: true}
+ def set_tags(
+ site_id = nil : Int?,
+ post_id = nil : Int?,
+ tags = [] of String : Array(String),
+ use_transaction = true : Bool
+ )
+ if use_transaction
+ transaction do
+ raw_set_tags(site_id, post_id, tags)
+ end
+ else
+ raw_set_tags(site_id, post_id, tags)
+ end
+
+ nil
+ end
+
+ private def raw_set_tags(
+ site_id = nil : Int?,
+ post_id = nil : Int?,
+ tags = [] of String : Array(String),
+ )
+ # build sql args
+ args = {
+ "site_id": (site_id || @models.site.get_default).to_s,
+ "post_id": post_id.to_s,
+ }
+
+ # delete existing post tags
+ query(:set_tags_delete, args, nil)
+
+ if tags.size > 0
+ @models.tag.add_tags(tags)
+
+ # add new post tags
+ query(:set_tags_insert, args, {
+ "tags": tags.map { |tag|
+ "'" + @db.quote(tag) + "'"
+ }.join(','),
+ })
+ end
end
end
end