X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fchatty;a=blobdiff_plain;f=lib%2FChatty%2FController%2FChat.pm;fp=lib%2FChatty%2FController%2FChat.pm;h=9be0e796e28449eefb4e31fbaad02c3e7cef997d;hp=0000000000000000000000000000000000000000;hb=6177b7c3d795a2505f95269ab2dd1d4c6f05a525;hpb=65447d848567b9383f01ab73d8d7eb42b9d8844d diff --git a/lib/Chatty/Controller/Chat.pm b/lib/Chatty/Controller/Chat.pm new file mode 100644 index 0000000..9be0e79 --- /dev/null +++ b/lib/Chatty/Controller/Chat.pm @@ -0,0 +1,111 @@ +package Chatty::Controller::Chat; +use Moose; +use namespace::autoclean; + +BEGIN { extends 'Catalyst::Controller' } + +#__PACKAGE__->config(namespace => 'room'); + +use Chatty::Form::RoomCreate; + +has 'roomcreate_form' => ( + isa => 'Chatty::Form::RoomCreate', + is => 'rw', + lazy => 1, + default => sub { Chatty::Form::RoomCreate->new } +); + +=head1 NAME + +Chatty::Controller::Chat - Catalyst Controller + +=head1 DESCRIPTION + +Catalyst Controller. + +=head1 METHODS + +=head2 room + +Base action for chat rooms. Sets up the model. + +=cut + +sub room :Chained(/) :CaptureArgs(0) { + my ($self, $c) = @_; + $c->detach('/access_denied') if !$c->user_exists; +} + +=head2 list + +List the current list of chat rooms. + +=cut + +sub list :Chained(room) :Args(0) { + my ($self, $c) = @_; + + $c->stash(rooms => [$c->model('DB::Room')->all]); + + my $form = Chatty::Form::RoomCreate->new(action => + $c->uri_for_action('/chat/create')); + $c->stash(form => $form); +} + +=head2 create + +Create a new chat room. + +=cut + +sub create :Chained(room) :Args(0) { + my ($self, $c) = @_; + + $c->stash(form => $self->roomcreate_form); + + my $new_room = $c->model('DB::Room')->new_result({}); + $self->roomcreate_form->process( + item => $new_room, + params => $c->req->params + ); + + if (!$self->roomcreate_form->is_valid) { + if ($c->req->method eq 'POST') { + $c->stash->{error} = "The form has a validation error. Try again..."; + } + return; + } + + $c->flash->{message} = "Your new room was created!"; + $c->res->redirect($c->uri_for_action('/chat/view', $new_room->id)); +} + +=head2 view + +View a chat room. + +=cut + +sub view :Chained(room) :PathPart('') :Args(1) { + my ($self, $c, $room) = @_; + + $c->stash(room => $c->model('DB::Room')->find($room)); + $c->detach('/missing') if !$c->stash->{room}; + + $c->stash(messages => [$c->model('DB::Message')->search(room => $room)]); +} + +=head1 AUTHOR + +Charles McGarvey + +=head1 LICENSE + +This library is free software. You can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +__PACKAGE__->meta->make_immutable; + +1;