]> Dogcows Code - chaz/chatty/commitdiff
create, list, and room viewing implemented
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 20 Oct 2011 03:07:20 +0000 (21:07 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 20 Oct 2011 03:07:20 +0000 (21:07 -0600)
lib/Chatty/Controller/Chat.pm [new file with mode: 0644]
lib/Chatty/Form/RoomCreate.pm [new file with mode: 0644]
root/tt/chat/create.tt [new file with mode: 0644]
root/tt/chat/list.tt [new file with mode: 0644]
root/tt/chat/view.tt [new file with mode: 0644]
t/controller_Chat.t [new file with mode: 0644]

diff --git a/lib/Chatty/Controller/Chat.pm b/lib/Chatty/Controller/Chat.pm
new file mode 100644 (file)
index 0000000..9be0e79
--- /dev/null
@@ -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 (file)
index 0000000..7b6a6bc
--- /dev/null
@@ -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 (file)
index 0000000..ea23f10
--- /dev/null
@@ -0,0 +1,6 @@
+[% META title = 'Create a Chat Room' -%]
+[% BLOCK js -%]
+$('form').validationEngine();
+[% END -%]
+<h1>Create a Chat Room</h1>
+[% form.render %]
diff --git a/root/tt/chat/list.tt b/root/tt/chat/list.tt
new file mode 100644 (file)
index 0000000..1e30b65
--- /dev/null
@@ -0,0 +1,19 @@
+[% META title = 'Join a Chat Room' -%]
+[% BLOCK js -%]
+$('form').validationEngine();
+[% END -%]
+<h1>Join a Chat Room</h1>
+<table>
+[% FOREACH room IN rooms -%]
+<tr>
+       <td>
+               <a href="[% c.uri_for_action('/chat/view', room.id) %]">[% room.name %]</a>
+       </td>
+       <td>
+               [% room.created %]
+       </td>
+</tr>
+[% END -%]
+</table>
+<h2>Create Your Own Room</h2>
+[% form.render %]
diff --git a/root/tt/chat/view.tt b/root/tt/chat/view.tt
new file mode 100644 (file)
index 0000000..bfb4590
--- /dev/null
@@ -0,0 +1,13 @@
+[% META title = 'Live' -%]
+[% BLOCK js -%]
+$('form').validationEngine();
+[% END -%]
+<h1>Room: [% room.name %]</h1>
+[% IF messages.count == 0 -%]
+[% FOREACH msg IN messages -%]
+<p>[% msg.author.username %] ([% msg.posted %]): [% msg.content %]</p>
+[% END -%]
+[% ELSE -%]
+<p>No messages, yet.</p>
+[% END -%]
+[% form.render -%]
diff --git a/t/controller_Chat.t b/t/controller_Chat.t
new file mode 100644 (file)
index 0000000..755d754
--- /dev/null
@@ -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();
This page took 0.028126 seconds and 4 git commands to generate.