diff options
-rw-r--r-- | src/guff.cr | 236 |
1 files changed, 231 insertions, 5 deletions
diff --git a/src/guff.cr b/src/guff.cr index c340cfe..61330a1 100644 --- a/src/guff.cr +++ b/src/guff.cr @@ -178,6 +178,8 @@ module Guff end end + ISO8601 = ::Time::Format.new("%Y-%m-%dT%H:%M:%SZ") + module MimeType TYPES = { ".js": "text/javascript; charset=utf-8", @@ -271,8 +273,6 @@ module Guff @context.dbs.rw.last_insert_row_id.to_i64 end - ISO8601_TIME = ::Time::Format.new("%Y-%m-%dT%H:%M:%SZ") - def set( post_id : Int64, @@ -302,12 +302,17 @@ module Guff if state sets << "state_id = (SELECT state_id FROM states WHERE state = ?)" args << state + + if state == "posted" && !have_posted_at + # set posted_at by default + sets << "posted_at = COALESCE(posted_at, CURRENT_TIMESTAMP)" + end end if have_posted_at if posted_at sets << "posted_at = ?" - args << ISO8601_TIME.format(posted_at) + args << ISO8601.format(posted_at) else sets << "posted_at = NULL" end @@ -316,7 +321,7 @@ module Guff if have_expires_at if expires_at sets << "expires_at = ?" - args << ISO8601_TIME.format(expires_at) + args << ISO8601.format(expires_at) else sets << "expires_at = NULL" end @@ -327,7 +332,7 @@ module Guff args << slug end - if slug_lock.not_nil? + unless slug_lock.nil? sets << "slug_lock = ?" args << (slug_lock ? "1" : "0") end @@ -374,6 +379,12 @@ module Guff INSERT INTO pages(post_id, layout_id) VALUES (?, (SELECT layout_id FROM layouts WHERE layout = 'default')) ", + + set: " + UPDATE pages + SET layout_id = (SELECT layout_id FROM layouts WHERE layout = ?) + WHERE post_id = ? + ", } def get_id( @@ -398,6 +409,54 @@ module Guff post_id end + + def set( + post_id : Int64, + + site_id : Int64? = nil, + state : String? = nil, + + have_posted_at : Bool = false, + posted_at : Time? = nil, + + have_expires_at : Bool = false, + expires_at : Time? = nil, + + slug : String? = nil, + slug_lock : Bool? = nil, + + name : String? = nil, + body : String? = nil, + + layout : String? = nil, + ) + db = @context.dbs.rw + + db.transaction do + @context.models.post.set( + post_id: post_id, + + site_id: site_id, + state: state, + + have_posted_at: have_posted_at, + posted_at: posted_at, + + have_expires_at: have_expires_at, + expires_at: expires_at, + + slug: slug, + slug_lock: slug_lock, + + name: name, + body: body, + ) + + if layout + db.query(SQL[:set], [layout, post_id.to_s]) + end + end + end end class ProjectModel < Model @@ -424,6 +483,12 @@ module Guff add: " INSERT INTO projects(post_id) VALUES (?) ", + + set: " + UPDATE projects + SET repo_url = ? + WHERE post_id = ? + ", } def get_id( @@ -448,6 +513,54 @@ module Guff post_id end + + def set( + post_id : Int64, + + site_id : Int64? = nil, + state : String? = nil, + + have_posted_at : Bool = false, + posted_at : Time? = nil, + + have_expires_at : Bool = false, + expires_at : Time? = nil, + + slug : String? = nil, + slug_lock : Bool? = nil, + + name : String? = nil, + body : String? = nil, + + repo_url : String? = nil, + ) + db = @context.dbs.rw + + db.transaction do + @context.models.post.set( + post_id: post_id, + + site_id: site_id, + state: state, + + have_posted_at: have_posted_at, + posted_at: posted_at, + + have_expires_at: have_expires_at, + expires_at: expires_at, + + slug: slug, + slug_lock: slug_lock, + + name: name, + body: body, + ) + + if repo_url + db.query(SQL[:set], [repo_url, post_id.to_s]) + end + end + end end class BlogModel < Model @@ -550,6 +663,46 @@ module Guff post_id end + + def set( + post_id : Int64, + + site_id : Int64? = nil, + state : String? = nil, + + have_posted_at : Bool = false, + posted_at : Time? = nil, + + have_expires_at : Bool = false, + expires_at : Time? = nil, + + slug : String? = nil, + slug_lock : Bool? = nil, + + name : String? = nil, + body : String? = nil, + + repo_url : String? = nil, + ) + @context.models.post.set( + post_id: post_id, + + site_id: site_id, + state: state, + + have_posted_at: have_posted_at, + posted_at: posted_at, + + have_expires_at: have_expires_at, + expires_at: expires_at, + + slug: slug, + slug_lock: slug_lock, + + name: name, + body: body, + ) + end end class UserModel < Model @@ -974,6 +1127,31 @@ module Guff { "post_id": post_id } end + + def do_page_set(params : HTTP::Params) + @context.models.page.set( + post_id: params["post_id"].to_i64, + + site_id: params["site_id"]? ? params["site_id"].to_i64 : nil, + state: params["state"]?, + + have_posted_at: !!params["posted_at"]?, + posted_at: params["posted_at"]? ? ISO8601.parse(params["posted_at"]) : nil, + + have_expires_at: !!params["expires_at"]?, + expires_at: params["expires_at"]? ? ISO8601.parse(params["expires_at"]) : nil, + + slug: params["slug"]?, + slug_lock: params["slug_lock"]? ? (params["slug_lock"] == "t") : nil, + + name: params["name"]?, + body: params["body"]?, + + layout: params["layout"]?, + ) + + nil + end end module ProjectAPI @@ -985,6 +1163,31 @@ module Guff { "post_id": post_id } end + + def do_project_set(params : HTTP::Params) + @context.models.project.set( + post_id: params["post_id"].to_i64, + + site_id: params["site_id"]? ? params["site_id"].to_i64 : nil, + state: params["state"]?, + + have_posted_at: !!params["posted_at"]?, + posted_at: params["posted_at"]? ? ISO8601.parse(params["posted_at"]) : nil, + + have_expires_at: !!params["expires_at"]?, + expires_at: params["expires_at"]? ? ISO8601.parse(params["expires_at"]) : nil, + + slug: params["slug"]?, + slug_lock: params["slug_lock"]? ? (params["slug_lock"] == "t") : nil, + + name: params["name"]?, + body: params["body"]?, + + repo_url: params["repo_url"]?, + ) + + nil + end end module BlogAPI @@ -996,6 +1199,29 @@ module Guff { "post_id": post_id } end + + def do_blog_set(params : HTTP::Params) + @context.models.blog.set( + post_id: params["post_id"].to_i64, + + site_id: params["site_id"]? ? params["site_id"].to_i64 : nil, + state: params["state"]?, + + have_posted_at: !!params["posted_at"]?, + posted_at: params["posted_at"]? ? ISO8601.parse(params["posted_at"]) : nil, + + have_expires_at: !!params["expires_at"]?, + expires_at: params["expires_at"]? ? ISO8601.parse(params["expires_at"]) : nil, + + slug: params["slug"]?, + slug_lock: params["slug_lock"]? ? (params["slug_lock"] == "t") : nil, + + name: params["name"]?, + body: params["body"]?, + ) + + nil + end end module UserAPI |