diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-07-15 20:15:44 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-07-15 20:15:44 -0400 |
commit | 5589dac10cf28c462793079469ec811c8267531c (patch) | |
tree | 5f4398079c90b76c3f69308afff0502e7e6f4191 | |
parent | 817340c34f8287bc13424c0871240d1f52f20494 (diff) | |
download | guff-5589dac10cf28c462793079469ec811c8267531c.tar.bz2 guff-5589dac10cf28c462793079469ec811c8267531c.zip |
refactor apis
-rw-r--r-- | src/guff.cr | 190 | ||||
-rw-r--r-- | src/guff/apis.cr | 189 |
2 files changed, 189 insertions, 190 deletions
diff --git a/src/guff.cr b/src/guff.cr index 3fecd10..c7e3216 100644 --- a/src/guff.cr +++ b/src/guff.cr @@ -1325,196 +1325,6 @@ module Guff end end - module APIs - module PostAPI - def do_post_get_posts(params : HTTP::Params) - @context.models.post.get_posts( - site_id: (params["site_id"]? && params["site_id"] != "all") ? params["site_id"].to_i64 : nil, - user_id: (params["user_id"]? && params["user_id"] != "all") ? params["user_id"].to_i64 : nil, - state: params["state"]?, - type: params["type"]?, - q: params["q"]?, - page: params["page"].to_i32, - ) - end - end - - module PageAPI - def do_page_add(params : HTTP::Params) - post_id = @context.models.page.add( - site_id: params["site_id"]? ? params["site_id"].to_i64 : @context.models.site.get_default_id, - user_id: @context.user_id.not_nil!, - ) - - { "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 - - def do_page_get(params : HTTP::Params) - @context.models.page.get( - post_id: params["post_id"].to_i64 - ).reduce({} of String => String) do |r, kv| - r[kv[0]] = kv[1].to_s - r - end - end - end - - module ProjectAPI - def do_project_add(params : HTTP::Params) - post_id = @context.models.project.add( - site_id: params["site_id"]? ? params["site_id"].to_i64 : @context.models.site.get_default_id, - user_id: @context.user_id.not_nil!, - ) - - { "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 - - def do_project_get(params : HTTP::Params) - @context.models.project.get( - post_id: params["post_id"].to_i64 - ).reduce({} of String => String) do |r, kv| - r[kv[0]] = kv[1].to_s - r - end - end - end - - module BlogAPI - def do_blog_add(params : HTTP::Params) - post_id = @context.models.blog.add( - site_id: params["site_id"]? ? params["site_id"].to_i64 : @context.models.site.get_default_id, - user_id: @context.user_id.not_nil!, - ) - - { "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 - - def do_blog_get(params : HTTP::Params) - @context.models.blog.get( - post_id: params["post_id"].to_i64 - ).reduce({} of String => String) do |r, kv| - r[kv[0]] = kv[1].to_s - r - end - end - end - - module UserAPI - def do_user_add(params : HTTP::Params) - user_id = @context.models.user.add( - name: params["name"], - email: params["email"], - password: params["password"], - active: (params["active"] == "t"), - role: params["role"], - ) - - { "user_id": user_id } - end - - def do_user_get(params : HTTP::Params) - @context.models.user.get(params["user_id"].to_i64) - end - - - def do_user_set(params : HTTP::Params) - @context.models.user.set( - user_id: params["user_id"].to_i64, - name: params["name"]?, - email: params["email"]?, - password: (params["password"]? && params["password"].size > 0) ? params["password"] : nil, - active: params["active"]? ? (params["active"] == "t") : nil, - role: params["role"]?, - ) - - nil - end - - def do_user_get_users(params : HTTP::Params) - @context.models.user.get_users - end - end - - module SiteAPI - def do_site_get_sites(params : HTTP::Params) - @context.models.site.get_sites - end - end - end - module Views abstract class View def initialize(@context : Context) diff --git a/src/guff/apis.cr b/src/guff/apis.cr new file mode 100644 index 0000000..13d0d44 --- /dev/null +++ b/src/guff/apis.cr @@ -0,0 +1,189 @@ +module Guff::APIs + module PostAPI + def do_post_get_posts(params : HTTP::Params) + @context.models.post.get_posts( + site_id: (params["site_id"]? && params["site_id"] != "all") ? params["site_id"].to_i64 : nil, + user_id: (params["user_id"]? && params["user_id"] != "all") ? params["user_id"].to_i64 : nil, + state: params["state"]?, + type: params["type"]?, + q: params["q"]?, + page: params["page"].to_i32, + ) + end + end + + module PageAPI + def do_page_add(params : HTTP::Params) + post_id = @context.models.page.add( + site_id: params["site_id"]? ? params["site_id"].to_i64 : @context.models.site.get_default_id, + user_id: @context.user_id.not_nil!, + ) + + { "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 + + def do_page_get(params : HTTP::Params) + @context.models.page.get( + post_id: params["post_id"].to_i64 + ).reduce({} of String => String) do |r, kv| + r[kv[0]] = kv[1].to_s + r + end + end + end + + module ProjectAPI + def do_project_add(params : HTTP::Params) + post_id = @context.models.project.add( + site_id: params["site_id"]? ? params["site_id"].to_i64 : @context.models.site.get_default_id, + user_id: @context.user_id.not_nil!, + ) + + { "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 + + def do_project_get(params : HTTP::Params) + @context.models.project.get( + post_id: params["post_id"].to_i64 + ).reduce({} of String => String) do |r, kv| + r[kv[0]] = kv[1].to_s + r + end + end + end + + module BlogAPI + def do_blog_add(params : HTTP::Params) + post_id = @context.models.blog.add( + site_id: params["site_id"]? ? params["site_id"].to_i64 : @context.models.site.get_default_id, + user_id: @context.user_id.not_nil!, + ) + + { "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 + + def do_blog_get(params : HTTP::Params) + @context.models.blog.get( + post_id: params["post_id"].to_i64 + ).reduce({} of String => String) do |r, kv| + r[kv[0]] = kv[1].to_s + r + end + end + end + + module UserAPI + def do_user_add(params : HTTP::Params) + user_id = @context.models.user.add( + name: params["name"], + email: params["email"], + password: params["password"], + active: (params["active"] == "t"), + role: params["role"], + ) + + { "user_id": user_id } + end + + def do_user_get(params : HTTP::Params) + @context.models.user.get(params["user_id"].to_i64) + end + + + def do_user_set(params : HTTP::Params) + @context.models.user.set( + user_id: params["user_id"].to_i64, + name: params["name"]?, + email: params["email"]?, + password: (params["password"]? && params["password"].size > 0) ? params["password"] : nil, + active: params["active"]? ? (params["active"] == "t") : nil, + role: params["role"]?, + ) + + nil + end + + def do_user_get_users(params : HTTP::Params) + @context.models.user.get_users + end + end + + module SiteAPI + def do_site_get_sites(params : HTTP::Params) + @context.models.site.get_sites + end + end +end |