Compare commits

...

2 Commits

Author SHA1 Message Date
barnold 3dc4410342 Rename some things for clarity. 2022-09-18 17:31:01 +01:00
barnold a1aeb653bf Begin a view-author page. 2022-09-18 17:17:30 +01:00
8 changed files with 56 additions and 35 deletions

View File

@ -17,8 +17,9 @@ sub startup ($self) {
$r->get('/auth')->to('auth#index');
$r->post('/login')->to('auth#login');
$r->get('/logout')->to('auth#logout');
$r->get('/book/page/<page_number:num>')->to('book#index')->name('book_page');
$r->get('/author/page/<page_number:num>')->to('book#author')->name('author_page');
$r->get('/books/<page_number:num>')->to('book#books')->name('books');
$r->get('/authors/<page_number:num>')->to('book#authors')->name('authors');
$r->get('/author/<id:num>')->to('book#author')->name('author');
# Put a route under athentication.
my $auth = $r->under(

View File

@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;
use MyModel::Book;
use MyModel::Author;
sub index ($self) {
sub books ($self) {
my $page_number = $self->param('page_number') // 1;
my $book_model = MyModel::Book->new;
my $book_page = $book_model->get_page($page_number, $self->param('title_like'));
@ -13,7 +13,7 @@ sub index ($self) {
);
}
sub author ($self) {
sub authors ($self) {
my $page_number = $self->param('page_number') // 1;
my $author_model = MyModel::Author->new;
my $author_page = $author_model->get_page(
@ -26,4 +26,11 @@ sub author ($self) {
);
}
sub author ($self) {
my $id = $self->param('id');
$self->render(
author => MyModel::Author->new->find_by_id($id)
);
}
1;

View File

@ -19,5 +19,12 @@ sub get_page ($self, $page_number = 1, $name_like = undef) {
return $rs;
}
sub find_by_id ($self, $id) {
return $self->schema->resultset('Author')->find(
{ id => $id },
{ prefetch => [ "books" ] },
);
}
no Moose;
__PACKAGE__->meta->make_immutable;

View File

@ -11,7 +11,8 @@ $t->get_ok('/account')->status_is(302);
$t->get_ok('/auth')->status_is(200)->content_like(qr/login/i);
$t->post_ok('/login' => form => { logname => 'Dobby' });
$t->get_ok('/')->status_is(200)->content_like(qr/dobby/i);
$t->get_ok('/book/page/1')->status_is(200)->content_like(qr/[[:digit:]] books found/i);
$t->get_ok('/books/1')->status_is(200)->content_like(qr/[[:digit:]] books found/i);
$t->get_ok('/authors/1')->status_is(200)->content_like(qr/[[:digit:]] authors found/i);
$t->get_ok('/logout')->status_is(302);
# Can't do "content_unlike()" so check it offers login now.
$t->get_ok('/')->status_is(200)->content_like(qr/login/i);

View File

@ -1,29 +1,5 @@
% layout 'default';
% title 'Authors';
<h1>Author page</h1>
% title 'Author: ' . $author->name;
<h1>Author: <%= $author->name %></h1>
<%# On a new search, reset the page number to 1. %>
%= form_for author_page => { page_number => 1 } => begin
%= label_for name_like => 'Name like'
%= text_field name_like => flash('name_like')
%= submit_button "Search"
%= end
<p/>
<%= $pager->total_entries %> authors found.
<p/>
<div>
<table>
<tr>
<th>Name</th>
</tr>
% for my $author ($author_page->all) {
<tr>
<td><%= $author->name %></td>
</tr>
% }
</table>
</div>
%= include '_page-navigation'
<strong><%= $author->name %></strong> wrote <%= $author->books->count %> books.

View File

@ -0,0 +1,29 @@
% layout 'default';
% title 'Authors';
<h1>Author page</h1>
<%# On a new search, reset the page number to 1. %>
%= form_for authors => { page_number => 1 } => begin
%= label_for name_like => 'Name like'
%= text_field name_like => flash('name_like')
%= submit_button "Search"
%= end
<p/>
<%= $pager->total_entries %> authors found.
<p/>
<div>
<table>
<tr>
<th>Name</th>
</tr>
% for my $author ($author_page->all) {
<tr>
<td><%= link_to($author->name => 'author' => { id => $author->id}) %></td>
</tr>
% }
</table>
</div>
%= include '_page-navigation'

View File

@ -3,7 +3,7 @@
<h1>Book page</h1>
<%# On a new search, reset the page number to 1. %>
%= form_for book_page => { page_number => 1 } => begin
%= form_for books => { page_number => 1 } => begin
%= label_for title_like => 'Title like'
%= text_field title_like => flash('title_like')
%= submit_button "Search"

View File

@ -5,8 +5,8 @@
<p>Pages that don't require login:</p>
<p><%= link_to("About" => 'about') %></p>
<p><%= link_to("Books" => 'book_page' => { page_number => 1 }) %></p>
<p><%= link_to("Authors" => 'author_page' => { page_number => 1 }) %></p>
<p><%= link_to("Books" => 'books' => { page_number => 1 }) %></p>
<p><%= link_to("Authors" => 'authors' => { page_number => 1 }) %></p>
<p>For this, you'll need to log in, then MyApp will redirect you:</p>
<p><%= link_to("Account" => 'account') %></p>