Skip to content

Commit

Permalink
View Blog
Browse files Browse the repository at this point in the history
  • Loading branch information
symkat committed Nov 23, 2021
1 parent d5fa85d commit 5296354
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 18 deletions.
14 changes: 14 additions & 0 deletions DB/lib/BlogDB/DB/Result/Blog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,18 @@ __PACKAGE__->has_many(


# You can replace this text with custom code or comments, and it will be preserved on regeneration

sub tags {
my ( $self ) = @_;

return [map {
+{
id => $_->tag->id,
name => $_->tag->name,
is_adult => $_->tag->is_adult,
}
} $self->search_related('blog_tag_maps', {})->all];
}


1;
11 changes: 7 additions & 4 deletions Web/lib/BlogDB/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ sub startup ($self) {
$auth->post( '/user/settings/email' )->to( 'User#post_email' )->name( 'do_user_email' );

# /blog/ routes
$r->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List new blogs.
$r->post ( '/blog/new' )->to( 'Blog#post_new_blog' )->name( 'do_new_blog' ); # Create a new blog.
$r->get ( '/blog/new/:id' )->to( 'Blog#get_edit_new_blog' )->name( 'edit_new_blog' ); # Show edit a new blog page.
$r->post ( '/blog/new/:id' )->to( 'Blog#post_edit_new_blog')->name( 'do_edit_new_blog' ); # Update a new blog.
$r->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List new blogs.
$r->post ( '/blog/new' )->to( 'Blog#post_new_blog' )->name( 'do_new_blog' ); # Create a new blog.
$r->get ( '/blog/new/:id' )->to( 'Blog#get_edit_new_blog' )->name( 'edit_new_blog' ); # Show edit a new blog page.
$r->post ( '/blog/new/:id' )->to( 'Blog#post_edit_new_blog' )->name( 'do_edit_new_blog' ); # Update a new blog.
$r->post ( '/blog/publish/:id' )->to( 'Blog#post_publish_new_blog')->name( 'do_publish_new_blog' ); # Publish (PendingBlog -> Blog.)

$r->get ( '/blog/v/:slug' )->to( 'Blog#get_view_blog' )->name( 'view_blog');

# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
#
Expand Down
44 changes: 43 additions & 1 deletion Web/lib/BlogDB/Web/Controller/Blog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@ package BlogDB::Web::Controller::Blog;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use Data::UUID;

sub get_blog ($c) {
sub _slug_to_id ($self, $slug) {
# /:id
# /:id-
# /:id-anything

if ( $slug =~ /^(\d+)-?$/ ) {
return $1;
} elsif ( $slug =~ /^(\d+)-.+$/ ) {
return $1;
}

return "";
}

sub get_view_blog ($c) {
$c->set_template( 'blog/index' );

my $blog_id = $c->_slug_to_id($c->param('slug'));
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find( $blog_id );

}

sub post_follow ($c) {
Expand Down Expand Up @@ -145,7 +162,32 @@ sub post_edit_new_blog ($c) {

# Send the user back to the standard GET path.
$c->redirect_to( $c->url_for( 'edit_new_blog', id => $blog->id ) );
}

sub post_publish_new_blog ($c) {
my $pb = $c->db->resultset('PendingBlog')->find( $c->param('id') );

my $blog = $c->db->resultset('Blog')->create({
title => $pb->title,
url => $pb->url,
img_url => $pb->img_url,
rss_url => $pb->rss_url,
tagline => $pb->tagline,
about => $pb->about,
is_adult => $pb->is_adult,
});

my @tags = $pb->search_related('pending_blog_tag_maps')->all;

foreach my $tag ( @tags ) {
$blog->create_related('blog_tag_maps', {
tag_id => $tag->tag_id,
});
$tag->delete;
}
$pb->delete;

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

1;
74 changes: 74 additions & 0 deletions Web/templates/default/blog/index.html.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%% cascade default::_::layout { title => $blog.title,
%%
%% }

%% override panel -> {

<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item"><a href="#">Blogs</a></li>
<li class="breadcrumb-item active" aria-current="page">[% $blog.url %]</li>
</ol>
</nav>


<div class="row">
<div class="col">
<h1>[% $blog.title %]</h1>
<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>
</div>
</div>

<div class="row">
<div class="col">
<p>[% $blog.about %]</p>
<p>X readers follow</p>

%% for $blog.tags -> $tag {
[% $tag.name %]
%% }

</div>
<div class="col">
<img width="80%" src="/blog_screenshots/[% $blog.id %].jpg">
</div>
</div>

<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Reviews</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Latest Posts</a>
</li>
</ul>

<form method="post" action="[% $c.url_for( 'do_review_blog' ) %]">
<input type="hidden" name="blog_id" value="[% $blog.id %]">

<textarea name="comment" rows="4">[% $form_comment %]</textarea>

<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" value="1" name="review_pos" id="review_pos" [% $form_review_pos ? " checked " : "" %]>
<label class="form-check-label" for="review_pos">This is a positive review. I recommend this blog.</label>
</div>

<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" value="1" name="review_neg" id="review_neg" [% $form_review_neg ? " checked " : "" %]>
<label class="form-check-label" for="review_neg">This is a negative review. I dislike this blog.</label>
</div>

<button type="submit" class="btn btn-primary float-end">Post Review</button>
</form>

<div class="row" style="height: 20em;"></div>


%% }
12 changes: 11 additions & 1 deletion Web/templates/default/blog/new/index.html.tx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
%% override panel -> {

%% for $blogs -> $blog {
[% $blog.title %]
<div class="card" style="width: 20rem;">
<img src="/blog_screenshots/[% $blog.id %].jpg" class="card-img-top" alt="Screenshot">
<div class="card-body">
<h5 class="card-title">[% $blog.title %]</h5>
<p class="card-text">
[% $blog.tagline %] <br />
About: [% $blog.about %]<br/>
</p>
<a href="[% $c.url_for( 'edit_new_blog', id => $blog.id ) %]" class="btn btn-primary">View Edit Page</a>
</div>
</div>

%% }

Expand Down
23 changes: 11 additions & 12 deletions Web/templates/default/blog/new/item.html.tx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
%% }

%% override panel -> {
<h3>[% $blog_obj.title %]</h3>












<h3>[% $blog_url %]</h3>
<div class="row" style="margin: 2em">
<div class="col">
<form method="post" action="[% $c.url_for( 'do_publish_new_blog', id => $blog_obj.id ) %]">
<button type="submit" class="btn btn-primary float-end">Publish Blog</button>
</form>
</div>
<div class="col">
</div>
</div>

<div class="row">
<!-- Error Handling On LHS -->
Expand All @@ -35,7 +34,7 @@

<div class="row">
<div class="col">
<form method="post" action="[% $c.url_for( 'do_edit_new_blog' ) %]">
<form method="post" action="[% $c.url_for( 'do_edit_new_blog', id => $blog_obj.id ) %]">

%% include 'default/_/form/input.tx' { type => 'text', name => 'title',
%% title => 'Title',
Expand Down

0 comments on commit 5296354

Please sign in to comment.