From 7c97d1ca167a3fc55fdfcb89a2fe2a5e549585b6 Mon Sep 17 00:00:00 2001 From: Blog Manager Robot Date: Wed, 19 Oct 2022 02:07:25 +0000 Subject: [PATCH] Updating posts actually works now... --- Web/lib/MJB/Web.pm | 11 ++++--- Web/lib/MJB/Web/Controller/Dashboard.pm | 31 +++++++++++++++++++ Web/templates/dashboard/blog_post.html.ep | 2 +- Web/templates/dashboard/blog_posts.html.ep | 25 +++++++++++++++ .../lib/MJB/Backend/Jekyll.pm | 21 +++++++++++-- .../lib/MJB/Backend/Jekyll/MarkdownFile.pm | 1 + 6 files changed, 82 insertions(+), 9 deletions(-) diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 3b41da7..752e74b 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -114,11 +114,12 @@ sub startup ($self) { $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); # Dashboard - $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); - $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); - $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); - $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); - $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); + $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); + $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); + $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); + $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); + $auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); + $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); # Blog Management $auth->get ( '/blog/create' )->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 c49d999..545152d 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -54,6 +54,37 @@ sub blog_post ( $c ) { my $post = $c->stash->{post} = $c->jekyll($blog->domain->name)->get_post( $c->param('mdfile') ); } +sub do_blog_post ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $title = $c->stash->{form_title} = $c->param('postTitle'); + my $date = $c->stash->{form_date} = $c->param('postDate'); + my $content = $c->stash->{form_content} = $c->param('postContent'); + + my $jekyll = $c->jekyll($blog->domain->name); + my $post = $c->stash->{post} = $jekyll->get_post( $c->param('mdfile') ); + + $post->markdown( $content ); + $post->headers->{title} = $title; + $post->headers->{date} = $date; + + $jekyll->update_post( $post ); + + $c->minion->enqueue( 'deploy_blog', [ $blog->id ] ); + + $c->flash( confirmation => "Updated $title!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_posts', { id => $blog->id } ) ); +} + sub blog_post_create ( $c ) { my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); diff --git a/Web/templates/dashboard/blog_post.html.ep b/Web/templates/dashboard/blog_post.html.ep index 70d824a..81be301 100644 --- a/Web/templates/dashboard/blog_post.html.ep +++ b/Web/templates/dashboard/blog_post.html.ep @@ -21,7 +21,7 @@ -
+
diff --git a/Web/templates/dashboard/blog_posts.html.ep b/Web/templates/dashboard/blog_posts.html.ep index 4af14f6..ffda86d 100644 --- a/Web/templates/dashboard/blog_posts.html.ep +++ b/Web/templates/dashboard/blog_posts.html.ep @@ -17,6 +17,31 @@ +% if ( my $confirmation = flash 'confirmation' ) { + +% } + +% if ( $c->stash->{success} ) { + +% } + +% if ( $c->stash->{errors} ) { + +% } + + + % if ( $blog_posts ) { diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index 11f14df..db408a3 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -179,11 +179,26 @@ sub create_post { } sub update_post { - my ( $self, $file, $headers, $content ) = @_; + my ( $self, $md_file ) = @_; - $self->delete_post( $file ); + $md_file->write; + + # Add the file to git + $self->system_command( [ qw( git add ), $md_file->path ], { + chdir => $self->repo_path, + }); + + # Commit the file + $self->system_command( [ qw( git commit -m ), "Created " . $md_file->headers->{title} ], { + chdir => $self->repo_path, + }); - $self->create_post( $headers, $content ); + # Push the repo to the store server + $self->system_command( [ qw( git push origin master ) ], { + chdir => $self->repo_path, + }); + + return 1; } sub delete_post { diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm index d48a00b..99d573a 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm @@ -66,6 +66,7 @@ sub write { or die "Failed to open $file for writing: $!"; print $sf Dump($self->headers); + print $sf "---\n"; print $sf $self->markdown; close $sf;