Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 103 lines (89 sloc) 2.89 kb
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 93 94 95 96 97 98 99 100 101 102 103
package DvdDatabase::Controller::Dvd1;
use Moose;
use MooseX::NonMoose;

extends 'WebNano::Controller';

use DvdDatabase::Controller::Dvd::Form;

has record_methods => (
    is => 'ro',
    isa => 'HashRef',
    default => sub { { view_action => 1, 'delete_action' => 1, edit_action => 1 } }
);

sub record_action {
    my( $self, $id, $method, @args ) = @_;
    my $rs = $self->app->schema->resultset( 'Dvd' );
    my $record = $rs->find( $id );
    my @path = @{ $self->path };
    $self->path( [ $method, $record, @path[ 2 .. $#path ] ] );
    return $self->local_dispatch();
}

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 );
}

sub view_action {
    my ( $self, $record ) = @_;
    if( !$record || !blessed($record) ) {
        my $res = $self->req->new_response(404);
        $res->content_type('text/plain');
        $res->body( 'No record found' );
        return $res;
    }

    return $self->render( template => 'record.tt', record => $record );
}

sub delete_action {
    my ( $self, $record ) = @_;
    if( !$record || !blessed($record) ) {
        my $res = $self->req->new_response(404);
        $res->content_type('text/plain');
        $res->body( 'No record found' );
        return $res;
    }

    if( $self->req->method eq 'GET' ){
        return $self->render( record => $record );
    }
    else{
        $record->delete;
        my $res = $self->req->new_response();
        $res->redirect( $self->self_url );
        return $res;
    }
}

sub edit_action {
    my ( $self, $record ) = @_;
    if( !$record || !blessed($record) ) {
        my $res = $self->req->new_response(404);
        $res->content_type('text/plain');
        $res->body( 'No record found' );
        return $res;
    }

    my $req = $self->req;
    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;
Something went wrong with that request. Please try again.