--- /dev/null
+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;
--- /dev/null
+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;
--- /dev/null
+[% 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 %]
--- /dev/null
+[% 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 -%]