1 package Slists
::Controller
::Lists
;
3 use namespace
::autoclean
;
5 use Slists
::Form
::List
;
7 BEGIN { extends
'Catalyst::Controller'; }
11 Slists::Controller::Lists - Catalyst Controller
25 sub index :Path
:Args
(0) {
26 my ( $self, $c ) = @_;
28 $c->response->body('Matched Slists::Controller::Lists in Lists.');
35 Can place common logic to start chained dispatch here
39 sub base
:Chained
('/') :PathPart
('lists') :CaptureArgs
(0) {
42 # Store the ResultSet in stash so it's available for other methods
43 $c->stash(resultset
=> $c->model('DB::List'));
45 # Print a message to the debug log
46 $c->log->debug('*** INSIDE BASE METHOD ***');
48 # Load status messages
54 Fetch all lists objects [for this user] and pass to lists/list.tt2 in stash to be displayed
58 sub list
:Chained
('base') :PathParth
('list') :Args
(0) {
59 # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
60 # 'Context' that's used to 'glue together' the various components
61 # that make up the application
64 # retrieve all lists for this user
65 $c->stash(lists
=> [$c->model('DB::List')->search({
66 user_id
=> $c->user->id,
70 # Set the TT template to use. You will almost always want to do this
71 # in your action methods (action methods respond to user input in
73 $c->stash(template
=> 'lists/list.tt2');
78 Fetch the specified list object based on the list ID and store
83 sub object
:Chained
('base') :PathPart
('id') :CaptureArgs
(1) {
84 # $id = primary key of book to delete
85 my ($self, $c, $id) = @_;
87 # Find the book object and store it in the stash
88 $c->stash(object
=> $c->stash->{resultset
}->find($id));
90 # Make sure the lookup was successful. You would probably
91 # want to do something like this in a real app:
92 # $c->detach('/error_404') if !$c->stash->{object};
93 $c->log->debug("Lists::object - List id is $id");
94 die "List $id not found!" if !$c->stash->{object
};
96 # Print a message to the debug log
97 $c->log->debug("*** INSIDE OBJECT METHOD for obj id=$id ***");
106 sub delete :Chained
('object') :PathPart
('delete') :Args
(0) {
109 # Saved the PK id for status_msg below
110 my $id = $c->stash->{object
}->id;
112 # Use the list object saved by 'object' and delete it [along
113 # with related 'list_author' entries]
114 $c->stash->{object
}->delete;
116 # Redirect the user back to the list page
117 $c->response->redirect($c->uri_for($self->action_for('list'),
118 {mid
=> $c->set_status_msg("Deleted list $id")}));
123 This one is very similar to list, but it lists list items, not lists.
127 sub view
:Chained
('object') :PathPart
('view') :Args
(0) {
130 my $id = $c->stash->{object
}->id;
132 # get list item info from database
133 $c->stash(list_items
=> [$c->model('DB::ListItem')->search(
134 { 'list_id' => $id },
136 join => {'products' => 'products' },
137 '+select' => [ 'products.id', 'products.name', 'products.weight', 'products.cost' ],
138 '+as' => [ 'pid', 'name', 'weight', 'cost' ],
140 { 'pid' => 'product_id' },
143 $c->stash(list_id
=> $id);
146 $c->stash(template
=> 'lists/view.tt2');
152 Rename an existing list with FormHandler
156 sub rename : Chained
('object') PathPart
('rename') Args
(0) {
157 my ( $self, $c ) = @_;
159 # $c->set_status_msg("List renamed");
161 $c->stash( my_status_msg
=> "List renamed" );
163 return $self->form($c, $c->stash->{object
});
168 Use HTML::FormHandler to create a new list
172 sub create
: Chained
('base') : PathPart
('create') Args
(0) {
173 my ($self, $c ) = @_;
174 my $list = $c->model('DB::List')->new_result({ user_id
=> $c->user->id });
175 $c->stash( my_status_msg
=> "List created" );
176 return $self->form($c, $list);
181 Process the FormHandler list form
182 This is a separate function, because it's used by both /create and /edit.
187 my ( $self, $c, $list ) = @_;
188 my $form = Slists
::Form
::List
->new;
190 $c->stash( template
=> 'lists/form.tt2', form
=> $form );
193 params
=> $c->req->params,
195 return unless $form->validated;
196 # Set a status message for the user & return to books list
197 return $c->response->redirect($c->uri_for($self->action_for('list'),
198 {mid
=> $c->set_status_msg($c->stash->{my_status_msg
})}));
207 This library is free software. You can redistribute it and/or modify
208 it under the same terms as Perl itself.
212 __PACKAGE__
->meta->make_immutable;