diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-03-08 04:38:03 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-03-08 04:38:03 -0500 |
commit | 490464d7d07e94b6ee69bbbc4b290a2b0e3a0975 (patch) | |
tree | 4d5e7bb1e5704433110f16e4314c295875c475f8 /src | |
parent | 66460b1190a958bbb84e87d01a9588dfb17f9f51 (diff) | |
download | old-guff-490464d7d07e94b6ee69bbbc4b290a2b0e3a0975.tar.bz2 old-guff-490464d7d07e94b6ee69bbbc4b290a2b0e3a0975.zip |
add update_post
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/api-handler.cr | 1 | ||||
-rw-r--r-- | src/guff/api-methods.cr | 55 | ||||
-rw-r--r-- | src/guff/post-model.cr | 76 |
3 files changed, 128 insertions, 4 deletions
diff --git a/src/guff/api-handler.cr b/src/guff/api-handler.cr index 19b6aa5..370c8f6 100644 --- a/src/guff/api-handler.cr +++ b/src/guff/api-handler.cr @@ -89,6 +89,7 @@ module Guff post: [ get_posts, add_post, + update_post, remove_posts, set_tags, ], diff --git a/src/guff/api-methods.cr b/src/guff/api-methods.cr index 14c71c2..334be88 100644 --- a/src/guff/api-methods.cr +++ b/src/guff/api-methods.cr @@ -77,6 +77,41 @@ module Guff }, }, + "update_post": { + text: "Update existing post.", + args: { + "name": { + text: "Post title.", + type: :text, + required: false, + }, + + "slug": { + text: "Post slug.", + type: :slug, + required: false, + }, + + "body": { + text: "Post body.", + type: :text, + required: false, + }, + + "tags": { + text: "Post tags.", + type: :json, + required: false, + }, + + "posted": { + text: "Is this post posted?", + type: :bool, + required: false, + }, + }, + }, + "remove_posts": { text: "Remove existing posts.", @@ -353,7 +388,7 @@ module Guff site_id: get_site(context), slug: args["slug"], name: args["name"], - body: args["name"], + body: args["body"], tags: get_tags(args["tags"]?), ) @@ -361,6 +396,24 @@ module Guff { post_id: post_id }.to_json end + private def do_post_update_post( + context : HTTP::Server::Context, + args : Hash(String, String) + ) + post_id = @models.post.update_post( + site_id: get_site(context), + post_id: args["post_id"].to_i, + slug: args["slug"]?, + name: args["name"], + body: args["body"], + tags: args.has_key?("tags") ? get_tags(args["tags"]?) : nil, + posted: args["posted"]?, + ) + + # return json + { ok: true }.to_json + end + private def do_post_remove_posts( context : HTTP::Server::Context, args : Hash(String, String) 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, |