Last active
May 20, 2022 17:12
-
-
Save jjn1056/15c0f85fb833fb83970a5aaf981d78e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
$c->send(my $list = $c->user->todos); | |
} | |
sub collection :Chained(root) CaptureArgs(0) ($self, $c, $list) { | |
my $view = $c->view('Components::Todos', | |
todo_list => $list, | |
new_todo => my $todo = $list->new_result(+{status=>'active'}), | |
); | |
return $c->send($view, $todo); | |
} | |
sub list :GET Chained(collection) PathPart('') Args(0) ($self, $c, $view, $todo) { return $view->http_ok } | |
sub create :POST Chained(collection) PathPart('') Args(0) ($self, $c, $view, $todo) { | |
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, $list) { | |
my $todo = $list->find($id) || return $c->detach_error(404); | |
my $view = $c->view('Components::Todo', todo => $todo); | |
return $c->send($view, $todo); | |
} | |
sub show :GET Chained(item) Args(0) ($self, $c, $view, $todo) { $view->http_ok } | |
sub update :PATCH Chained(item) Args(0) ($self, $c, $view, $todo) { | |
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