diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-05-23 03:59:32 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-05-23 03:59:32 -0400 |
commit | 78c7ab1ee6cc86cba98844c3fb7356e355f85658 (patch) | |
tree | 2f4f24da12928d26d7a8a04a378702e2ac459510 /src | |
parent | 752be8d81d14b3a2a547dc9eb18976b4cce7545d (diff) | |
download | guff-78c7ab1ee6cc86cba98844c3fb7356e355f85658.tar.bz2 guff-78c7ab1ee6cc86cba98844c3fb7356e355f85658.zip |
add page, project, and blog apis
Diffstat (limited to 'src')
-rw-r--r-- | src/guff.cr | 249 |
1 files changed, 237 insertions, 12 deletions
diff --git a/src/guff.cr b/src/guff.cr index 00d6daa..c340cfe 100644 --- a/src/guff.cr +++ b/src/guff.cr @@ -219,6 +219,136 @@ module Guff end end + class PostModel < Model + SQL = { + add: " + INSERT INTO posts(site_id, created_by, state_id) VALUES + (?, ?, (SELECT state_id FROM states WHERE state = 'draft')) + ", + + set: " + UPDATE posts + SET %s + WHERE post_id = ? + ", + + unused: " + post_id INTEGER PRIMARY KEY, + + site_id INTEGER NOT NULL + REFERENCES sites(site_id), + + created_at TIMESTAMP WITH TIME ZONE NOT NULL + DEFAULT CURRENT_TIMESTAMP, + + created_by INTEGER NOT NULL + REFERENCES users(user_id), + + state_id INTEGER NOT NULL + REFERENCES states(state_id), + + posted_at TIMESTAMP WITH TIME ZONE, + expires_at TIMESTAMP WITH TIME ZONE, + + name TEXT NOT NULL DEFAULT '', + + slug TEXT NOT NULL DEFAULT '' CHECK ( + slug NOT LIKE '% %' AND + slug = LOWER(slug) + ), + + slug_lock BOOLEAN NOT NULL DEFAULT true, + + body TEXT NOT NULL DEFAULT '' + ", + } + + def add( + site_id : Int64, + user_id : Int64, + ) : Int64 + @context.dbs.rw.query(SQL[:add], [site_id.to_s, user_id.to_s]) + @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, + + 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, + ) + sets = [] of String + args = [] of String + + if site_id + sets << "site_id = ?" + args << site_id.to_s + end + + if state + sets << "state_id = (SELECT state_id FROM states WHERE state = ?)" + args << state + end + + if have_posted_at + if posted_at + sets << "posted_at = ?" + args << ISO8601_TIME.format(posted_at) + else + sets << "posted_at = NULL" + end + end + + if have_expires_at + if expires_at + sets << "expires_at = ?" + args << ISO8601_TIME.format(expires_at) + else + sets << "expires_at = NULL" + end + end + + if slug + sets << "slug = ?" + args << slug + end + + if slug_lock.not_nil? + sets << "slug_lock = ?" + args << (slug_lock ? "1" : "0") + end + + if name + sets << "name = ?" + args << name + end + + if body + sets << "body = ?" + args << body + end + + if sets.size > 0 + args << post_id.to_s + @context.dbs.rw.query(SQL[:set] % sets.join(","), args) + end + end + end + class PageModel < Model SQL = { get_id: " @@ -239,6 +369,11 @@ module Guff ORDER BY b.created_at DESC LIMIT 1 ", + + add: " + INSERT INTO pages(post_id, layout_id) + VALUES (?, (SELECT layout_id FROM layouts WHERE layout = 'default')) + ", } def get_id( @@ -248,6 +383,21 @@ module Guff r = @context.dbs.ro.one(SQL[:get_id], [site_id.to_s, slug]) r ? r.to_i64 : nil end + + def add( + site_id : Int64, + user_id : Int64, + ) : Int64 + db = @context.dbs.rw + post_id = -1_i64 + + db.transaction do + post_id = @context.models.post.add(site_id, user_id) + db.query(SQL[:add], [post_id.to_s]) + end + + post_id + end end class ProjectModel < Model @@ -270,6 +420,10 @@ module Guff ORDER BY b.created_at DESC LIMIT 1 ", + + add: " + INSERT INTO projects(post_id) VALUES (?) + ", } def get_id( @@ -279,6 +433,21 @@ module Guff r = @context.dbs.ro.one(SQL[:get_id], [site_id.to_s, slug]) r ? r.to_i64 : nil end + + def add( + site_id : Int64, + user_id : Int64, + ) : Int64 + db = @context.dbs.rw + post_id = -1_i64 + + db.transaction do + post_id = @context.models.post.add(site_id, user_id) + db.query(SQL[:add], [post_id.to_s]) + end + + post_id + end end class BlogModel < Model @@ -301,6 +470,10 @@ module Guff ORDER BY COALESCE(b.posted_at, b.created_at) DESC ", + + add: " + INSERT INTO blogs(post_id) VALUES (?) + ", } def get_id( @@ -362,6 +535,21 @@ module Guff # return results r end + + def add( + site_id : Int64, + user_id : Int64, + ) : Int64 + db = @context.dbs.rw + post_id = -1_i64 + + db.transaction do + post_id = @context.models.post.add(site_id, user_id) + db.query(SQL[:add], [post_id.to_s]) + end + + post_id + end end class UserModel < Model @@ -386,12 +574,12 @@ module Guff AND role_id IN (SELECT role_id FROM roles WHERE role IN (%s)) ", - add_user: " + add: " INSERT INTO users(role_id, name, email, password, is_active) VALUES ((SELECT role_id FROM roles where role = ?), ?, ?, ?, ?) ", - set_user: " + set: " UPDATE users SET %s WHERE user_id = ? @@ -423,14 +611,14 @@ module Guff ], [user_id.to_s].concat(roles)) end - def add_user( + def add( name : String, email : String, password : String, role : String, active : Bool, ) : Int64 - @context.dbs.rw.query(SQL[:add_user], [ + @context.dbs.rw.query(SQL[:add], [ role, name, email, @@ -441,7 +629,7 @@ module Guff @context.dbs.rw.last_insert_row_id.to_i64 end - def set_user( + def set( user_id : Int64, name : String? = nil, email : String? = nil, @@ -479,7 +667,7 @@ module Guff if sets.size > 0 args << user_id.to_s - @context.dbs.rw.query(SQL[:set_user] % sets.join(", "), args) + @context.dbs.rw.query(SQL[:set] % sets.join(", "), args) end end @@ -597,7 +785,7 @@ module Guff class SiteModel < Model SQL = { - get_site_id: " + get_id: " SELECT site_id FROM ( @@ -625,7 +813,7 @@ module Guff } def get_id(host : String?) : Int64? - r = @context.dbs.ro.one(SQL[:get_site_id], [host || ""]) + r = @context.dbs.ro.one(SQL[:get_id], [host || ""]) r ? r.to_i64 : nil end end @@ -639,6 +827,7 @@ module Guff user: Models::UserModel, session: Models::SessionModel, csrf: Models::CSRFModel, + post: Models::PostModel, page: Models::PageModel, project: Models::ProjectModel, blog: Models::BlogModel, @@ -776,9 +965,42 @@ module Guff end end + module PageAPI + def do_page_add(params : HTTP::Params) + post_id = @context.models.page.add( + site_id: params["site_id"].to_i64, + user_id: @context.user_id.not_nil!, + ) + + { "post_id": post_id } + end + end + + module ProjectAPI + def do_project_add(params : HTTP::Params) + post_id = @context.models.project.add( + site_id: params["site_id"].to_i64, + user_id: @context.user_id.not_nil!, + ) + + { "post_id": post_id } + end + end + + module BlogAPI + def do_blog_add(params : HTTP::Params) + post_id = @context.models.blog.add( + site_id: params["site_id"].to_i64, + user_id: @context.user_id.not_nil!, + ) + + { "post_id": post_id } + end + end + module UserAPI - def do_user_add_user(params : HTTP::Params) - user_id = @context.models.user.add_user( + def do_user_add(params : HTTP::Params) + user_id = @context.models.user.add( name: params["name"], email: params["email"], password: params["password"], @@ -789,8 +1011,8 @@ module Guff { "user_id": user_id } end - def do_user_set_user(params : HTTP::Params) - @context.models.user.set_user( + def do_user_set(params : HTTP::Params) + @context.models.user.set( user_id: params["user_id"].to_i64, name: params["name"]?, email: params["email"]?, @@ -987,6 +1209,9 @@ module Guff API_MODULES = [ APIs::PostAPI, APIs::UserAPI, + APIs::PageAPI, + APIs::ProjectAPI, + APIs::BlogAPI, ] include_api_modules(API_MODULES) |