Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,065 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
MANIFEST | ||
cpanfile | ||
Makefile.PL | ||
MANIFEST.SKIP | ||
config.yml | ||
public/dispatch.fcgi | ||
public/500.html | ||
public/dispatch.cgi | ||
public/favicon.ico | ||
public/404.html | ||
public/images/perldancer-bg.jpg | ||
public/images/perldancer.jpg | ||
public/css/style.css | ||
public/css/error.css | ||
public/javascripts/jquery.js | ||
t/001_base.t | ||
t/002_index_route.t | ||
lib/BookWeb.pm | ||
views/index.tt | ||
views/layouts/main.tt | ||
environments/production.yml | ||
environments/development.yml | ||
bin/app.psgi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
^\.git\/ | ||
maint | ||
^tags$ | ||
.last_cover_stats | ||
Makefile$ | ||
^blib | ||
^pm_to_blib | ||
^.*.bak | ||
^.*.old | ||
^t.*sessions | ||
^cover_db | ||
^.*\.log | ||
^.*\.swp$ | ||
MYMETA.* | ||
^.gitignore | ||
^.svn\/ | ||
^BookWeb- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use strict; | ||
use warnings; | ||
use ExtUtils::MakeMaker; | ||
|
||
# Normalize version strings like 6.30_02 to 6.3002, | ||
# so that we can do numerical comparisons on it. | ||
my $eumm_version = $ExtUtils::MakeMaker::VERSION; | ||
$eumm_version =~ s/_//; | ||
|
||
WriteMakefile( | ||
NAME => 'BookWeb', | ||
AUTHOR => q{YOUR NAME <youremail@example.com>}, | ||
VERSION_FROM => 'lib/BookWeb.pm', | ||
ABSTRACT => 'YOUR APPLICATION ABSTRACT', | ||
($eumm_version >= 6.3001 | ||
? ('LICENSE'=> 'perl') | ||
: ()), | ||
PL_FILES => {}, | ||
PREREQ_PM => { | ||
'Test::More' => 0, | ||
'YAML' => 0, | ||
'Dancer2' => 0.159003, | ||
}, | ||
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, | ||
clean => { FILES => 'BookWeb-*' }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use FindBin; | ||
use lib "$FindBin::Bin/../lib"; | ||
|
||
use BookWeb; | ||
BookWeb->to_app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This is the main configuration file of your Dancer2 app | ||
# env-related settings should go to environments/$env.yml | ||
# all the settings in this file will be loaded at Dancer's startup. | ||
|
||
# Your application's name | ||
appname: "BookWeb" | ||
|
||
# The default layout to use for your application (located in | ||
# views/layouts/main.tt) | ||
layout: "main" | ||
|
||
# when the charset is set to UTF-8 Dancer2 will handle for you | ||
# all the magic of encoding and decoding. You should not care | ||
# about unicode within your app when this setting is set (recommended). | ||
charset: "UTF-8" | ||
|
||
session: Cookie | ||
|
||
# template engine | ||
# simple: default and very basic template engine | ||
# template_toolkit: TT | ||
|
||
# template: "simple" | ||
|
||
template: "template_toolkit" | ||
engines: | ||
template: | ||
template_toolkit: | ||
start_tag: '<%' | ||
end_tag: '%>' | ||
session: | ||
Cookie: | ||
secret_key: somerandomnonsense | ||
default_duration: 604800 | ||
|
||
plugins: | ||
DBIC: | ||
book: | ||
schema_class: BookWeb::Schema | ||
dsn: dbi:mysql:database=books | ||
user: books | ||
pass: README | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
requires "Dancer2" => "0.159003"; | ||
|
||
recommends "YAML" => "0"; | ||
recommends "URL::Encode::XS" => "0"; | ||
recommends "CGI::Deurl::XS" => "0"; | ||
recommends "HTTP::Parser::XS" => "0"; | ||
|
||
on "test" => sub { | ||
requires "Test::More" => "0"; | ||
requires "HTTP::Request::Common" => "0"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# configuration file for development environment | ||
|
||
# the logger engine to use | ||
# console: log messages to STDOUT (your console where you started the | ||
# application server) | ||
# file: log message to a file in log/ | ||
logger: "console" | ||
|
||
# the log level for this environment | ||
# core is the lowest, it shows Dancer2's core log messages as well as yours | ||
# (debug, info, warning and error) | ||
log: "core" | ||
|
||
# should Dancer2 consider warnings as critical errors? | ||
warnings: 1 | ||
|
||
# should Dancer2 show a stacktrace when an error is caught? | ||
# if set to yes, public/500.html will be ignored and either | ||
# views/500.tt, 'error_template' template, or a default error template will be used. | ||
show_errors: 1 | ||
|
||
# print the banner | ||
startup_info: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# configuration file for production environment | ||
|
||
# only log warning and error messsages | ||
log: "warning" | ||
|
||
# log message to a file in logs/ | ||
logger: "file" | ||
|
||
# don't consider warnings critical | ||
warnings: 0 | ||
|
||
# hide errors | ||
show_errors: 0 | ||
|
||
# disable server tokens in production environments | ||
no_server_tokens: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package BookWeb; | ||
use Dancer2; | ||
#use Dancer2::Session; | ||
use Dancer2::Plugin::DBIC; | ||
use Net::Amazon; | ||
use DateTime; | ||
|
||
our $VERSION = '0.1'; | ||
|
||
sub get_amazon { | ||
return Net::Amazon->new( | ||
token => $ENV{AMAZON_KEY}, | ||
secret_key => $ENV{AMAZON_SECRET}, | ||
associate_tag => $ENV{AMAZON_ASSTAG}, | ||
locale => 'uk', | ||
) or die "Cannot connect to Amazon\n"; | ||
} | ||
|
||
my %public_path = map { $_ => 1 } ('/', '/login', '/search'); | ||
|
||
hook before => sub { | ||
if (! session('logged_in') and | ||
! $public_path{request->path_info}) { | ||
var requested_path => request->path_info; | ||
forward '/login'; | ||
} | ||
}; | ||
|
||
get '/' => sub { | ||
my $books_rs = schema->resultset('Book'); | ||
|
||
my @reading = $books_rs->search({ | ||
started => { '!=', undef }, | ||
ended => undef, | ||
}); | ||
|
||
my @read = $books_rs->search({ | ||
ended => { '!=', undef }, | ||
}); | ||
|
||
my @to_read = $books_rs->search({ | ||
started => undef, | ||
}); | ||
|
||
template 'index', { | ||
reading => \@reading, | ||
read => \@read, | ||
to_read => \@to_read, | ||
}; | ||
}; | ||
|
||
get '/start/:isbn' => sub { | ||
|
||
my $books_rs = schema->resultset('Book'); | ||
my $book = $books_rs->find({ isbn => param('isbn')}); | ||
|
||
if ($book) { | ||
$book->update({started => DateTime->now}); | ||
} | ||
|
||
return redirect '/'; | ||
}; | ||
|
||
get '/end/:isbn' => sub { | ||
|
||
my $books_rs = schema->resultset('Book'); | ||
my $book = $books_rs->find({ isbn => param('isbn')}); | ||
|
||
if ($book) { | ||
$book->update({ended => DateTime->now}); | ||
} | ||
|
||
return redirect '/'; | ||
}; | ||
|
||
get '/add/:isbn' => sub { | ||
my $author_rs = schema->resultset('Author'); | ||
|
||
my $amz = get_amazon(); | ||
|
||
# Search for the book at Amazon | ||
my $resp = $amz->search(asin => param('isbn')); | ||
|
||
unless ($resp->is_success) { | ||
die 'Error: ', $resp->message; | ||
} | ||
|
||
my $book = $resp->properties; | ||
my $title = $book->ProductName; | ||
my $author_name = ($book->authors)[0]; | ||
my $imgurl = $book->ImageUrlMedium; | ||
|
||
# Find or create the author | ||
my $author = $author_rs->find_or_create({ | ||
name => $author_name, | ||
}); | ||
|
||
# Add the book to the author | ||
$author->add_to_books({ | ||
isbn => param('isbn'), | ||
title => $title, | ||
image_url => $imgurl, | ||
}); | ||
|
||
return redirect '/'; | ||
}; | ||
|
||
post '/search' => sub { | ||
my $amz = get_amazon(); | ||
|
||
my $resp = $amz->search( | ||
keyword => param('search'), | ||
mode => 'books', | ||
); | ||
|
||
my %data; | ||
$data{search} = param('search'); | ||
if ($resp->is_success) { | ||
$data{books} = [ $resp->properties ]; | ||
} else { | ||
$data{error} = $resp->message; | ||
} | ||
|
||
template 'results', \%data; | ||
}; | ||
|
||
get '/login' => sub { | ||
template 'login', { path => vars->{requested_path } }; | ||
}; | ||
|
||
post '/login' => sub { | ||
if (params->{user} eq 'reader' && params->{pass} eq 'letmein') { | ||
session 'logged_in' => 1; | ||
} | ||
|
||
redirect params->{path} || '/'; | ||
}; | ||
|
||
true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use utf8; | ||
package BookWeb::Schema; | ||
|
||
# Created by DBIx::Class::Schema::Loader | ||
# DO NOT MODIFY THE FIRST PART OF THIS FILE | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use base 'DBIx::Class::Schema'; | ||
|
||
__PACKAGE__->load_namespaces; | ||
|
||
|
||
# Created by DBIx::Class::Schema::Loader v0.07042 @ 2015-03-29 16:32:33 | ||
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:NG4hEMUGO2NB1sOwP35hUw | ||
|
||
|
||
# You can replace this text with custom code or comments, and it will be preserved on regeneration | ||
1; |
Oops, something went wrong.