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 @@