remove redundant comments
[slists.git] / lib / Slists / Controller / Lists.pm
blob462763ecd30a3690f63eac737b0e8981485799c5
1 package Slists::Controller::Lists;
2 use Moose;
3 use namespace::autoclean;
4 use Data::Dumper; #XXX
5 use Slists::Form::List;
7 BEGIN { extends 'Catalyst::Controller'; }
9 =head1 NAME
11 Slists::Controller::Lists - Catalyst Controller
13 =head1 DESCRIPTION
15 Catalyst Controller.
17 =head1 METHODS
19 =cut
21 =head2 index
23 =cut
25 sub index :Path :Args(0) {
26 my ( $self, $c ) = @_;
28 $c->response->body('Matched Slists::Controller::Lists in Lists.');
33 =head2 base
35 Can place common logic to start chained dispatch here
37 =cut
39 sub base :Chained('/') :PathPart('lists') :CaptureArgs(0) {
40 my ($self, $c) = @_;
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
49 $c->load_status_msgs;
52 =head2 list
54 Fetch all lists objects [for this user] and pass to lists/list.tt2 in stash to be displayed
56 =cut
58 #sub list :Local {
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
63 my ($self, $c) = @_;
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
68 # $c->{user}->{id};
70 # $c->stash(lists => [$c->model('DB::List')->all]); # works
71 # my $cd_rs = $schema->resultset('CD')->search({
72 # title => 'something',
73 # year => 2009,
74 # });
76 $c->stash(lists => [$c->model('DB::List')->search({
77 user_id => $c->user->id,
78 })->all
79 ]);
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
83 # your controllers).
84 $c->stash(template => 'lists/list.tt2');
87 =head2 object
89 Fetch the specified list object based on the list ID and store
90 it in the stash
92 =cut
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 ***");
111 =head2 delete
113 Delete a list
115 =cut
117 sub delete :Chained('object') :PathPart('delete') :Args(0) {
118 my ($self, $c) = @_;
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")}));
141 =head2 edit
143 This one is very similar to list, but it lists list items, not lists.
145 =cut
147 sub view :Chained('object') :PathPart('view') :Args(0) {
148 my ($self, $c) = @_;
149 # get list id
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' },
161 )->all
164 # use template
165 $c->stash(template => 'lists/view.tt2');
169 =head2 create
171 Use HTML::FormHandler to create a new list
173 =cut
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;
180 # Set the template
181 $c->stash( template => 'lists/form.tt2', form => $form );
182 $form->process(
183 item => $list,
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")}));
192 =head2 form
194 Process the FormHandler list form
196 =cut
198 sub form {
202 =head1 AUTHOR
204 user,,,,
206 =head1 LICENSE
208 This library is free software. You can redistribute it and/or modify
209 it under the same terms as Perl itself.
211 =cut
213 __PACKAGE__->meta->make_immutable;