Skip to content

Commit

Permalink
Blog display tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
symkat committed Nov 24, 2021
1 parent 4d75ac0 commit 7b9f48f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
12 changes: 12 additions & 0 deletions DB/lib/BlogDB/DB/Result/Blog.pm
Expand Up @@ -251,5 +251,17 @@ sub slug {
return sprintf( "%d-%s", $self->id, $title );
}

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

return [ map {
+{
title => $_->title,
url => $_->url,
date => $_->publish_date,
}
} $self->search_related( 'blog_entries')->all ];
}


1;
35 changes: 34 additions & 1 deletion DB/lib/BlogDB/DB/Result/PendingBlog.pm
Expand Up @@ -237,6 +237,39 @@ __PACKAGE__->belongs_to(
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-11-24 04:45:56
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:F4GsyDnhsW34Ywl+BCrVEw

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

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

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

my $title = $self->title ? $self->title : $self->url;

$title = lc($title);
s/[^a-zA-Z0-9]/_/g, s/[_]+/_/g, s/^_//, s/_$// for $title;

return sprintf( "%d-%s", $self->id, $title );
}

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

return [ map {
+{
title => $_->title,
url => $_->url,
date => $_->publish_date,
}
} $self->search_related( 'pending_blog_entries')->all ];
}

# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;
25 changes: 18 additions & 7 deletions Web/lib/BlogDB/Web/Controller/Blog.pm
Expand Up @@ -89,7 +89,6 @@ sub post_new_blog ($c) {
),
});

$c->minion->enqueue( populate_blog_entires => [ $blog->id, 'pending' ]);
$c->minion->enqueue( populate_blog_screenshot => [ $blog->id, 'pending' ]);

$c->redirect_to( $c->url_for( 'edit_new_blog', id => $blog->id ) );
Expand All @@ -98,8 +97,8 @@ sub post_new_blog ($c) {
sub get_edit_new_blog ($c) {
$c->set_template( 'blog/new/item' );

my $blog_id = $c->stash->{blog_id} = $c->param('id');
my $blog = $c->stash->{blog_obj} = $c->db->resultset('PendingBlog')->find( $blog_id );
my $blog_id = $c->stash->{blog_id} = $c->param('id');
my $blog = $c->stash->{blog} = $c->db->resultset('PendingBlog')->find( $blog_id );


# Populate the form with the current values.
Expand All @@ -120,12 +119,21 @@ sub get_edit_new_blog ($c) {
};

}

foreach my $post ( $blog->search_related('pending_blog_entries')->all ) {
push @{$c->stash->{posts}}, {
title => $post->title,
url => $post->url,
date => $post->publish_date,
};
}

}

sub post_edit_new_blog ($c) {

my $blog_id = $c->stash->{blog_id} = $c->param('id');
my $blog = $c->stash->{blog_obj} = $c->db->resultset('PendingBlog')->find( $blog_id );
my $blog_id = $c->stash->{blog_id} = $c->param('id');
my $blog = $c->stash->{blog} = $c->db->resultset('PendingBlog')->find( $blog_id );


# TODO: This section should be guarded by checking that the user
Expand All @@ -147,6 +155,9 @@ sub post_edit_new_blog ($c) {

$blog->update;

# Get Posts from RSS Feed.
$c->minion->enqueue( populate_blog_entries => [ $blog->id, 'pending' ]);

# Remove all tags, then add the tags we have set.
$blog->search_related('pending_blog_tag_maps')->delete;
foreach my $tag_id ( @{$c->every_param('tags')}) {
Expand All @@ -173,16 +184,16 @@ sub post_publish_new_blog ($c) {
});

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->search_related('pending_blog_entries')->delete;
$pb->delete;

$c->minion->enqueue( populate_blog_entires => [ $blog->id, 'prod' ]);
$c->minion->enqueue( populate_blog_entries => [ $blog->id, 'prod' ]);
$c->minion->enqueue( populate_blog_screenshot => [ $blog->id, 'prod' ]);

$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
Expand Down
2 changes: 1 addition & 1 deletion Web/lib/BlogDB/Web/Plugin/MinionTasks.pm
Expand Up @@ -68,7 +68,7 @@ sub register ( $self, $app, $config ) {
$blog->update;
});

$app->minion->add_task(populate_blog_entires => sub ( $job, $blog_id, $type ) {
$app->minion->add_task(populate_blog_entries => sub ( $job, $blog_id, $type ) {
my $rs1 = $type eq 'pending' ? 'PendingBlog' : 'Blog';
my $rs2 = $type eq 'pending' ? 'PendingBlogEntry' : 'BlogEntry';
my $rs3 = $type eq 'pending' ? 'pending_blog_entries' : 'blog_entries';
Expand Down
9 changes: 8 additions & 1 deletion Web/templates/default/blog/index.html.tx
Expand Up @@ -37,7 +37,7 @@

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

Expand Down Expand Up @@ -68,6 +68,13 @@
<button type="submit" class="btn btn-primary float-end">Post Review</button>
</form>

<div class="row">
<h2>Recent Posts</h2>
%% for $blog.posts -> $post {
<a href="[% $post.url %]" target="_blank" alt="[% $post.date %]">[% $post.title %]</a>
%% }
</div>

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


Expand Down
14 changes: 10 additions & 4 deletions Web/templates/default/blog/new/item.html.tx
Expand Up @@ -3,12 +3,12 @@
%% }

%% override panel -> {
<h3>[% $blog_obj.title %]</h3>
<h3>[% $blog.title %]</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 ) %]">
<form method="post" action="[% $c.url_for( 'do_publish_new_blog', id => $blog.id ) %]">
<button type="submit" class="btn btn-primary float-end">Publish Blog</button>
</form>
</div>
Expand All @@ -34,7 +34,7 @@

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

%% include 'default/_/form/input.tx' { type => 'text', name => 'title',
%% title => 'Title',
Expand Down Expand Up @@ -83,8 +83,14 @@
</form>
</div>
<div class="col">
<img width="80%" src="[% $blog_obj.img_url %]">
<img width="80%" src="[% $blog.img_url %]">
</div>
</div>
<div class="row">
<h2>Recent Posts</h2>
%% for $blog.posts -> $post {
<a href="[% $post.url %]" target="_blank" alt="[% $post.date %]">[% $post.title %]</a>
%% }
</div>

%% }

0 comments on commit 7b9f48f

Please sign in to comment.