1 |
package App::RoomReservation; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
|
7 |
use lib 'lib'; |
8 |
use App::RoomReservation::Room; |
9 |
|
10 |
sub dsn { 'DBI:Pg:dbname=room-reservation' } |
11 |
|
12 |
=head1 DEPLOY |
13 |
|
14 |
$ createdb --encoding=utf-8 room-reservation |
15 |
|
16 |
=cut |
17 |
|
18 |
our $dbh; |
19 |
sub dbh { |
20 |
my ($self) = @_; |
21 |
|
22 |
return $dbh if defined $dbh; |
23 |
|
24 |
$dbh = DBI->connect( $self->dsn, '', '', { RaiseError => 1 } ) || die $DBI::errstr; |
25 |
$dbh->do( qq{ set client_encoding='utf-8' } ) if $self->dsn =~ m{pg}i; |
26 |
|
27 |
return $dbh; |
28 |
} |
29 |
|
30 |
sub as_markup { |
31 |
my ($self) = @_; |
32 |
|
33 |
|
34 |
my $url = $self->url_for( 'Reservation/create_as_markup' ); |
35 |
|
36 |
return |
37 |
App::RoomReservation::Room->new->room_as_markup |
38 |
. qq|<a target="App::RoomReservation::Reservation" href="$url">seat reservation</a>| |
39 |
; |
40 |
} |
41 |
|
42 |
# my $url = $self->uri_for( 'Reservation/create_as_makrup' ); |
43 |
|
44 |
sub url_for { |
45 |
my ( $self, $to ) = @_; |
46 |
my $app = ref($self); |
47 |
$app =~ s{^(App::[^:]+)::.+$}{$1}; |
48 |
$app .= '::' . $to; |
49 |
|
50 |
my ( $class, $method ) = split(m{/}, $app); |
51 |
$method =~ s{\?.+$}{}; # remove arguments |
52 |
Class::MOP::load_class( $class ); |
53 |
die "$class doesn't implement $method" unless $class->meta->has_method($method); |
54 |
|
55 |
return |
56 |
$self->request_url->scheme . '://' . $self->request_url->authority . '/' . $app; |
57 |
} |
58 |
|
59 |
__PACKAGE__->meta->make_immutable; |
60 |
no Moose; |
61 |
|
62 |
1; |