From b8cf933a5b18c8dfe8bd5a47aaf08c1b5ee1bad5 Mon Sep 17 00:00:00 2001 From: Blog Manager Robot Date: Sat, 29 Oct 2022 20:11:58 +0000 Subject: [PATCH] Remove post/page buttons, handler consistency --- Web/lib/MJB/Web.pm | 19 +++++-- Web/lib/MJB/Web/Controller/Dashboard.pm | 57 +++++++++++++++---- Web/templates/dashboard/blog_pages.html.ep | 7 +++ Web/templates/dashboard/blog_post.html.ep | 16 +++--- ..._create.html.ep => blog_post_edit.html.ep} | 16 +++--- Web/templates/dashboard/blog_posts.html.ep | 11 +++- .../lib/MJB/Backend/Jekyll.pm | 21 +++++++ 7 files changed, 115 insertions(+), 32 deletions(-) rename Web/templates/dashboard/{blog_post_create.html.ep => blog_post_edit.html.ep} (69%) diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 5f215e0..f6275f2 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -75,6 +75,13 @@ sub startup ($self) { my $blog = $auth->under( '/dashboard/blog/:id' => sub ( $c ) { my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + # Make sure that a blog can be loaded. + if ( ! $blog ) { + $c->redirect_to( $c->url_for( 'show_dashboard' ) ); + return undef; + } + + # Make sure the current user owns the blog that has been loaded. if ( $blog->person->id ne $c->stash->{person}->id ) { $c->redirect_to( $c->url_for( 'show_dashboard' ) ); return undef; @@ -128,12 +135,15 @@ sub startup ($self) { # Dashboard / Blog Management $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); + + # This Dashboard $blog route starts at /dashboard/blog/:id $blog->get ( '/' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); $blog->get ( '/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); - $blog->get ( '/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); - $blog->post( '/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); - $blog->get ( '/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); - $blog->post( '/post' )->to('Dashboard#do_blog_post_create' )->name('do_dashboard_blog_post_create' ); + $blog->get ( '/post' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); + $blog->post( '/post' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); + $blog->get ( '/post/edit' )->to('Dashboard#blog_post_edit' )->name('show_dashboard_blog_post_edit' ); + $blog->post( '/post/edit' )->to('Dashboard#do_blog_post_edit' )->name('do_dashboard_blog_post_edit' ); + $blog->post( '/post/remove' )->to('Dashboard#do_blog_post_remove' )->name('do_dashboard_blog_post_remove' ); $blog->get ( '/settings' )->to('Dashboard#blog_settings' )->name('show_dashboard_blog_settings' ); $blog->post( '/settings' )->to('Dashboard#do_blog_settings' )->name('do_dashboard_blog_settings' ); $blog->get ( '/config' )->to('Dashboard#blog_config' )->name('show_dashboard_blog_config' ); @@ -149,6 +159,7 @@ sub startup ($self) { $blog->post( '/page' )->to('Dashboard#do_blog_page' )->name('do_dashboard_blog_page' ); $blog->get ( '/page/edit' )->to('Dashboard#blog_page_edit' )->name('show_dashboard_blog_page_edit' ); $blog->post( '/page/edit' )->to('Dashboard#do_blog_page_edit' )->name('do_dashboard_blog_page_edit' ); + $blog->post( '/page/remove' )->to('Dashboard#do_blog_page_remove' )->name('do_dashboard_blog_page_remove' ); # Blog Creation $auth->get ( '/blog' )->to('Blog#create' )->name('show_blog_create' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index 4598488..fa14860 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -2,32 +2,47 @@ package MJB::Web::Controller::Dashboard; use Mojo::Base 'Mojolicious::Controller', -signatures; sub index ($c) { - push @{$c->stash->{blogs}}, $c->stash->{person}->search_related('blogs')->all; - } sub blog ( $c ) { my $blog = $c->stash->{blog}; } -# TODO: There is a lot of repetition here, check out making a chain -# like the auth ones, but for loading up the blog and ensuring -# the user has access to it? sub blog_posts ( $c ) { my $blog = $c->stash->{blog}; my $blog_posts = $c->stash->{blog_posts} = [ map { $_->read } @{$c->jekyll($blog->domain->name)->list_posts} ]; } -sub blog_post ( $c ) { +sub blog_post_edit ( $c ) { my $blog = $c->stash->{blog}; my $post = $c->stash->{post} = $c->jekyll($blog->domain->name)->get_post( $c->param('mdfile') ); } -sub do_blog_post ( $c ) { +sub do_blog_post_remove ( $c ) { + my $blog = $c->stash->{blog}; + + my $jekyll = $c->jekyll($blog->domain->name); + my $post = $jekyll->get_post( $c->param('file') ); + + $jekyll->remove_markdown_file( $post ); + + my $build_job_id = $c->minion->enqueue( 'deploy_blog', [ $blog->id ], { + notes => { '_bid_' . $blog->id => 1 }, + priority => $blog->build_priority, + }); + $blog->create_related( 'builds', { job_id => $build_job_id } ); + + $c->flash( confirmation => "That post has been removed." ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_posts', { id => $blog->id } ) ); +} + + + +sub do_blog_post_edit ( $c ) { my $blog = $c->stash->{blog}; my $title = $c->stash->{form_title} = $c->param('postTitle'); @@ -53,7 +68,7 @@ sub do_blog_post ( $c ) { $c->redirect_to( $c->url_for( 'show_dashboard_blog_posts', { id => $blog->id } ) ); } -sub blog_post_create ( $c ) { +sub blog_post ( $c ) { my $blog = $c->stash->{blog}; } @@ -73,7 +88,7 @@ sub _make_slug ( $date, $title ) { } -sub do_blog_post_create ( $c ) { +sub do_blog_post ( $c ) { my $blog = $c->stash->{blog}; my $title = $c->stash->{form_title} = $c->param('postTitle'); @@ -259,6 +274,29 @@ sub blog_page_edit ( $c ) { } +sub do_blog_page_remove ( $c ) { + my $blog = $c->stash->{blog}; + + my $jekyll = $c->jekyll($blog->domain->name); + + my $blog_pages = $c->stash->{blog_pages} = [ map { $_->read } @{$jekyll->list_pages} ]; + + my $rel_path = $c->param('file'); + + my ( $page ) = grep { $_->rel_path eq $rel_path } @{$blog_pages}; + + $jekyll->remove_markdown_file( $page ); + + my $build_job_id = $c->minion->enqueue( 'deploy_blog', [ $blog->id ], { + notes => { '_bid_' . $blog->id => 1 }, + priority => $blog->build_priority, + }); + $blog->create_related( 'builds', { job_id => $build_job_id } ); + + $c->flash( confirmation => "That page has been removed." ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_pages', { id => $blog->id } ) ); +} + sub do_blog_page_edit ( $c ) { my $blog = $c->stash->{blog}; @@ -317,7 +355,6 @@ sub do_blog_page ( $c ) { $c->flash( confirmation => "Created Page " . $page->filename . "!" ); $c->redirect_to( $c->url_for( 'show_dashboard_blog_pages', { id => $blog->id } ) ); - } 1; diff --git a/Web/templates/dashboard/blog_pages.html.ep b/Web/templates/dashboard/blog_pages.html.ep index 3a1536d..c4effb2 100644 --- a/Web/templates/dashboard/blog_pages.html.ep +++ b/Web/templates/dashboard/blog_pages.html.ep @@ -43,6 +43,7 @@ Path Type Edit + Delete @@ -51,6 +52,12 @@ <%= $page->filename %> <%= $page->headers->{layout} %> Edit Post + +
+ + +
+ % } diff --git a/Web/templates/dashboard/blog_post.html.ep b/Web/templates/dashboard/blog_post.html.ep index 47a89f5..9a04fdd 100644 --- a/Web/templates/dashboard/blog_post.html.ep +++ b/Web/templates/dashboard/blog_post.html.ep @@ -1,16 +1,16 @@ % layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; -%= include 'dashboard/_blog_nav', page => 'edit' +%= include 'dashboard/_blog_nav', page => 'create' -
+
- +
- -
+ +
- +
@@ -18,11 +18,11 @@ Post Content - See Markdown Syntax Docs - +
- +
diff --git a/Web/templates/dashboard/blog_post_create.html.ep b/Web/templates/dashboard/blog_post_edit.html.ep similarity index 69% rename from Web/templates/dashboard/blog_post_create.html.ep rename to Web/templates/dashboard/blog_post_edit.html.ep index 9a04fdd..47a89f5 100644 --- a/Web/templates/dashboard/blog_post_create.html.ep +++ b/Web/templates/dashboard/blog_post_edit.html.ep @@ -1,16 +1,16 @@ % layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; -%= include 'dashboard/_blog_nav', page => 'create' +%= include 'dashboard/_blog_nav', page => 'edit' -
+
- +
- -
+ +
- +
@@ -18,11 +18,11 @@ Post Content - See Markdown Syntax Docs - +
- +
diff --git a/Web/templates/dashboard/blog_posts.html.ep b/Web/templates/dashboard/blog_posts.html.ep index f2eaa44..4b9a44b 100644 --- a/Web/templates/dashboard/blog_posts.html.ep +++ b/Web/templates/dashboard/blog_posts.html.ep @@ -28,7 +28,7 @@
- + New Post @@ -43,6 +43,7 @@ Title Date Edit + Delete @@ -50,7 +51,13 @@ <%= $post->headers->{title} %> <%= $post->headers->{date} %> - Edit Post + Edit Post + +
+ + +
+ % } diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index 3ac688d..4df203c 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -134,6 +134,27 @@ sub list_posts { return [ @files ]; } +sub remove_markdown_file { + my ( $self, $file ) = @_; + + # Update the origin that is set + $self->system_command( [ qw( git rm ), $file->path ], { + chdir => $self->repo_path, + }); + + # Commit The Changes + $self->system_command( [ qw( git commit -m ), 'Removed ' . $file->filename ], { + chdir => $self->repo_path, + }); + + # Push the changes + $self->system_command( [ qw( git push origin master ) ], { + chdir => $self->repo_path, + }); + + return $self; +} + sub list_pages { my ( $self ) = @_;