Skip to content
Browse files

first commit

  • Loading branch information...
1 parent df6bc48 commit 283a5cf490adc29bcbb61c4275c47bef7a4a1abb @typester committed Jun 23, 2009
View
1 .gitignore
@@ -0,0 +1 @@
+minibbs.db
View
12 Makefile.PL
@@ -0,0 +1,12 @@
+use inc::Module::Install;
+name 'MiniBBS';
+all_from 'lib/MiniBBS.pm';
+
+requires 'Ark';
+
+tests 't/*.t';
+
+build_requires 'Test::More';
+use_test_base;
+auto_include;
+WriteAll;
View
16 lib/MiniBBS.pm
@@ -0,0 +1,16 @@
+package MiniBBS;
+use Ark;
+
+our $VERSION = '0.01';
+
+use_plugins qw{
+ Session
+ Session::State::Cookie
+ Session::Store::Memory
+
+ Authentication
+ Authentication::Credential::OpenID
+ Authentication::Store::Null
+};
+
+1;
View
24 lib/MiniBBS/Controller/Login.pm
@@ -0,0 +1,24 @@
+package MiniBBS::Controller::Login;
+use Ark 'Controller';
+
+sub login :Path :Args(0) {
+ my ($self, $c) = @_;
+
+ if (my $user = $c->authenticate) {
+ # login 成功
+ $c->detach('redirect_home');
+ }
+}
+
+sub logout :Global {
+ my ($self, $c) = @_;
+ $c->logout;
+ $c->detach('redirect_home');
+}
+
+sub redirect_home :Private {
+ my ($self, $c) = @_;
+ $c->redirect( $c->uri_for('/') );
+}
+
+1;
View
47 lib/MiniBBS/Controller/Root.pm
@@ -0,0 +1,47 @@
+package MiniBBS::Controller::Root;
+use Ark 'Controller';
+
+has '+namespace' => default => '';
+
+# default 404 handler
+sub default :Path :Args {
+ my ($self, $c) = @_;
+
+ $c->res->status(404);
+ $c->res->body('404 Not Found');
+}
+
+sub index :Path :Args(0) {
+ my ($self, $c) = @_;
+
+ if ($c->req->method eq 'POST') {
+ $c->detach('post');
+ }
+
+ $c->stash->{messages} = $c->model('BBS')->messages;
+}
+
+sub post :Private {
+ my ($self, $c) = @_;
+
+ if ($c->user and my $msg = $c->req->param('message')) {
+ $c->model('BBS')->add_message({
+ user => $c->user->obj->{display},
+ body => $msg,
+ });
+ }
+
+ $c->redirect( $c->uri_for('/') )
+}
+
+sub end :Private {
+ my ($self, $c) = @_;
+
+ $c->res->header('Cache-Control' => 'private');
+
+ unless ($c->res->body or $c->res->status =~ /^3\d\d/) {
+ $c->forward( $c->view('MT') );
+ }
+}
+
+1;
View
10 lib/MiniBBS/Model/BBS.pm
@@ -0,0 +1,10 @@
+package MiniBBS::Model::BBS;
+use Ark 'Model::Adaptor';
+
+__PACKAGE__->config(
+ class => 'MiniBBS::Service::BBS',
+ args => { connect_info => ['dbi:SQLite:' . MiniBBS->path_to('minibbs.db') ] },
+ deref => 1,
+);
+
+1;
View
36 lib/MiniBBS/Service/BBS.pm
@@ -0,0 +1,36 @@
+package MiniBBS::Service::BBS;
+use Any::Moose;
+
+use MiniBBS::Service::BBS::Schema;
+
+has connect_info => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ required => 1,
+);
+
+has schema => (
+ is => 'rw',
+ isa => 'MiniBBS::Service::BBS::Schema',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ MiniBBS::Service::BBS::Schema->connect( @{ $self->connect_info } );
+ },
+);
+
+no Any::Moose;
+
+sub add_message {
+ my ($self, $message) = @_;
+ my $new_msg = $self->schema->resultset('Message')->create($message);
+}
+
+sub messages {
+ my ($self,) = @_;
+ my $messages = $self->schema->resultset('Message')
+ ->search({}, { order_by => 'created_date desc' });
+}
+
+1;
+
View
9 lib/MiniBBS/Service/BBS/Schema.pm
@@ -0,0 +1,9 @@
+package MiniBBS::Service::BBS::Schema;
+use strict;
+use warnings;
+use base 'DBIx::Class::Schema::Loader';
+
+__PACKAGE__->loader_options;
+
+1;
+
View
22 lib/MiniBBS/Service/BBS/Schema/Message.pm
@@ -0,0 +1,22 @@
+package MiniBBS::Service::BBS::Schema::Message;
+use strict;
+use warnings;
+no warnings 'redefine';
+
+use DateTime;
+
+__PACKAGE__->inflate_column(
+ created_date => {
+ inflate => sub { DateTime->from_epoch( epoch => shift, time_zone => 'local' ) },
+ deflate => sub { shift->epoch },
+ },
+);
+
+sub insert {
+ my $self = shift;
+ $self->created_date( DateTime->now ) unless $self->created_date;
+ $self->next::method(@_);
+}
+
+1;
+
View
4 lib/MiniBBS/View/MT.pm
@@ -0,0 +1,4 @@
+package MiniBBS::View::MT;
+use Ark 'View::MT';
+
+1;
View
6 minibbs.sqlite.sql
@@ -0,0 +1,6 @@
+CREATE TABLE message (
+ id INTEGER NOT NULL PRIMARY KEY,
+ user TEXT NOT NULL,
+ body TEXT NOT NULL,
+ created_date INTEGER NOT NULL
+);
View
31 root/index.mt
@@ -0,0 +1,31 @@
+? if ($c->user) {
+
+<form method="post">
+ <p>
+ <label for="message">メッセージ:</label>
+ <textarea id="message" name="message"></textarea>
+ </p>
+ <p><input type="submit" value="投稿"/></p>
+</form>
+
+? } else {
+
+<p>メッセージを投稿するには<a href="<?= $c->uri_for('/login') ?>">ログイン</a>が必要です。</p>
+
+? }
+
+
+? while (my $msg = $s->{messages}->next) {
+
+<hr />
+
+<dl>
+ <dt>ID:</dt>
+ <dd><?= $msg->user ?></dd>
+ <dt>Date:</dt>
+ <dd><?= $msg->created_date ?></dd>
+ <dt>Body:</dt>
+ <dd><?= $msg->body ?></dd>
+</dl>
+
+? }
View
9 root/login/login.mt
@@ -0,0 +1,9 @@
+<p>OpenID でログイン</p>
+
+<form method="get">
+<p>
+ <label for="openid_identifier">OpenID URI:</label>
+ <input type="text" id="openid_identifier" name="openid_identifier" />
+</p>
+<p><input type="submit" value="login"/></p>
+</form>
View
4 t/00_compile.t
@@ -0,0 +1,4 @@
+use strict;
+use Test::More tests => 1;
+
+BEGIN { use_ok 'MiniBBS' }
View
49 t/service_bbs.t
@@ -0,0 +1,49 @@
+use Test::Base;
+
+plan 'no_plan';
+
+use_ok('MiniBBS::Service::BBS');
+
+# create test db
+my $db = "test_database";
+END { unlink $db }
+
+my $connect_info = "dbi:SQLite:$db";
+
+{
+ use DBI;
+ my $dbh = DBI->connect($connect_info);
+ $dbh->do(<<'...');
+CREATE TABLE message (
+ id INTEGER NOT NULL PRIMARY KEY,
+ user TEXT NOT NULL,
+ body TEXT NOT NULL,
+ created_date INTEGER NOT NULL
+);
+...
+
+}
+
+my $bbs = MiniBBS::Service::BBS->new(
+ connect_info => [$connect_info],
+);
+isa_ok($bbs, 'MiniBBS::Service::BBS');
+
+{
+ # no message
+ my $messages = $bbs->messages;
+ is( $messages->count, 0, 'no message ok');
+}
+
+{
+ # insert message
+ my $user = 'user ' . time;
+ my $body = 'test ' . time;
+ my $new_message = $bbs->add_message({ user => $user, body => $body });
+ ok( $new_message, 'create message ok' );
+
+ my $messages = $bbs->messages;
+ is( $messages->count, 1, 'count 1 ok' );
+ is( $messages->first->user, $user, 'user ok');
+ is( $messages->first->body, $body, 'body ok');
+}

0 comments on commit 283a5cf

Please sign in to comment.
Something went wrong with that request. Please try again.