From 0557ef64e2bd1161fd98a5dff6e45aea8e0d7567 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 30 Jul 2016 15:10:14 -0400 Subject: add site assets and remove set_default --- src/guff/apis.cr | 23 ++++++-- src/guff/models/site.cr | 106 +++++++++++++++++++++++++++++++--- src/views/dialogs/admin/page-edit.ecr | 4 +- src/views/dialogs/admin/site-add.ecr | 42 +++++++++++++- 4 files changed, 160 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/guff/apis.cr b/src/guff/apis.cr index 3754545..5449c72 100644 --- a/src/guff/apis.cr +++ b/src/guff/apis.cr @@ -200,6 +200,9 @@ module Guff::APIs # get site_id site_id = params["site_id"].to_i64 + # get site assets + assets = @context.models.site.assets(site_id) + @context.models.site.get(site_id).reduce( {} of String => String ) do |r, kv| @@ -207,6 +210,8 @@ module Guff::APIs r end.merge({ "domains" => @context.models.site.domains(site_id), + "styles" => assets.styles, + "scripts" => assets.scripts, } of String => Array(String)) end @@ -217,9 +222,13 @@ module Guff::APIs slug: params["slug"], lang: params["lang"], body: params["body"], + is_full_feed: (params["is_full_feed"] == "t"), theme_id: params["theme_id"].to_i64, + domains: Array(String).from_json(params["domains"]), + styles: Array(String).from_json(params["styles"]), + scripts: Array(String).from_json(params["scripts"]), ) } end @@ -227,22 +236,28 @@ module Guff::APIs def do_site_set(params : HTTP::Params) @context.models.site.set( site_id: params["site_id"].to_i64, + name: params["name"]?, slug: params["slug"]?, lang: params["lang"]?, body: params["body"]?, is_full_feed: (params.includes?("is_full_key")) ? (params["is_full_feed"] == "t") : nil, theme_id: params["theme_id"]? ? params["theme_id"].to_i64 : nil, + domains: params["domains"]? ? Array(String).from_json(params["domains"]) : nil, + styles: params["styles"]? ? Array(String).from_json(params["styles"]) : nil, + scripts: params["scripts"]? ? Array(String).from_json(params["scripts"]) : nil, ) nil end - def do_site_set_default(params : HTTP::Params) - @context.models.site.set(params["site_id"].to_i64) - nil - end +# +# def do_site_set_default(params : HTTP::Params) +# @context.models.site.set(params["site_id"].to_i64) +# nil +# end +# end module FileAPI diff --git a/src/guff/models/site.cr b/src/guff/models/site.cr index 4ea639e..9eaed1a 100644 --- a/src/guff/models/site.cr +++ b/src/guff/models/site.cr @@ -68,11 +68,27 @@ class Guff::Models::SiteModel < Guff::Models::Model domains: " SELECT domain + FROM site_domains + WHERE site_id = ? + ORDER BY sort_order ", + assets: " + SELECT b.name AS type_name, + a.path + + FROM site_assets a + JOIN asset_types b + ON (b.type_id = a.type_id) + + WHERE a.site_id = ? + + ORDER BY a.sort_order + ", + get_name: " SELECT name @@ -105,10 +121,34 @@ class Guff::Models::SiteModel < Guff::Models::Model INSERT INTO site_domains(site_id, domain, sort_order) VALUES (?, ?, ?) ", - set_default: " - UPDATE sites - SET is_default = (site_id = ?) + set_assets_clear: " + DELETE FROM site_assets + WHERE site_id = ? + AND type_id = (SELECT type_id + FROM asset_types + WHERE name = ?) + ", + + set_assets_add: " + INSERT INTO site_assets ( + site_id, + type_id, + path, + sort_order + ) VALUES ( + ?, + (SELECT type_id FROM asset_types WHERE name = ?), + ?, + ? + ) ", + +# +# set_default: " +# UPDATE sites +# SET is_default = (site_id = ?) +# ", +# } def get_id(host : String?) : Int64? @@ -163,6 +203,8 @@ class Guff::Models::SiteModel < Guff::Models::Model is_full_feed : Bool? = nil, theme_id : Int64? = nil, domains : Array(String)? = nil, + styles : Array(String)? = nil, + scripts : Array(String)? = nil, ) : Int64 db = @context.dbs.rw site_id = -1_i64 @@ -177,6 +219,8 @@ class Guff::Models::SiteModel < Guff::Models::Model lang: lang, is_full_feed: is_full_feed, domains: domains, + styles: styles, + scripts: scripts, ) # create files directory @@ -200,6 +244,8 @@ class Guff::Models::SiteModel < Guff::Models::Model is_full_feed : Bool? = nil, theme_id : Int64? = nil, domains : Array(String)? = nil, + styles : Array(String)? = nil, + scripts : Array(String)? = nil, ) sql = [] of String args = [] of String @@ -238,6 +284,8 @@ class Guff::Models::SiteModel < Guff::Models::Model end set_domains(site_id, domains) + set_assets(site_id, "style", styles) + set_assets(site_id, "script", scripts) slug.try do |slug| # rename files directory @@ -276,14 +324,56 @@ class Guff::Models::SiteModel < Guff::Models::Model nil end - def set_default(site_id : Int64) - db = @context.dbs.rw + def assets(site_id : Int64) : Theme::Assets + r = Theme::Assets.new + + @context.dbs.ro.all(SQL[:assets], [site_id.to_s]) do |row| + case row["type_name"].to_s + when "script" + r.scripts << row["path"].to_s + when "style" + r.styles << row["path"].to_s + else + # never reached + raise "unknown type: #{row["type_name"]}" + end + end - db.transaction do - db.query(SQL[:set_default], [site_id.to_s]) - get_default_id + # return result + r + end + + private def set_assets( + site_id : Int64, + type : String, + paths : Array(String)? = nil, + ) + paths.try do |paths| + db = @context.dbs.rw + id = site_id.to_s + + db.transaction do + db.query(SQL[:set_assets_clear], [id, type]) + + paths.each_with_index do |path, i| + db.query(SQL[:set_assets_add], [id, type, path, i.to_s]) + end + end end nil end + +# +# def set_default(site_id : Int64) +# db = @context.dbs.rw +# +# db.transaction do +# db.query(SQL[:set_default], [site_id.to_s]) +# get_default_id +# end +# +# nil +# end +# end diff --git a/src/views/dialogs/admin/page-edit.ecr b/src/views/dialogs/admin/page-edit.ecr index e44cf86..1eb609a 100644 --- a/src/views/dialogs/admin/page-edit.ecr +++ b/src/views/dialogs/admin/page-edit.ecr @@ -207,7 +207,7 @@ @@ -224,7 +224,7 @@ diff --git a/src/views/dialogs/admin/site-add.ecr b/src/views/dialogs/admin/site-add.ecr index 86fd7df..0f99364 100644 --- a/src/views/dialogs/admin/site-add.ecr +++ b/src/views/dialogs/admin/site-add.ecr @@ -129,7 +129,7 @@ role='tabpanel' >
-
+
+
+
+ +
+
+
+ + + + +

+ Newline-delimited site styles. +

+
+
+ +
+
+ + + + +

+ Newline-delimited site scripts. +

+
-- cgit v1.2.3