Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Overriding the handle controller method

  • Loading branch information...
commit 0cde20cf53afce5a3d7259f5ea137985c526360a 1 parent bb98c87
Zbigniew Lukasiak authored
View
63 examples/DvdDatabase/lib/DvdDatabase/Controller/DvdSimpleUrl_HandlerOv.pm
@@ -0,0 +1,63 @@
+use strict;
+use warnings;
+
+package DvdDatabase::Controller::DvdSimpleUrl_HandlerOv;
+use Moose;
+use MooseX::NonMoose;
+
+extends 'WebNano::Controller';
+
+use DvdDatabase::Controller::Dvd::Form;
+use DvdDatabase::Controller::Dvd::Record;
+
+sub handle {
+ my ( $class, %args ) = @_;
+ my @path = @{ delete $args{path} };
+ my $id = $path[0];
+ if( $id && $id =~ /^\d+$/ ){
+ my $rs = $args{app}->schema->resultset( 'Dvd' );
+ my $record = $rs->find( $id );
+ if( ! $record ) {
+ my $res = Plack::Response->new(404);
+ $res->content_type('text/plain');
+ $res->body( 'No record with id: ' . $id );
+ return $res;
+ }
+ return DvdDatabase::Controller::Dvd::Record->handle(
+ %args,
+ path => [ @path[ 1 .. $#path ] ],
+ self_url => $args{self_url} . "$id/",
+ record => $record,
+ );
+ }
+ my $self = $class->new( %args );
+ return $self->local_dispatch( @path );
+};
+
+sub index_action {
+ my( $self ) = @_;
+ my $rs = $self->app->schema->resultset( 'Dvd' );
+ return $self->render( template => 'list.tt', items => [ $rs->search ] );
+}
+
+sub create_action {
+ my ( $self ) = @_;
+ my $req = $self->req;
+
+ my $form = DvdDatabase::Controller::Dvd::Form->new(
+ params => $req->parameters->as_hashref,
+ schema => $self->app->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 );
+}
+
+
+
+1;
View
3  examples/DvdDatabase/t/simple_url.t
@@ -9,7 +9,7 @@ use File::Copy;
use Plack::Middleware::Session;
use Test::WWW::Mechanize::PSGI;
-for my $controller( qw/DvdSimpleUrl DvdSimpleUrl_TwoClasses/ ){
+for my $controller( qw/DvdSimpleUrl_HandlerOv DvdSimpleUrl DvdSimpleUrl_TwoClasses/ ){
copy('t/data/dvdzbr.db','dvdzbr.db') or die "Copy failed: $!";
my $app = Plack::Middleware::Session->wrap( DvdDatabase->new()->psgi_app );
@@ -30,6 +30,7 @@ for my $controller( qw/DvdSimpleUrl DvdSimpleUrl_TwoClasses/ ){
is( $res->code, 404 , '404 for view with no record' );
$res = $cb->(GET "/$controller/555/view");
is( $res->code, 404 , '404 for view with no record' );
+# warn Dumper( $res ); use Data::Dumper;
}
);
}
View
6 examples/DvdDatabase/templates/DvdSimpleUrl_HandlerOv/edit.tt
@@ -0,0 +1,6 @@
+<html>
+<body>
+[% form %]
+</body>
+</html>
+
View
26 examples/DvdDatabase/templates/DvdSimpleUrl_HandlerOv/list.tt
@@ -0,0 +1,26 @@
+<html>
+<body>
+<table>
+<tr>
+<th>name</td>
+<th>creation_date</td>
+<td></td>
+<td></td>
+<td></td>
+</tr>
+[% FOREACH record IN items %]
+<tr>
+<td>
+[% record.name %]
+</td>
+<td>
+[% record.creation_date %]
+</td>
+<td><a href="[% self_url %][% record.id %]/view">View</a></td>
+<td><a href="[% self_url %][% record.id %]/edit">Edit</a></td>
+<td><a href="[% self_url %][% record.id %]/delete">Delete</a></td>
+</tr>
+[% END %]
+</body>
+</html>
+
Please sign in to comment.
Something went wrong with that request. Please try again.