Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added new versions of {archive,code,db,feed}.pm

  • Loading branch information...
commit 1bde2bd6ed0574a8826382bfbe8e5499e844cc20 1 parent ad09105
@gnusosa authored
View
2  lib/log.pm
@@ -6,7 +6,7 @@ use log::code;
use log::db;
use log::feed;
-our $VERSION = '0.1';
+our $VERSION = '0.2';
prefix undef;
# This is a mini-blog engine based on the notes and tips by
View
23 lib/log/archive.pm
@@ -1,28 +1,17 @@
package log::archive;
use Dancer ':syntax';
-use log::db;
-use Time::Piece;
-
-sub _archive_sql {
- my $db = _connect_db();
- my $sql = 'select datetime, url, title from entries order by id desc';
- my $sth = $db->prepare($sql) or die $db->errstr;
- $sth->execute or die $sth->errstr;
- my $entries = $sth->fetchall_arrayref( {} );
- map {
- my $date = Time::Piece->strptime( $_->{datetime}, "%Y-%m-%d %H:%M:%S" );
- $_->{datetime} = $date->strftime("%b %d %Y");
- } @$entries;
- return $entries;
-}
+use log::db;
prefix '/archive';
get '/' => sub {
- my $list = _archive_sql();
- template 'archive', { list => $list }, { layout => 'archive' };
+ my $table = "entries";
+ my $list = _list($table);
+ template 'archive', { list => $list },
+ { layout => 'archive' };
};
1;
+
View
77 lib/log/code.pm
@@ -7,12 +7,77 @@ prefix '/code';
get '/' => sub {
my $entry = "code";
- my $table = "main";
- my $field = _entry( $table, $entry );
- template 'entries' => {
- 'title' => $field->{title},
- 'text' => $field->{text},
- };
+ my $table = "main";
+ my $field = _entry($table,$entry);
+ my $list_table = "code";
+ my $list = _list($list_table);
+ template 'list' => {
+ 'text' => $field->{text},
+ 'title' => $field->{title},
+ 'root' => $entry,
+ 'list' => $list,
+ }, { layout => 'code' };
};
+get '/category' => sub {
+ my $entry = "category";
+ my $table = "code";
+ my $field = _entry($table,$entry);
+ my $list_table = "category";
+ my $list = _list($list_table);
+ my $root = "$table/$entry";
+ template 'list' => {
+ 'text' => $field->{text},
+ 'title' => $field->{title},
+ 'root' => $root,
+ 'list' => $list,
+ }, { layout => 'code' };
+};
+
+get '/category/:report' => sub {
+ my $entry = params->{report};
+ my $table = "category";
+ my $field = _entry($table,$entry);
+ template 'entries' => {
+ 'text' => $field->{text},
+ 'title' => $field->{title},
+ }, { layout => 'code' };
+};
+
+get '/category2' => sub {
+ my $entry = "category2";
+ my $table = "code";
+ my $field = _entry($table,$entry);
+ my $list_table = "category2";
+ my $list = _list($list_table);
+ my $root = "$table/$entry";
+ template 'list' => {
+ 'text' => $field->{text},
+ 'title' => $field->{title},
+ 'root' => $root,
+ 'list' => $list,
+ }, { layout => 'code' };
+};
+
+get '/category2/:entry' => sub {
+ my $entry = params->{entry};
+ my $table = "category2";
+ my $field = _entry($table,$entry);
+ template 'entries' => {
+ 'text' => $field->{text},
+ 'title' => $field->{title},
+ }, { layout => 'code' };
+};
+
+get '/:entr' => sub {
+ my $entry = params->{entr};
+ my $table = "code";
+ my $field = _entry($table,$entry);
+ template 'entries' => {
+ 'text' => $field->{text},
+ 'title' => $field->{title},
+ }, { layout => 'code' };
+};
+
+
1;
View
31 lib/log/db.pm
@@ -1,19 +1,34 @@
package log::db;
-use Dancer::Plugin;
+use strict;
+use Dancer::Plugin;
use Dancer::Plugin::Database;
+use Time::Piece;
register _connect_db => sub {
- my $db = database();
- return $db;
+ my $db = database();
+ return $db;
};
register _entry => sub {
- my ( $table, $url ) = @_;
- my $db = _connect_db();
- my $sth = $db->quick_select( $table, { url => $url } )
- or die $db->errstr;
- return $sth;
+ my ($table, $url) = @_;
+ my $db = _connect_db();
+ my $sth = $db->quick_select($table, { url => $url})
+ or die $db->errstr;
+ return $sth;
+};
+
+register _list => sub {
+ my $table = shift;
+ my $db = _connect_db();
+ my $sql = qq{select * from $table order by id desc};
+ my $sth = $db->prepare($sql) or die $db->errstr;
+ $sth->execute or die $sth->errstr;
+ my $entries = $sth->fetchall_arrayref({});
+ map { my $date = Time::Piece->strptime($_->{datetime},"%Y-%m-%d %H:%M:%S");
+ $_->{datetime} = $date->strftime("%b %d %Y");
+ } @$entries;
+ return $entries;
};
register_plugin;
View
60 lib/log/feed.pm
@@ -6,51 +6,45 @@ use Dancer::Plugin::Feed;
use DateTime::Format::Strptime;
sub _feed_sql {
- my $db = _connect_db();
- my $sql = 'select * from entries order by id desc limit 10';
- my $sth = $db->prepare($sql) or die $db->errstr;
- $sth->execute or die $sth->errstr;
- my $entries = $sth->fetchall_arrayref( {} );
- return $entries;
+ my $db = _connect_db();
+ my $sql = 'select * from entries order by id desc limit 10';
+ my $sth = $db->prepare($sql) or die $db->errstr;
+ $sth->execute or die $sth->errstr;
+ my $entries = $sth->fetchall_arrayref({});
+ return $entries;
}
-
sub _get_entries {
- my $item = _feed_sql();
- my $parser = DateTime::Format::Strptime->new(
- pattern => '%Y-%m-%d %H:%M:%S',
- locale => 'en_US',
- time_zone => 'US/Pacific',
- );
- [
- map {
- my $dt = $parser->parse_datetime( $_->{datetime} );
- {
- title => $_->{title},
- link => config->{domain} . $_->{url},
- content => $_->{text},
- author => config->{author},
- issued => $dt,
+ my $item = _feed_sql();
+ my $parser = DateTime::Format::Strptime->new(
+ pattern => '%Y-%m-%d %H:%M:%S',
+ locale => 'en_US',
+ time_zone => 'US/Pacific',);
+ [ map{
+ my $dt = $parser->parse_datetime($_->{datetime});
+ { title => $_->{title} , link => config->{domain}.$_->{url},
+ content => $_->{text}, author => config->{author},
+ issued => $dt,
}
- } @$item
- ];
+ } @$item
+ ];
}
prefix '/feed';
get '/' => sub {
my $entry = "feed";
- my $table = "main";
- my $field = _entry( $table, $entry );
- template 'entries' => {
- 'title' => $field->{title},
- 'text' => $field->{text},
- };
+ my $table = "main";
+ my $field = _entry($table,$entry);
+ template 'entries' => {
+ 'title' => $field->{title},
+ 'text' => $field->{text},
+ };
};
get '/:format' => sub {
- my $feed = create_feed(
- format => params->{format},
- entries => _get_entries()
+ my $feed = create_feed(
+ format => params->{format},
+ entries => _get_entries()
);
return $feed;
};
Please sign in to comment.
Something went wrong with that request. Please try again.