Permalink
Browse files

Schema

  • Loading branch information...
kazeburo committed Apr 8, 2011
1 parent d9c41c9 commit 25e056b88a76c2470761553c0b4e12f18805f526
Showing with 70 additions and 39 deletions.
  1. +33 −38 lib/NoNoPaste.pm
  2. +36 −0 lib/NoNoPaste/Data.pm
  3. +1 −1 lib/Shirahata2.pm
View
@@ -4,36 +4,39 @@ use Shirahata2;
use Scope::Container::DBI 0.03;
use Path::Class;
use Digest::SHA;
+use NoNoPaste::Data;
our $VERSION = 0.01;
-sub dbh {
- my $self = shift;
- my $db_path = Path::Class::file( $self->root_dir, "data", "nonopaste.db" );
- local $Scope::Container::DBI::DBI_CLASS = 'DBIx::Sunny';
- my $dbh = Scope::Container::DBI->connect(
- "dbi:SQLite:dbname=$db_path", '', '',
- {
- Callbacks => {
- connected => sub {
- my $connect = shift;
- $connect->do(<<EOF);
+my $_on_connect = sub {
+ my $connect = shift;
+ $connect->do(<<EOF);
CREATE TABLE IF NOT EXISTS entries (
id VARCHAR(255) NOT NULL PRIMARY KEY,
nick VARCHAR(255) NOT NULL,
body TEXT,
ctime DATETIME NOT NULL
)
EOF
- $connect->do(<<EOF);
+ $connect->do(<<EOF);
CREATE INDEX IF NOT EXISTS index_ctime ON entries ( ctime )
EOF
- return;
- }
+ return;
+};
+
+sub data {
+ my $self = shift;
+ my $db_path = Path::Class::file( $self->root_dir, "data", "nonopaste.db" );
+ local $Scope::Container::DBI::DBI_CLASS = 'DBIx::Sunny';
+ my $dbh = Scope::Container::DBI->connect(
+ "dbi:SQLite:dbname=$db_path", '', '',
+ {
+ Callbacks => {
+ connected => $_on_connect,
},
}
);
- $dbh;
+ NoNoPaste::Data->new( dbh => $dbh );
}
sub add_entry {
@@ -43,37 +46,29 @@ sub add_entry {
$nick = 'anonymouse' if ! defined $nick;
my $id = substr Digest::SHA::sha1_hex($$ . $self . join("\0", @_) . rand(1000) ), 0, 16;
- my $row = $self->dbh->query(<<EOF, $id, $nick, $body);
-INSERT INTO entries ( id, nick, body, ctime ) values ( ?, ?, ?, DATETIME('now') )
-EOF
- return ( $row == 1 ) ? $id : 0;
+ my $row = $self->data->add_entry(
+ id => $id,
+ nick => $nick,
+ body => $body,
+ );
+ return ( $row ) ? $id : 0;
}
sub entry_list {
my $self = shift;
my $offset = shift || 0;
- my $sth = $self->dbh->prepare(<<EOF);
-SELECT id,nick,body,ctime FROM entries ORDER BY ctime DESC LIMIT ?,11
-EOF
- $sth->execute($offset);
-
- my @ret;
- while ( my $row = $sth->fetchrow_hashref ) {
- push @ret, $row;
- last if @ret == 10;
- }
-
- my $next = $sth->fetchrow_hashref;
- return \@ret, $next;
+ my $rows = $self->data->entry_list( offset => $offset );
+
+ my $next;
+ $next = pop @$rows if @$rows > 10;
+
+ return $rows, $next;
}
sub retrieve_entry {
my $self = shift;
my $id = shift;
- my $row = $self->dbh->select_row(<<EOF, $id);
-SELECT id,nick,body,ctime FROM entries WHERE id = ?
-EOF
- return $row;
+ $self->data->retrieve_entry( id => $id );
}
get '/' => sub {
@@ -161,7 +156,7 @@ $(function() {
: cascade 'base'
: around content -> {
-<h2 class="subheader">New Entry</h2>
+<h2 class="subheader">新規投稿</h2>
: block form | fillinform( $c.req ) -> {
<form method="post" action="/add" id="nopaste">
<textarea name="body" rows="20" cols="60"></textarea>
@@ -171,7 +166,7 @@ $(function() {
</form>
: } # block form
-<h2 class="subheader">List</h2>
+<h2 class="subheader">最新一覧</h2>
: for $entries -> $entry {
<div class="entry">
<pre class="prettyprint">
View
@@ -0,0 +1,36 @@
+package NoNoPaste::Data;
+
+use strict;
+use warnings;
+use utf8;
+use parent qw/DBIx::Sunny::Schema/;
+use Mouse::Util::TypeConstraints;
+
+subtype 'Uint'
+ => as 'Int'
+ => where { $_ >= 0 };
+
+no Mouse::Util::TypeConstraints;
+
+__PACKAGE__->query(
+ 'add_entry',
+ id => 'Str',
+ nick => { isa => 'Str', default => 'anonymouse' },
+ body => 'Str',
+ q{INSERT INTO entries ( id, nick, body, ctime ) values ( ?, ?, ?, DATETIME('now') )},
+);
+
+__PACKAGE__->select_all(
+ 'entry_list',
+ 'offset' => { isa => 'Uint', default => 0 },
+ q{SELECT id,nick,body,ctime FROM entries ORDER BY ctime DESC LIMIT ?,11}
+);
+
+__PACKAGE__->select_row(
+ 'retrieve_entry',
+ 'id' => 'Str',
+ q{SELECT id,nick,body,ctime FROM entries WHERE id = ?}
+);
+
+1;
+
View
@@ -12,7 +12,7 @@ use Cwd qw/realpath/;
use File::Basename qw/dirname/;
use Path::Class;
-use Text::Xslate;
+use Text::Xslate 1.1003;
use Data::Section::Simple;
use HTML::FillInForm::Lite qw//;

0 comments on commit 25e056b

Please sign in to comment.