Skip to content

Commit 5296354

Browse files
committed
View Blog
1 parent d5fa85d commit 5296354

File tree

6 files changed

+160
-18
lines changed

6 files changed

+160
-18
lines changed

DB/lib/BlogDB/DB/Result/Blog.pm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,18 @@ __PACKAGE__->has_many(
227227

228228

229229
# You can replace this text with custom code or comments, and it will be preserved on regeneration
230+
231+
sub tags {
232+
my ( $self ) = @_;
233+
234+
return [map {
235+
+{
236+
id => $_->tag->id,
237+
name => $_->tag->name,
238+
is_adult => $_->tag->is_adult,
239+
}
240+
} $self->search_related('blog_tag_maps', {})->all];
241+
}
242+
243+
230244
1;

Web/lib/BlogDB/Web.pm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ sub startup ($self) {
8989
$auth->post( '/user/settings/email' )->to( 'User#post_email' )->name( 'do_user_email' );
9090

9191
# /blog/ routes
92-
$r->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List new blogs.
93-
$r->post ( '/blog/new' )->to( 'Blog#post_new_blog' )->name( 'do_new_blog' ); # Create a new blog.
94-
$r->get ( '/blog/new/:id' )->to( 'Blog#get_edit_new_blog' )->name( 'edit_new_blog' ); # Show edit a new blog page.
95-
$r->post ( '/blog/new/:id' )->to( 'Blog#post_edit_new_blog')->name( 'do_edit_new_blog' ); # Update a new blog.
92+
$r->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List new blogs.
93+
$r->post ( '/blog/new' )->to( 'Blog#post_new_blog' )->name( 'do_new_blog' ); # Create a new blog.
94+
$r->get ( '/blog/new/:id' )->to( 'Blog#get_edit_new_blog' )->name( 'edit_new_blog' ); # Show edit a new blog page.
95+
$r->post ( '/blog/new/:id' )->to( 'Blog#post_edit_new_blog' )->name( 'do_edit_new_blog' ); # Update a new blog.
96+
$r->post ( '/blog/publish/:id' )->to( 'Blog#post_publish_new_blog')->name( 'do_publish_new_blog' ); # Publish (PendingBlog -> Blog.)
97+
98+
$r->get ( '/blog/v/:slug' )->to( 'Blog#get_view_blog' )->name( 'view_blog');
9699

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

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@ package BlogDB::Web::Controller::Blog;
22
use Mojo::Base 'Mojolicious::Controller', -signatures;
33
use Data::UUID;
44

5-
sub get_blog ($c) {
5+
sub _slug_to_id ($self, $slug) {
6+
# /:id
7+
# /:id-
8+
# /:id-anything
9+
10+
if ( $slug =~ /^(\d+)-?$/ ) {
11+
return $1;
12+
} elsif ( $slug =~ /^(\d+)-.+$/ ) {
13+
return $1;
14+
}
15+
16+
return "";
17+
}
18+
19+
sub get_view_blog ($c) {
620
$c->set_template( 'blog/index' );
721

22+
my $blog_id = $c->_slug_to_id($c->param('slug'));
23+
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find( $blog_id );
24+
825
}
926

1027
sub post_follow ($c) {
@@ -145,7 +162,32 @@ sub post_edit_new_blog ($c) {
145162

146163
# Send the user back to the standard GET path.
147164
$c->redirect_to( $c->url_for( 'edit_new_blog', id => $blog->id ) );
165+
}
166+
167+
sub post_publish_new_blog ($c) {
168+
my $pb = $c->db->resultset('PendingBlog')->find( $c->param('id') );
169+
170+
my $blog = $c->db->resultset('Blog')->create({
171+
title => $pb->title,
172+
url => $pb->url,
173+
img_url => $pb->img_url,
174+
rss_url => $pb->rss_url,
175+
tagline => $pb->tagline,
176+
about => $pb->about,
177+
is_adult => $pb->is_adult,
178+
});
179+
180+
my @tags = $pb->search_related('pending_blog_tag_maps')->all;
181+
182+
foreach my $tag ( @tags ) {
183+
$blog->create_related('blog_tag_maps', {
184+
tag_id => $tag->tag_id,
185+
});
186+
$tag->delete;
187+
}
188+
$pb->delete;
148189

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

151193
1;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
%% cascade default::_::layout { title => $blog.title,
2+
%%
3+
%% }
4+
5+
%% override panel -> {
6+
7+
<nav aria-label="breadcrumb">
8+
<ol class="breadcrumb">
9+
<li class="breadcrumb-item"><a href="#">Home</a></li>
10+
<li class="breadcrumb-item"><a href="#">Blogs</a></li>
11+
<li class="breadcrumb-item active" aria-current="page">[% $blog.url %]</li>
12+
</ol>
13+
</nav>
14+
15+
16+
<div class="row">
17+
<div class="col">
18+
<h1>[% $blog.title %]</h1>
19+
<h3>[% $blog.tagline %]</h3>
20+
</div>
21+
<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>
26+
</div>
27+
</div>
28+
29+
<div class="row">
30+
<div class="col">
31+
<p>[% $blog.about %]</p>
32+
<p>X readers follow</p>
33+
34+
%% for $blog.tags -> $tag {
35+
[% $tag.name %]
36+
%% }
37+
38+
</div>
39+
<div class="col">
40+
<img width="80%" src="/blog_screenshots/[% $blog.id %].jpg">
41+
</div>
42+
</div>
43+
44+
<ul class="nav nav-tabs">
45+
<li class="nav-item">
46+
<a class="nav-link active" aria-current="page" href="#">Reviews</a>
47+
</li>
48+
<li class="nav-item">
49+
<a class="nav-link" href="#">Latest Posts</a>
50+
</li>
51+
</ul>
52+
53+
<form method="post" action="[% $c.url_for( 'do_review_blog' ) %]">
54+
<input type="hidden" name="blog_id" value="[% $blog.id %]">
55+
56+
<textarea name="comment" rows="4">[% $form_comment %]</textarea>
57+
58+
<div class="form-check form-switch">
59+
<input class="form-check-input" type="checkbox" value="1" name="review_pos" id="review_pos" [% $form_review_pos ? " checked " : "" %]>
60+
<label class="form-check-label" for="review_pos">This is a positive review. I recommend this blog.</label>
61+
</div>
62+
63+
<div class="form-check form-switch">
64+
<input class="form-check-input" type="checkbox" value="1" name="review_neg" id="review_neg" [% $form_review_neg ? " checked " : "" %]>
65+
<label class="form-check-label" for="review_neg">This is a negative review. I dislike this blog.</label>
66+
</div>
67+
68+
<button type="submit" class="btn btn-primary float-end">Post Review</button>
69+
</form>
70+
71+
<div class="row" style="height: 20em;"></div>
72+
73+
74+
%% }

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
%% override panel -> {
66

77
%% for $blogs -> $blog {
8-
[% $blog.title %]
8+
<div class="card" style="width: 20rem;">
9+
<img src="/blog_screenshots/[% $blog.id %].jpg" class="card-img-top" alt="Screenshot">
10+
<div class="card-body">
11+
<h5 class="card-title">[% $blog.title %]</h5>
12+
<p class="card-text">
13+
[% $blog.tagline %] <br />
14+
About: [% $blog.about %]<br/>
15+
</p>
16+
<a href="[% $c.url_for( 'edit_new_blog', id => $blog.id ) %]" class="btn btn-primary">View Edit Page</a>
17+
</div>
18+
</div>
919

1020
%% }
1121

Web/templates/default/blog/new/item.html.tx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
%% }
44

55
%% override panel -> {
6+
<h3>[% $blog_obj.title %]</h3>
67

78

8-
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
<h3>[% $blog_url %]</h3>
9+
<div class="row" style="margin: 2em">
10+
<div class="col">
11+
<form method="post" action="[% $c.url_for( 'do_publish_new_blog', id => $blog_obj.id ) %]">
12+
<button type="submit" class="btn btn-primary float-end">Publish Blog</button>
13+
</form>
14+
</div>
15+
<div class="col">
16+
</div>
17+
</div>
1918

2019
<div class="row">
2120
<!-- Error Handling On LHS -->
@@ -35,7 +34,7 @@
3534

3635
<div class="row">
3736
<div class="col">
38-
<form method="post" action="[% $c.url_for( 'do_edit_new_blog' ) %]">
37+
<form method="post" action="[% $c.url_for( 'do_edit_new_blog', id => $blog_obj.id ) %]">
3938

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

0 commit comments

Comments
 (0)