Remove debug output
[slists.git] / lib / Slists / Controller / Lists.pm
blob5fc02f53e336bff6dfe78ed4f5b99973898f907d
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 :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
62 my ($self, $c) = @_;
64 # retrieve all lists for this user
65 $c->stash(lists => [$c->model('DB::List')->search({
66 user_id => $c->user->id,
67 })->all
68 ]);
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
72 # your controllers).
73 $c->stash(template => 'lists/list.tt2');
76 =head2 object
78 Fetch the specified list object based on the list ID and store
79 it in the stash
81 =cut
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 ***");
100 =head2 delete
102 Delete a list
104 =cut
106 sub delete :Chained('object') :PathPart('delete') :Args(0) {
107 my ($self, $c) = @_;
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")}));
121 =head2 edit
123 This one is very similar to list, but it lists list items, not lists.
125 =cut
127 sub view :Chained('object') :PathPart('view') :Args(0) {
128 my ($self, $c) = @_;
129 # get list id
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' },
141 )->all
143 $c->stash(list_id => $id);
145 # use template
146 $c->stash(template => 'lists/view.tt2');
150 =head2 create
152 Rename an existing list with FormHandler
154 =cut
156 sub rename : Chained('object') PathPart('rename') Args(0) {
157 my ( $self, $c ) = @_;
159 # $c->set_status_msg("List renamed");
160 # did not work
161 $c->stash( my_status_msg => "List renamed" );
163 return $self->form($c, $c->stash->{object});
166 =head2 create
168 Use HTML::FormHandler to create a new list
170 =cut
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);
179 =head2 form
181 Process the FormHandler list form
182 This is a separate function, because it's used by both /create and /edit.
184 =cut
186 sub form{
187 my ( $self, $c, $list ) = @_;
188 my $form = Slists::Form::List->new;
189 # Set the template
190 $c->stash( template => 'lists/form.tt2', form => $form );
191 $form->process(
192 item => $list,
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})}));
201 =head1 AUTHOR
203 user,,,,
205 =head1 LICENSE
207 This library is free software. You can redistribute it and/or modify
208 it under the same terms as Perl itself.
210 =cut
212 __PACKAGE__->meta->make_immutable;