From: Charles McGarvey Date: Thu, 20 Oct 2011 03:07:20 +0000 (-0600) Subject: create, list, and room viewing implemented X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=6177b7c3d795a2505f95269ab2dd1d4c6f05a525;p=chaz%2Fchatty create, list, and room viewing implemented --- 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; diff --git a/lib/Chatty/Form/RoomCreate.pm b/lib/Chatty/Form/RoomCreate.pm new file mode 100644 index 0000000..7b6a6bc --- /dev/null +++ b/lib/Chatty/Form/RoomCreate.pm @@ -0,0 +1,16 @@ +package Chatty::Form::RoomCreate; + +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler::Model::DBIC'; +use namespace::autoclean; + +has '+item_class' => (default => 'Room'); +has '+unique_messages' => (default => sub { + {name => 'Room name is already taken'}; +}); + +has_field 'name' => (input_class => 'validate[required]', label => 'Room name', required => 1, unique => 1); +has_field 'submit' => (type => 'Submit', value => 'Create'); + +__PACKAGE__->meta->make_immutable; +1; diff --git a/root/tt/chat/create.tt b/root/tt/chat/create.tt new file mode 100644 index 0000000..ea23f10 --- /dev/null +++ b/root/tt/chat/create.tt @@ -0,0 +1,6 @@ +[% META title = 'Create a Chat Room' -%] +[% BLOCK js -%] +$('form').validationEngine(); +[% END -%] +

Create a Chat Room

+[% form.render %] diff --git a/root/tt/chat/list.tt b/root/tt/chat/list.tt new file mode 100644 index 0000000..1e30b65 --- /dev/null +++ b/root/tt/chat/list.tt @@ -0,0 +1,19 @@ +[% META title = 'Join a Chat Room' -%] +[% BLOCK js -%] +$('form').validationEngine(); +[% END -%] +

Join a Chat Room

+ +[% FOREACH room IN rooms -%] + + + + +[% END -%] +
+ [% room.name %] + + [% room.created %] +
+

Create Your Own Room

+[% form.render %] diff --git a/root/tt/chat/view.tt b/root/tt/chat/view.tt new file mode 100644 index 0000000..bfb4590 --- /dev/null +++ b/root/tt/chat/view.tt @@ -0,0 +1,13 @@ +[% META title = 'Live' -%] +[% BLOCK js -%] +$('form').validationEngine(); +[% END -%] +

Room: [% room.name %]

+[% IF messages.count == 0 -%] +[% FOREACH msg IN messages -%] +

[% msg.author.username %] ([% msg.posted %]): [% msg.content %]

+[% END -%] +[% ELSE -%] +

No messages, yet.

+[% END -%] +[% form.render -%] diff --git a/t/controller_Chat.t b/t/controller_Chat.t new file mode 100644 index 0000000..755d754 --- /dev/null +++ b/t/controller_Chat.t @@ -0,0 +1,10 @@ +use strict; +use warnings; +use Test::More; + + +use Catalyst::Test 'Chatty'; +use Chatty::Controller::Chat; + +ok( request('/chat')->is_success, 'Request should succeed' ); +done_testing();