-
Notifications
You must be signed in to change notification settings - Fork 3
/
DvdSimpleUrl.pm
93 lines (80 loc) · 2.57 KB
/
DvdSimpleUrl.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package DvdDatabase::Controller::DvdSimpleUrl;
use Moose;
use MooseX::NonMoose;
extends 'WebNano::Controller';
use DvdDatabase::Controller::Dvd::Form;
has record_methods => (
is => 'ro',
isa => 'HashRef',
default => sub { { view => 1, 'delete' => 1, edit => 1 } }
);
around 'local_dispatch' => sub {
my( $orig, $self, $path) = @_;
my( $id, $method, @args ) = split qr{/}, $path;
$method ||= 'view';
if( $id && $id =~ /^\d+$/ && $self->record_methods->{ $method } ){
my $rs = $self->application->schema->resultset( 'Dvd' );
my $record = $rs->find( $id );
if( ! $record ) {
my $res = $self->request->new_response(404);
$res->content_type('text/plain');
$res->body( 'No record with id: ' . $id );
return $res;
}
return $self->$method( $record, @args );
}
return $self->$orig( $path );
};
sub index_action {
my( $self ) = @_;
my $rs = $self->application->schema->resultset( 'Dvd' );
return $self->render( template => 'list.tt', items => [ $rs->search ] );
}
sub create_action {
my ( $self ) = @_;
my $req = $self->request;
my $form = DvdDatabase::Controller::Dvd::Form->new(
params => $req->parameters->as_hashref,
schema => $self->application->schema,
);
if( $req->method eq 'POST' && $form->process() ){
my $record = $form->item;
my $res = $req->new_response();
$res->redirect( $self->self_url . 'record/' . $record->id . '/view' );
return $res;
}
$form->field( 'submit' )->value( 'Create' );
return $self->render( template => 'edit.tt', form => $form->render );
}
sub view {
my ( $self, $record ) = @_;
return $self->render( template => 'record.tt', record => $record );
}
sub delete {
my ( $self, $record ) = @_;
if( $self->request->method eq 'GET' ){
return $self->render( record => $record );
}
else{
$record->delete;
my $res = $self->request->new_response();
$res->redirect( $self->self_url );
return $res;
}
}
sub edit {
my ( $self, $record ) = @_;
my $req = $self->request;
my $form = DvdDatabase::Controller::Dvd::Form->new(
item => $record,
params => $req->parameters->as_hashref,
);
if( $req->method eq 'POST' && $form->process() ){
my $res = $req->new_response();
$res->redirect( $self->self_url . '/' . $record->id . '/view' );
return $res;
}
$form->field( 'submit' )->value( 'Update' );
return $self->render( form => $form->render );
}
1;