From 2cded0bd02eb859063da4bc6878c173ee5d2b6ff Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Thu, 28 Jul 2016 20:57:55 -0400 Subject: add ThemeAPI and theme.delete --- src/guff/apis.cr | 12 ++++++++ src/guff/handlers.cr | 1 + src/guff/models/theme.cr | 72 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/guff/apis.cr b/src/guff/apis.cr index f5424dd..c801158 100644 --- a/src/guff/apis.cr +++ b/src/guff/apis.cr @@ -228,4 +228,16 @@ module Guff::APIs ) end end + + module ThemeAPI + def do_theme_add(params : HTTP::Params) + # TODO: + Theme::Installer.run(@context, "/path/to/file") + nil + end + + def do_theme_list(params : HTTP::Params) + @context.models.theme.all + end + end end diff --git a/src/guff/handlers.cr b/src/guff/handlers.cr index ce52b6a..d6c526f 100644 --- a/src/guff/handlers.cr +++ b/src/guff/handlers.cr @@ -102,6 +102,7 @@ module Guff::Handlers APIs::BlogAPI, APIs::SiteAPI, APIs::FileAPI, + APIs::ThemeAPI, ] include_api_modules(API_MODULES) diff --git a/src/guff/models/theme.cr b/src/guff/models/theme.cr index 76d9ea6..5b24a71 100644 --- a/src/guff/models/theme.cr +++ b/src/guff/models/theme.cr @@ -123,6 +123,45 @@ class Guff::Models::ThemeModel < Guff::Models::Model ORDER BY c.name, b.sort_order ", + + reset_page_themes: " + UPDATE pages + + SET theme_id = NULL + + WHERE theme_id = ? + ", + + reset_site_themes: " + UPDATE sites + + SET theme_id = (SELECT theme_id + FROM themes + WHERE theme_slug = 'default' + AND is_system + AND theme_hash IS NULL) + + WHERE theme_id = ? + ", + + delete_theme_assets: " + DELETE FROM theme_assets + WHERE file_id IN (SELECT file_id + FROM theme_files + WHERE theme_id = ?) + ", + + delete_theme_files: " + DELETE FROM theme_files WHERE theme_id = ? + ", + + delete_theme_data: " + DELETE FROM theme_data WHERE theme_id = ? + ", + + delete_theme: " + DELETE FROM themes WHERE theme_id = ? + ", } def initialize(context : Context) @@ -135,7 +174,7 @@ class Guff::Models::ThemeModel < Guff::Models::Model } end - def all(with_metadata : Bool = false) + def all # get rows rows = [] of Hash(String, String) @@ -150,6 +189,37 @@ class Guff::Models::ThemeModel < Guff::Models::Model rows end + DELETE_QUERIES = %i{ + reset_page_themes + reset_site_themes + delete_theme_assets + delete_theme_files + delete_theme_data + delete_theme + } + + # + # FIXME: this makes me uneasy + # + def delete(theme_id : Int32) + theme = get(theme_id: theme_id) + raise "cannot delete system theme" if is_system?(theme) + + # cache db handle + db = @context.dbs.rw + + db.transaction do + # cache theme id + id = theme_id.to_s + + # walk and exec delete queries + # FIXME: should make this a delete trigger + DELETE_QUERIES.each do |query_id| + db.query(SQL[query_id], [id]) + end + end + end + def get( theme_id : Int32? = nil, slug : String? = nil, -- cgit v1.2.3