Skip to content

Commit

Permalink
People can follow blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
symkat committed Nov 25, 2021
1 parent 55d6499 commit a5771b2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
12 changes: 12 additions & 0 deletions DB/lib/BlogDB/DB/Result/Person.pm
Expand Up @@ -323,4 +323,16 @@ sub get_settings {
return $return;
}

sub is_following_blog {
my ( $self, $blog_id ) = @_;

return 0 unless $blog_id;

my $count = $self->search_related('person_follow_blog_maps', {
blog_id => $blog_id,
})->count;

return $count >= 1 ? 1 : 0;
}

1;
2 changes: 2 additions & 0 deletions Web/lib/BlogDB/Web.pm
Expand Up @@ -99,6 +99,8 @@ sub startup ($self) {
$r->get ( '/blog/e/:slug' )->to( 'Blog#get_edit_blog' )->name( 'edit_blog');
$r->post ( '/blog/e/:slug' )->to( 'Blog#post_edit_blog' )->name( 'do_edit_blog');

$r->post ( '/blog/follow' )->to( 'Blog#post_blog_follow' )->name( 'do_follow_blog' );
$r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
#
# $auth->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List pending blogs for approval.
Expand Down
23 changes: 19 additions & 4 deletions Web/lib/BlogDB/Web/Controller/Blog.pm
Expand Up @@ -78,13 +78,28 @@ sub post_edit_blog ($c) {
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
}

sub post_follow ($c) {
$c->set_template( 'blog/index' );
sub post_blog_follow ($c) {
my $blog = $c->db->resultset('Blog')->find($c->param('blog_id'));

# TODO: Throw an error if we don't have {person}->id, or $blog.
$c->stash->{person}->create_related('person_follow_blog_maps', {
blog_id => $blog->id,
});

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

sub post_unfollow ($c) {
$c->set_template( 'blog/index' );
sub post_blog_unfollow ($c) {
my $blog = $c->db->resultset('Blog')->find($c->param('blog_id'));

# TODO: Throw an error if we don't have {person}->id, or $blog.

$c->db->resultset('PersonFollowBlogMap')->search({
person_id => $c->stash->{person}->id,
blog_id => $blog->id,
})->delete;

$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );

}

Expand Down
17 changes: 13 additions & 4 deletions Web/templates/default/blog/index.html.tx
Expand Up @@ -19,10 +19,19 @@
<h3>[% $blog.tagline %]</h3>
</div>
<div class="col">
<form method="post" action="[% $c.url_for( 'do_follow_blog' ) %]">
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
<button type="submit" class="btn btn-primary float-end">Follow Blog</button>
</form>
%% if ( $person ) {
%% if ( $person.is_following_blog($blog.id) ) {
<form method="post" action="[% $c.url_for( 'do_unfollow_blog' ) %]">
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
<button type="submit" class="btn btn-primary float-end">Unfollow Blog</button>
</form>
%% } else {
<form method="post" action="[% $c.url_for( 'do_follow_blog' ) %]">
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
<button type="submit" class="btn btn-primary float-end">Follow Blog</button>
</form>
%% }
%% }
</div>
</div>

Expand Down

0 comments on commit a5771b2

Please sign in to comment.