stuff
This commit is contained in:
@@ -2,6 +2,21 @@ package CW::Controller;
|
||||
|
||||
use Moo;
|
||||
|
||||
has dbh => ( is => 'ro', required => 1 );
|
||||
use Types::Standard qw(HashRef);
|
||||
|
||||
has dbh => ( is => 'ro', required => 1 );
|
||||
has request => ( is => 'ro', required => 1 );
|
||||
has response => ( is => 'ro', required => 1 );
|
||||
has params => (
|
||||
is => 'ro',
|
||||
isa => HashRef,
|
||||
default => sub { return +{} }
|
||||
);
|
||||
|
||||
sub evac {
|
||||
my ( $self, $code ) = @_;
|
||||
$self->response->status($code);
|
||||
$self->response->template("Error::$code");
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
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 } );
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ use Types::Standard qw(HashRef InstanceOf);
|
||||
has template => (
|
||||
is => 'ro',
|
||||
required => 1,
|
||||
isa => InstanceOf ['CW::Template']
|
||||
isa => sub {
|
||||
ref($_) && ref($_) =~ /^CW::Template/;
|
||||
}
|
||||
);
|
||||
|
||||
has ctx => (
|
||||
|
||||
@@ -14,7 +14,6 @@ sub render {
|
||||
if ( !$ctx->{nav} ) {
|
||||
$ctx->{nav} = [];
|
||||
my @path = split '/', $ctx->{env}->{PATH_INFO};
|
||||
|
||||
if (@path) {
|
||||
my $nav = [];
|
||||
for ( 0 .. $#path ) {
|
||||
@@ -41,7 +40,10 @@ sub render {
|
||||
push @additional_links, $self->_make_nav_link("logout");
|
||||
}
|
||||
|
||||
my $board = $ctx->{board} // 'cw';
|
||||
my $board = 'cw';
|
||||
if ( $ctx->{board} ) {
|
||||
$board = $ctx->{board}->{id};
|
||||
}
|
||||
|
||||
return [
|
||||
head => [
|
||||
@@ -68,18 +70,18 @@ sub render {
|
||||
rel => 'stylesheet'
|
||||
},
|
||||
link => {
|
||||
href => '/static/css/' . $board . '.css',
|
||||
href => '/static/css/cw.css',
|
||||
rel => 'stylesheet'
|
||||
}
|
||||
],
|
||||
body => [
|
||||
header => [
|
||||
header => { style => "margin-top: 1rem" } => [
|
||||
div => [
|
||||
div => {
|
||||
class => "header-wrapper"
|
||||
} => [
|
||||
img => {
|
||||
src => "/static/images/$board.svg",
|
||||
src => "/static/images/cw.svg",
|
||||
class => "logo"
|
||||
},
|
||||
div => [
|
||||
@@ -108,18 +110,27 @@ sub render {
|
||||
],
|
||||
]
|
||||
],
|
||||
hr => { style => 'margin: 1rem; display: block;' },
|
||||
main => [
|
||||
nav => { class => "quicknav" } => [
|
||||
map {
|
||||
(
|
||||
li => [
|
||||
"/",
|
||||
a => { href => $_->{path} } => [ $_->{text} ]
|
||||
]
|
||||
)
|
||||
} @{ $ctx->{nav} }
|
||||
],
|
||||
div => $self->template->render
|
||||
(
|
||||
@{ $ctx->{nav} }
|
||||
? (
|
||||
nav => { class => "quicknav" } => [
|
||||
map {
|
||||
(
|
||||
li => [
|
||||
"/",
|
||||
a => { href => $_->{path} } =>
|
||||
[ $_->{text} ]
|
||||
]
|
||||
)
|
||||
} @{ $ctx->{nav} }
|
||||
]
|
||||
)
|
||||
: ()
|
||||
),
|
||||
div => { class => "main-content-wrapper" } =>
|
||||
$self->template->render
|
||||
],
|
||||
footer => []
|
||||
]
|
||||
|
||||
26
lib/CW/Migration/05.pm
Normal file
26
lib/CW/Migration/05.pm
Normal file
@@ -0,0 +1,26 @@
|
||||
package CW::Migration::05;
|
||||
|
||||
use Moo;
|
||||
|
||||
extends 'CW::Migration';
|
||||
|
||||
sub id {
|
||||
return 5;
|
||||
}
|
||||
|
||||
sub sql {
|
||||
return q[CREATE TABLE posts (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
board_id VARCHAR(3) NOT NULL,
|
||||
user_id INT NOT NULL,
|
||||
parent_id INT,
|
||||
title VARCHAR(24),
|
||||
image VARCHAR(44) NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
1;
|
||||
@@ -11,7 +11,7 @@ has _request => (
|
||||
is => 'ro',
|
||||
init_arg => 'request',
|
||||
required => 1,
|
||||
handles => [qw(env)]
|
||||
handles => [qw(env query_parameters)]
|
||||
);
|
||||
|
||||
sub new_response {
|
||||
|
||||
@@ -15,6 +15,7 @@ has _router => (
|
||||
{
|
||||
## no critic [TestingAndDebugging::ProhibitNoStrict]
|
||||
no strict 'refs';
|
||||
|
||||
for (qw(get post put patch delete)) {
|
||||
*{ __PACKAGE__ . '::' . $_ } = sub {
|
||||
my ( $self, $path ) = @_;
|
||||
@@ -23,6 +24,10 @@ has _router => (
|
||||
|
||||
my $route = CW::Route->new( path => $path );
|
||||
$self->_router->connect( $path, { route => $route } );
|
||||
if ( $path !~ /.*\/$/ ) {
|
||||
$self->_router->connect( "$path/", { route => $route } );
|
||||
}
|
||||
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
|
||||
35
lib/CW/Template/Board.pm
Normal file
35
lib/CW/Template/Board.pm
Normal file
@@ -0,0 +1,35 @@
|
||||
package CW::Template::Board;
|
||||
|
||||
use Moo;
|
||||
|
||||
extends 'CW::Template';
|
||||
|
||||
sub render {
|
||||
my ($self) = @_;
|
||||
|
||||
my $board_id = $self->ctx->{board}->{id};
|
||||
my $posts = $self->ctx->{posts};
|
||||
|
||||
return [
|
||||
div => { class => "board-catalog-wrapper" } => [
|
||||
map {
|
||||
(
|
||||
div => { class => "board-catalog-post" } => [
|
||||
img => {
|
||||
class => "board-catalog-post-img",
|
||||
src => "/static/images/userdata/" . $_->{image}
|
||||
},
|
||||
a => { href => "/" . $board_id . "/" . $_->{id} } => [
|
||||
h4 => { class => "board-catalog-post-title" } =>
|
||||
[ $_->{title} ]
|
||||
],
|
||||
p => { class => "board-catalog-post-body" } =>
|
||||
[ $_->{body} ]
|
||||
]
|
||||
)
|
||||
} @$posts
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
1;
|
||||
20
lib/CW/Template/Error/404.pm
Normal file
20
lib/CW/Template/Error/404.pm
Normal file
@@ -0,0 +1,20 @@
|
||||
package CW::Template::Error::404;
|
||||
|
||||
use Moo;
|
||||
|
||||
extends 'CW::Template';
|
||||
|
||||
sub render {
|
||||
return [
|
||||
h2 => { style => "margin: 0" } =>
|
||||
[q[You are searching for something that seemingly doesn't exist.]],
|
||||
div => [
|
||||
p => { style => "margin: 0" } =>
|
||||
["Perhaps this is the sign you needed to create it yourself?"],
|
||||
img =>
|
||||
{ class => 'alexandria', src => "/static/images/alexandria.jpg" }
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
1;
|
||||
28
lib/CW/Template/Error/500.pm
Normal file
28
lib/CW/Template/Error/500.pm
Normal file
@@ -0,0 +1,28 @@
|
||||
package CW::Template::Error::500;
|
||||
|
||||
use Moo;
|
||||
|
||||
extends 'CW::Template';
|
||||
|
||||
sub render {
|
||||
my ($self) = @_;
|
||||
return [
|
||||
h2 => { style => "margin: 0" } =>
|
||||
[q[Something has gone terribly wrong.]],
|
||||
div => [
|
||||
p => { style => "margin: 0" } =>
|
||||
["Please re-try whatever it is you were doing, later."],
|
||||
img => {
|
||||
class => 'alexandria',
|
||||
src => "/static/images/alexandria_fire.jpg"
|
||||
},
|
||||
(
|
||||
$self->ctx->{show_errors}
|
||||
? ( p => [ $self->ctx->{error} ] )
|
||||
: ()
|
||||
)
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
1;
|
||||
@@ -9,18 +9,32 @@ sub render {
|
||||
|
||||
my @boards = @{ $self->ctx->{boards} };
|
||||
return [
|
||||
map {
|
||||
(
|
||||
li => [
|
||||
a => {
|
||||
href => '/' . $_->{id} . '/',
|
||||
class => 'index-board-link'
|
||||
} => [ '/' . $_->{id} . '/ - ' . $_->{name} ],
|
||||
p => { class => 'index-board-description' } =>
|
||||
[ $_->{description} ]
|
||||
]
|
||||
)
|
||||
} @boards
|
||||
div => { class => "index-content" } => [
|
||||
h2 => ["Boards"],
|
||||
ul => { class => "board-list" } => [
|
||||
map {
|
||||
(
|
||||
li => { class => "board-list-item" } => [
|
||||
a => {
|
||||
href => '/' . $_->{id} . '/',
|
||||
class => 'index-board-link'
|
||||
} => [ '/' . $_->{id} . '/ - ' . $_->{name} ],
|
||||
|
||||
# p => {
|
||||
# style => "margin: 0",
|
||||
# class => 'index-board-description'
|
||||
# } => [ $_->{description} ]
|
||||
]
|
||||
)
|
||||
} @boards
|
||||
]
|
||||
],
|
||||
div => { class => "index-content" } => [
|
||||
h2 => ["Changelog"],
|
||||
ul => { class => 'changelog-list' } => [
|
||||
li => ["Absofuckinglutely nothing"]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user