aboutsummaryrefslogtreecommitdiff
path: root/src/guff/post-model.cr
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-08 04:38:03 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-08 04:38:03 -0500
commit490464d7d07e94b6ee69bbbc4b290a2b0e3a0975 (patch)
tree4d5e7bb1e5704433110f16e4314c295875c475f8 /src/guff/post-model.cr
parent66460b1190a958bbb84e87d01a9588dfb17f9f51 (diff)
downloadold-guff-490464d7d07e94b6ee69bbbc4b290a2b0e3a0975.tar.bz2
old-guff-490464d7d07e94b6ee69bbbc4b290a2b0e3a0975.zip
add update_post
Diffstat (limited to 'src/guff/post-model.cr')
-rw-r--r--src/guff/post-model.cr76
1 files changed, 73 insertions, 3 deletions
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr
index 40e92fb..9200ccb 100644
--- a/src/guff/post-model.cr
+++ b/src/guff/post-model.cr
@@ -43,8 +43,15 @@ module Guff
LIMIT :limit OFFSET :offset
",
+ update_post: "
+ UPDATE posts
+ SET %{sets}
+ WHERE site_id = :site_id
+ AND post_id = :post_id
+ ",
+
add_post: "
- INSERT INTO posts(site_id, slug, name, raw_body, body)
+ INSERT INTO posts(site_id, slug, name, raw_body, body)
VALUES (:site_id, :slug, :name, :raw_body, :body)
",
@@ -273,6 +280,67 @@ module Guff
post_id
end
+ def update_post(
+ site_id = nil : Int?,
+ post_id = nil : Int?,
+ slug = nil : String?,
+ name = nil : String?,
+ body = nil : String?,
+ tags = nil : Array(String)?,
+ posted = nil : Bool?
+ )
+ raise "null post_id" if post_id.nil?
+
+ sets = [] of String
+ args = {
+ "site_id": (site_id || @models.site.get_default).to_s,
+ "post_id": post_id.to_s,
+ }
+
+ if slug
+ sets << "slug = :slug"
+ args["slug"] = slug
+ end
+
+ if name
+ sets << "name = :name"
+ args["name"] = name
+ end
+
+ if body
+ sets << "body = :body, raw_body = :body"
+ args["body"] = body
+ end
+
+ unless posted.nil?
+ val = posted ? "CURRENT_TIMESTAMP" : "NULL"
+ sets << "posted_at = %s" % [val]
+ end
+
+ if sets.size > 0 || tags
+ transaction do
+ if sets.size > 0
+ # update post
+ query(:update_post, args, {
+ "sets": sets.join(','),
+ })
+ end
+
+ # TODO: post history
+
+ if tags
+ # update tags
+ set_tags(
+ site_id: site_id,
+ post_id: post_id,
+ tags: tags,
+ use_transaction: false,
+ )
+ end
+ end
+ end
+ end
+
def remove_posts(
site_id = nil : Int?,
post_ids = [] of Int : Array(Int)
@@ -292,7 +360,7 @@ module Guff
def set_tags(
site_id = nil : Int?,
post_id = nil : Int?,
- tags = [] of String : Array(String),
+ tags = [] of String : Array(String)?,
use_transaction = true : Bool
)
if use_transaction
@@ -309,8 +377,10 @@ module Guff
private def raw_set_tags(
site_id = nil : Int?,
post_id = nil : Int?,
- tags = [] of String : Array(String),
+ tags = [] of String : Array(String)?,
)
+ return unless post_id && tags
+
# build sql args
args = {
"site_id": (site_id || @models.site.get_default).to_s,