Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Last active May 23, 2022 18:26
Show Gist options
  • Save jjn1056/4d6381cdea7c85c7920abe87c129bb3a to your computer and use it in GitHub Desktop.
Save jjn1056/4d6381cdea7c85c7920abe87c129bb3a to your computer and use it in GitHub Desktop.
package Example::Controller::Todos;
use Moose;
use MooseX::MethodAttributes;
use Example::Syntax;
use CatalystX::Controller::ACCEPT_CONTEXT::DependencyInjectionImports;
use HTTP::Status 'HTTP_OK', 'HTTP_BAD_REQUEST';
use Example::Types 'TodoResultset', 'TodoResult';
extends 'CatalystX::Controller::ACCEPT_CONTEXT';
has list => (is=>'ro', isa=>TodoResultset, required=>1, depends => 'User.todos' ); # Same as (..., lazy=>1, default=>sub { shift->c->user->todos } )
has todo => (is=>'rw', isa=>TodoResult);
sub root :Chained(/auth) PathPart('todos') CaptureArgs(0) ($self, $c) { }
sub collection :Chained(root) CaptureArgs(0) View(Todos) ($self, $c) {
$self->todo($self->list->new_result(+{status=>'active'}));
}
sub list :GET Chained(collection) PathPart('') Args(0) ($self, $c) { return $c->res->code(HTTP_OK) }
sub create :POST Chained(collection) PathPart('') Args(0) ($self, $c) {
my %params = $c->structured_body(
['todo'], 'title',
)->to_hash;
$self->todo->set_columns_recursively(\%params)->insert;
return $self->todo->valid ?
$c->redirect_to_action('list') :
$c->res->code(HTTP_BAD_REQUEST);
}
sub item :Chained(root) CaptureArgs(1) View(Todo) ($self, $c) {
$self->todo($self->list->find($id) || return $c->detach_error(404));
}
sub show :GET Chained(item) Args(0) ($self, $c) { return $c->res->code(HTTP_OK) }
sub update :PATCH Chained(item) Args(0) ($self, $c) {
my %params = $c->structured_body(
['todo'], 'title', 'status'
)->to_hash;
$self->todo->set_columns_recursively(\%params)->update;
return $self->todo->valid ?
$c->redirect_to_action('show') :
$c->res->code(HTTP_BAD_REQUEST);
}
# delegating the action render to a later action in the chain
# allows you to centralize modifying the view before actually
# sending it.
sub end :Action Does(ViewResponse) ($self, $c) { }
1;
package Example::View::Todos;
use Moose;
use Example::Syntax;
use CatalystX::View::PerRequestDependencyInjection;
use Example::Types 'TodoResultset', 'TodoResult';
extends 'CatalystX::View::PerRequestBase';
has 'list' => (
is => 'ro',
isa => TodoResultset,
required => 1,
depends_attr => 'Controller', # Same as (..., lazy => 1, default => sub {shift->ctx->controller->list} )
);
has 'todo' => (
is => 'ro',
isa => TodoResult,
required => 1,
depends_attr => 'Controller',
);
sub render ($self, $c) { ... }
1;
package Example::View::Todo;
use Moose;
use Example::Syntax;
use CatalystX::View::PerRequest::DependencyInjectionImports;
use Example::Types 'TodoResult';
extends 'CatalystX::View::PerRequestBase';
has 'todo' => (
is => 'ro',
isa => TodoResult,
required => 1,
depends_attr => 'Controller',
);
sub render ($self, $c) { ... }
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment