Skip to content

Commit 55d6499

Browse files
committed
Edit Blog Interface
1 parent e785e28 commit 55d6499

File tree

3 files changed

+156
-4
lines changed

3 files changed

+156
-4
lines changed

Web/lib/BlogDB/Web.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ sub startup ($self) {
9696
$r->post ( '/blog/publish/:id' )->to( 'Blog#post_publish_new_blog')->name( 'do_publish_new_blog' ); # Publish (PendingBlog -> Blog.)
9797

9898
$r->get ( '/blog/v/:slug' )->to( 'Blog#get_view_blog' )->name( 'view_blog');
99+
$r->get ( '/blog/e/:slug' )->to( 'Blog#get_edit_blog' )->name( 'edit_blog');
100+
$r->post ( '/blog/e/:slug' )->to( 'Blog#post_edit_blog' )->name( 'do_edit_blog');
99101

100102
# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
101103
#

Web/lib/BlogDB/Web/Controller/Blog.pm

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,63 @@ sub _slug_to_id ($self, $slug) {
1919
sub get_view_blog ($c) {
2020
$c->set_template( 'blog/index' );
2121

22-
my $blog_id = $c->_slug_to_id($c->param('slug'));
23-
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find( $blog_id );
22+
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find(
23+
$c->_slug_to_id($c->param('slug'))
24+
);
25+
}
26+
27+
sub get_edit_blog ($c) {
28+
$c->set_template( 'blog/edit' );
29+
30+
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find(
31+
$c->_slug_to_id($c->param('slug'))
32+
);
33+
34+
$c->stash->{form_title} = $blog->title;
35+
$c->stash->{form_url} = $blog->url;
36+
$c->stash->{form_rss_url} = $blog->rss_url;
37+
$c->stash->{form_tagline} = $blog->tagline;
38+
$c->stash->{form_about} = $blog->about;
39+
$c->stash->{form_adult} = $blog->is_adult;
2440

41+
# I should add this to the PendingBlog/Blog models - all tags + checked / not checked status.
42+
my %seen = map { $_->tag_id => 1 } $blog->search_related('blog_tag_maps', {})->all;
43+
foreach my $tag ( $c->db->resultset('Tag')->all ) {
44+
push @{$c->stash->{tags}}, {
45+
id => $tag->id,
46+
name => $tag->name,
47+
checked => $seen{$tag->id} ? 1 : 0,
48+
};
49+
50+
}
51+
}
52+
53+
sub post_edit_blog ($c) {
54+
$c->set_template( 'blog/edit' );
55+
56+
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find(
57+
$c->_slug_to_id($c->param('slug'))
58+
);
59+
60+
$blog->title ( $c->param('title') );
61+
$blog->url ( $c->param('url') );
62+
$blog->rss_url ( $c->param('rss_url') );
63+
$blog->tagline ( $c->param('tagline') );
64+
$blog->about ( $c->param('about') );
65+
$blog->is_adult( $c->param('is_adult') ? 1 : 0 );
66+
67+
$blog->update;
68+
69+
# Remove all tags, then add the tags we have set.
70+
$blog->search_related('blog_tag_maps')->delete;
71+
foreach my $tag_id ( @{$c->every_param('tags')}) {
72+
$blog->create_related('blog_tag_maps', {
73+
tag_id => $tag_id,
74+
});
75+
}
76+
77+
# Send the user back to the standard GET path.
78+
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
2579
}
2680

2781
sub post_follow ($c) {
@@ -117,17 +171,16 @@ sub get_edit_new_blog ($c) {
117171
name => $tag->name,
118172
checked => $seen{$tag->id} ? 1 : 0,
119173
};
120-
121174
}
122175

176+
123177
foreach my $post ( $blog->search_related('pending_blog_entries')->all ) {
124178
push @{$c->stash->{posts}}, {
125179
title => $post->title,
126180
url => $post->url,
127181
date => $post->publish_date,
128182
};
129183
}
130-
131184
}
132185

133186
sub post_edit_new_blog ($c) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
%% cascade default::_::layout { title => 'Edit ' ~ $blog_url,
3+
%%
4+
%% }
5+
6+
%% override panel -> {
7+
<h3>[% $blog.title %]</h3>
8+
9+
10+
<div class="row" style="margin: 2em">
11+
<div class="col">
12+
<form method="post" action="[% $c.url_for( 'do_publish_new_blog', id => $blog.id ) %]">
13+
<button type="submit" class="btn btn-primary float-end">Publish Blog</button>
14+
</form>
15+
</div>
16+
<div class="col">
17+
</div>
18+
</div>
19+
20+
<div class="row">
21+
<!-- Error Handling On LHS -->
22+
<div class="col">
23+
%% if ( $errors.size() ) {
24+
<div style="margin-top: 2em" class="alert alert-danger" role="alert">
25+
There were errors with your request that could not be resolved:
26+
<ul>
27+
%% for $errors -> $error {
28+
<li>[% $error %]</li>
29+
%% }
30+
</ul>
31+
</div>
32+
%% }
33+
</div>
34+
</div>
35+
36+
<div class="row">
37+
<div class="col">
38+
<form method="post" action="[% $c.url_for( 'do_edit_blog', slug => $blog.slug ) %]">
39+
40+
%% include 'default/_/form/input.tx' { type => 'text', name => 'title',
41+
%% title => 'Title',
42+
%% help => 'The title of the blog',
43+
%% value => $form_title,
44+
%% };
45+
46+
%% include 'default/_/form/input.tx' { type => 'text', name => 'tagline',
47+
%% title => 'Tagline',
48+
%% help => 'The tagline of the blog.',
49+
%% value => $form_tagline,
50+
%% };
51+
52+
<textarea name="about" rows="4">[% $form_about %]</textarea>
53+
54+
55+
<br />
56+
57+
<div class="form-check form-switch">
58+
59+
<input class="form-check-input" type="checkbox" value="1" name="is_adult" id="is_adult" [% $form_adult ? " checked " : "" %]>
60+
<label class="form-check-label" for="is_adult">Adult Content</label>
61+
</div>
62+
63+
%% for $tags -> $tag {
64+
<div class="form-check">
65+
<input class="form-check-input" type="checkbox" value="[% $tag.id %]" name="tags" id="tag_[% $tag.id %]" [% $tag.checked ? " checked " : "" %]>
66+
<label class="form-check-label" for="tag_[% $tag.id %]">[% $tag.name %]</label>
67+
</div>
68+
%% }
69+
<br />
70+
%% include 'default/_/form/input.tx' { type => 'text', name => 'url',
71+
%% title => 'Homepage URL',
72+
%% help => 'The url of the blog',
73+
%% value => $form_url,
74+
%% };
75+
76+
%% include 'default/_/form/input.tx' { type => 'text', name => 'rss_url',
77+
%% title => 'RSS URL',
78+
%% help => 'A URL to an RSS feed for the blog.',
79+
%% value => $form_rss_url,
80+
%% };
81+
82+
<button type="submit" class="btn btn-primary float-end">Update Blog</button>
83+
84+
</form>
85+
</div>
86+
<div class="col">
87+
<img width="80%" src="[% $blog.img_url %]">
88+
</div>
89+
</div>
90+
<div class="row">
91+
<h2>Recent Posts</h2>
92+
%% for $blog.posts -> $post {
93+
<a href="[% $post.url %]" target="_blank" alt="[% $post.date %]">[% $post.title %]</a>
94+
%% }
95+
</div>
96+
97+
%% }

0 commit comments

Comments
 (0)