Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Created May 20, 2022 16:31
Show Gist options
  • Save jjn1056/cb890ed4279300b4d1b3d173768254b5 to your computer and use it in GitHub Desktop.
Save jjn1056/cb890ed4279300b4d1b3d173768254b5 to your computer and use it in GitHub Desktop.
package Example::Controller::Todos;
use Moose;
use MooseX::MethodAttributes;
use Example::Syntax;
extends 'Catalyst::Controller';
sub root :Chained(/auth) PathPart('todos') CaptureArgs(0) ($self, $c) { }
sub collection :Chained(root) CaptureArgs(0) ($self, $c) {
my $list = $c->user->todos;
my $view = $c->view('Components::Todos',
todo_list => $list,
todo => my $todo = $list->new_result(+{status=>'active'}),
);
}
sub list :GET Chained(collection) PathPart('') Args(0) ($self, $c) { return $view->http_ok }
sub create :POST Chained(collection) PathPart('') Args(0) ($self, $c) {
my %params = $c->structured_body(
['todo'], 'title',
)->to_hash;
$todo->set_columns_recursively(\%params)->insert;
return $todo->valid ?
$c->redirect_to_action('list') :
$view->http_bad_request;
}
sub item :Chained(root) CaptureArgs(1) ($self, $c, $id) {
my $todo = $c->user->todos->find($id) || return $c->detach_error(404);
my $view = $c->view('Components::Todo', todo => $todo);
}
sub show :GET Chained(item) Args(0) ($self, $c) { $view->http_ok }
sub update :PATCH Chained(item) Args(0) ($self, $c) {
my %params = $c->structured_body(
['todo'], 'title', 'status'
)->to_hash;
$todo->set_columns_recursively(\%params)->update;
return $todo->valid ?
$c->redirect_to_action('show') :
$view->http_bad_request;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment