stuff
This commit is contained in:
59
lib/CW/Controller/Board.pm
Normal file
59
lib/CW/Controller/Board.pm
Normal file
@@ -0,0 +1,59 @@
|
||||
package CW::Controller::Board;
|
||||
|
||||
use Moo;
|
||||
|
||||
extends 'CW::Controller';
|
||||
|
||||
has page_size => (
|
||||
is => 'lazy',
|
||||
default => sub { 32 }
|
||||
);
|
||||
|
||||
sub index {
|
||||
my ($self) = @_;
|
||||
|
||||
my $board_identifier = $self->params->{board};
|
||||
|
||||
if ( !$board_identifier ) {
|
||||
return $self->evac(404);
|
||||
}
|
||||
|
||||
my $board =
|
||||
$self->dbh->selectrow_hashref( "SELECT * FROM boards WHERE id = ?",
|
||||
undef, $board_identifier );
|
||||
|
||||
if ( !$board ) {
|
||||
return $self->evac(404);
|
||||
}
|
||||
|
||||
my ($total_posts) = $self->dbh->selectrow_array(
|
||||
"SELECT COUNT(id) FROM posts WHERE board_id = ? ORDER BY created_at",
|
||||
undef, $board_identifier );
|
||||
|
||||
my $query = $self->request->query_parameters;
|
||||
my $page = $query->{p} // 1;
|
||||
my $page_size = $self->page_size;
|
||||
my $offset = $page == 1 ? 0 : $page - 1;
|
||||
|
||||
my $posts = $self->dbh->selectall_arrayref(
|
||||
"SELECT * FROM posts WHERE board_id = ?"
|
||||
. " ORDER BY created_at LIMIT ? , ?",
|
||||
undef,
|
||||
$board_identifier,
|
||||
$page_size + 0,
|
||||
$page_size * $offset
|
||||
);
|
||||
|
||||
$self->response->template(
|
||||
'Board',
|
||||
{
|
||||
page => $page,
|
||||
page_size => $page_size,
|
||||
total_posts => $total_posts,
|
||||
board => $board,
|
||||
posts => $posts
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
1;
|
||||
@@ -5,13 +5,13 @@ use Moo;
|
||||
extends 'CW::Controller';
|
||||
|
||||
sub index {
|
||||
my ( $self, $request, $response ) = @_;
|
||||
my ($self) = @_;
|
||||
|
||||
my $boards =
|
||||
$self->dbh->selectall_arrayref( q[SELECT * FROM boards ORDER BY id],
|
||||
{ Slice => {} } );
|
||||
|
||||
$response->template( 'Index',
|
||||
$self->response->template( 'Index',
|
||||
{ title => 'compiled.world | home', boards => $boards } );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user