aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-07-30 00:46:09 -0400
committerPaul Duncan <pabs@pablotron.org>2016-07-30 00:46:09 -0400
commit040cbfc93f870fd4242d112757bf1c52ddf7a2a0 (patch)
treef71599ed30a3a96021b3e335512a9e508caf9000 /src
parent183cc31c0b5f812c0f0fcd8a292e378a9695417f (diff)
downloadguff-040cbfc93f870fd4242d112757bf1c52ddf7a2a0.tar.bz2
guff-040cbfc93f870fd4242d112757bf1c52ddf7a2a0.zip
load and save custom page assets
Diffstat (limited to 'src')
-rw-r--r--src/guff/apis.cr24
-rw-r--r--src/guff/models/page.cr49
2 files changed, 65 insertions, 8 deletions
diff --git a/src/guff/apis.cr b/src/guff/apis.cr
index 5d87af2..8a84c1d 100644
--- a/src/guff/apis.cr
+++ b/src/guff/apis.cr
@@ -40,18 +40,30 @@ module Guff::APIs
have_theme_id: true,
theme_id: (params["theme_id"]? && params["theme_id"].size > 0) ? params["theme_id"].to_i32 : nil,
+
+ styles: (params["styles"]? && params["styles"] =~ /\S/) ? Array(String).from_json(params["styles"]) : nil,
+ scripts: (params["scripts"]? && params["scripts"] =~ /\S/) ? Array(String).from_json(params["scripts"]) : nil,
)
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
+ # cache post_id, get assets
+ post_id = params["post_id"].to_i64
+ assets = @context.models.page.assets(post_id)
+
+ ({
+ "styles" => assets.styles,
+ "scripts" => assets.scripts,
+ } of String => Array(String)).merge(
+ @context.models.page.get(
+ post_id: post_id
+ ).reduce({} of String => String) do |r, kv|
+ r[kv[0]] = kv[1].to_s
+ r
+ end
+ )
end
end
diff --git a/src/guff/models/page.cr b/src/guff/models/page.cr
index b7043a4..d01af01 100644
--- a/src/guff/models/page.cr
+++ b/src/guff/models/page.cr
@@ -117,6 +117,21 @@ class Guff::Models::PageModel < Guff::Models::Model
ORDER BY b.name, a.sort_order
",
+
+ set_assets_clear: "
+ DELETE FROM page_assets
+ WHERE post_id = ?
+ AND type_id = (SELECT type_id FROM asset_types WHERE name = ?)
+ ",
+
+ set_assets_add: "
+ INSERT INTO page_assets (
+ post_id,
+ type_id,
+ path,
+ sort_order
+ ) VALUES (?, (SELECT type_id FROM asset_types WHERE name = ?), ?, ?)
+ ",
}
LIMIT = 50
@@ -191,10 +206,16 @@ class Guff::Models::PageModel < Guff::Models::Model
have_theme_id : Bool = false,
theme_id : Int32? = nil,
+
+ styles : Array(String)? = nil,
+ scripts : Array(String)? = nil,
)
db = @context.dbs.rw
db.transaction do
+ # cache post_id string
+ id = post_id.to_s
+
@context.models.post.set(
post_id: post_id,
@@ -213,9 +234,33 @@ class Guff::Models::PageModel < Guff::Models::Model
if have_theme_id
if theme_id
- db.query(SQL[:set], [theme_id.to_s, post_id.to_s])
+ db.query(SQL[:set], [theme_id.to_s, id])
else
- db.query(SQL[:set_default], [post_id.to_s])
+ db.query(SQL[:set_default], [id])
+ end
+ end
+
+ set_assets(id, "style", styles)
+ set_assets(id, "script", scripts)
+ end
+ end
+
+ private def set_assets(
+ post_id : String,
+ type : String,
+ paths : Array(String)?,
+ )
+ if paths
+ # cache db handle
+ db = @context.dbs.rw
+
+ db.transaction do
+ # clear existing assets
+ db.query(SQL[:set_assets_clear], [post_id, type])
+
+ # add new assets
+ paths.not_nil!.each_with_index do |path, i|
+ db.query(SQL[:set_assets_add], [post_id, type, path, i.to_s])
end
end
end