Skip to content

Commit a5771b2

Browse files
committed
People can follow blogs
1 parent 55d6499 commit a5771b2

4 files changed

Lines changed: 46 additions & 8 deletions

File tree

DB/lib/BlogDB/DB/Result/Person.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,16 @@ sub get_settings {
323323
return $return;
324324
}
325325

326+
sub is_following_blog {
327+
my ( $self, $blog_id ) = @_;
328+
329+
return 0 unless $blog_id;
330+
331+
my $count = $self->search_related('person_follow_blog_maps', {
332+
blog_id => $blog_id,
333+
})->count;
334+
335+
return $count >= 1 ? 1 : 0;
336+
}
337+
326338
1;

Web/lib/BlogDB/Web.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ sub startup ($self) {
9999
$r->get ( '/blog/e/:slug' )->to( 'Blog#get_edit_blog' )->name( 'edit_blog');
100100
$r->post ( '/blog/e/:slug' )->to( 'Blog#post_edit_blog' )->name( 'do_edit_blog');
101101

102+
$r->post ( '/blog/follow' )->to( 'Blog#post_blog_follow' )->name( 'do_follow_blog' );
103+
$r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
102104
# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
103105
#
104106
# $auth->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List pending blogs for approval.

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,28 @@ sub post_edit_blog ($c) {
7878
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
7979
}
8080

81-
sub post_follow ($c) {
82-
$c->set_template( 'blog/index' );
81+
sub post_blog_follow ($c) {
82+
my $blog = $c->db->resultset('Blog')->find($c->param('blog_id'));
83+
84+
# TODO: Throw an error if we don't have {person}->id, or $blog.
85+
$c->stash->{person}->create_related('person_follow_blog_maps', {
86+
blog_id => $blog->id,
87+
});
8388

89+
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
8490
}
8591

86-
sub post_unfollow ($c) {
87-
$c->set_template( 'blog/index' );
92+
sub post_blog_unfollow ($c) {
93+
my $blog = $c->db->resultset('Blog')->find($c->param('blog_id'));
94+
95+
# TODO: Throw an error if we don't have {person}->id, or $blog.
96+
97+
$c->db->resultset('PersonFollowBlogMap')->search({
98+
person_id => $c->stash->{person}->id,
99+
blog_id => $blog->id,
100+
})->delete;
101+
102+
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
88103

89104
}
90105

Web/templates/default/blog/index.html.tx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@
1919
<h3>[% $blog.tagline %]</h3>
2020
</div>
2121
<div class="col">
22-
<form method="post" action="[% $c.url_for( 'do_follow_blog' ) %]">
23-
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
24-
<button type="submit" class="btn btn-primary float-end">Follow Blog</button>
25-
</form>
22+
%% if ( $person ) {
23+
%% if ( $person.is_following_blog($blog.id) ) {
24+
<form method="post" action="[% $c.url_for( 'do_unfollow_blog' ) %]">
25+
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
26+
<button type="submit" class="btn btn-primary float-end">Unfollow Blog</button>
27+
</form>
28+
%% } else {
29+
<form method="post" action="[% $c.url_for( 'do_follow_blog' ) %]">
30+
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
31+
<button type="submit" class="btn btn-primary float-end">Follow Blog</button>
32+
</form>
33+
%% }
34+
%% }
2635
</div>
2736
</div>
2837

0 commit comments

Comments
 (0)