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
59 sub list
:Chained
('base') :PathParth
('list') :Args
(0) {
60 # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
61 # 'Context' that's used to 'glue together' the various components
62 # that make up the application
65 # Retrieve all of the book records as book model objects and store in the
66 # stash where they can be accessed by the TT template
70 # $c->stash(lists => [$c->model('DB::List')->all]); # works
71 # my $cd_rs = $schema->resultset('CD')->search({
72 # title => 'something',
76 $c->stash(lists
=> [$c->model('DB::List')->search({
77 user_id
=> $c->user->id,
81 # Set the TT template to use. You will almost always want to do this
82 # in your action methods (action methods respond to user input in
84 $c->stash(template
=> 'lists/list.tt2');
89 Fetch the specified list object based on the list ID and store
94 sub object
:Chained
('base') :PathPart
('id') :CaptureArgs
(1) {
95 # $id = primary key of book to delete
96 my ($self, $c, $id) = @_;
98 # Find the book object and store it in the stash
99 $c->stash(object
=> $c->stash->{resultset
}->find($id));
101 # Make sure the lookup was successful. You would probably
102 # want to do something like this in a real app:
103 # $c->detach('/error_404') if !$c->stash->{object};
104 $c->log->debug("Lists::object - List id is $id");
105 die "List $id not found!" if !$c->stash->{object
};
107 # Print a message to the debug log
108 $c->log->debug("*** INSIDE OBJECT METHOD for obj id=$id ***");
117 sub delete :Chained
('object') :PathPart
('delete') :Args
(0) {
120 # Saved the PK id for status_msg below
121 my $id = $c->stash->{object
}->id;
123 # Use the list object saved by 'object' and delete it [along
124 # with related 'list_author' entries]
125 $c->stash->{object
}->delete;
127 # Set a status message to be displayed at the top of the view
128 # $c->stash->{status_msg} = "List deleted.";
130 # Forward to the list action/method in this controller
131 # $c->forward('list'); # dangerous url
132 # Redirect the user back to the list page. Note the use
133 # of $self->action_for as earlier in this section (BasicCRUD)
134 # $c->response->redirect($c->uri_for($self->action_for('list')));
136 # Redirect the user back to the list page
137 $c->response->redirect($c->uri_for($self->action_for('list'),
138 {mid
=> $c->set_status_msg("Deleted list $id")}));
143 This one is very similar to list, but it lists list items, not lists.
147 sub view
:Chained
('object') :PathPart
('view') :Args
(0) {
150 my $id = $c->stash->{object
}->id;
152 # get list item info from database
153 $c->stash(list_items
=> [$c->model('DB::ListItem')->search(
154 { 'list_id' => $id },
156 join => {'products' => 'products' },
157 '+select' => [ 'products.id', 'products.name', 'products.weight', 'products.cost' ],
158 '+as' => [ 'pid', 'name', 'weight', 'cost' ],
160 { 'pid' => 'product_id' },
165 $c->stash(template
=> 'lists/view.tt2');
171 Use HTML::FormHandler to create a new list
175 sub create
: Chained
('base') : PathPart
('create') Args
(0) {
176 my ($self, $c ) = @_;
178 my $list = $c->model('DB::List')->new_result({ user_id
=> $c->user->id });
179 my $form = Slists
::Form
::List
->new;
181 $c->stash( template
=> 'lists/form.tt2', form
=> $form );
184 params
=> $c->req->params,
186 return unless $form->validated;
187 # Set a status message for the user & return to books list
188 return $c->response->redirect($c->uri_for($self->action_for('list'),
189 {mid
=> $c->set_status_msg("List created")}));
194 Process the FormHandler list form
208 This library is free software. You can redistribute it and/or modify
209 it under the same terms as Perl itself.
213 __PACKAGE__
->meta->make_immutable;