Dodano mozliowsc logowania sie i rezerwowania ksiazek

master
Cranix 2013-04-01 11:48:07 -04:00
parent 49571459e8
commit b109f05eee
5 changed files with 93 additions and 16 deletions

View File

@ -3,12 +3,22 @@ use warnings;
use strict;
use Dancer;
use Dancer::Session::Simple;
use DBI;
use Time::localtime;
use LWP::UserAgent;
my $dbfile = 'ksiazki.db';
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
my ($year,$mon,$mday,$hour,$min,$sec);
$year = localtime->year() + 1900;
$mon = localtime->mon() +1 ; #Miesiace zaczynamy liczyc od 1, a nie od 0, wiec zeby styczen mial numer 1 dodajemy 1
$mday = localtime->mday();
$hour = localtime->hour();
$min = localtime->min();
$sec = localtime->sec();
get '/' => sub
{
my $sth = $dbh->prepare("select * from ksiazki;") or die "Nie mozna spreparowac zapytania";
@ -18,6 +28,7 @@ get '/' => sub
# |kolejne rekordy|kolejne pola rekordow
#return ${$result[2]}[4];
template 'main_page.tt',
{
'table' => \@result
@ -30,46 +41,102 @@ get '/req/:book' => sub
my ($id,$who) = split (/,/,params->{book});
my $sth=$dbh->prepare("select can_borrow from ksiazki where id=?") or die "Nie mozna spreparowac zapytania";
$sth->execute($id) or die "Nie mozna wykonac";
my $can_borrow = $sth->fetchrow_array or die "Nie mozna pobrac danych";
if ($can_borrow == 1)
my $can_borrow = $sth->fetchrow_array or die "Nie mozna pobrac danych"; #Uwaga w or die... moze byc potencjalny blad!!!
$sth=$dbh->prepare("select reserved from ksiazki where id=?") or die "Nie mozna spreparowac zapytania";
$sth->execute($id) or die "Nie mozna wykonac";
my $reserved = $sth->fetchrow_array;
if ($can_borrow == 1 && $reserved == 0)
{
my $sth=$dbh->prepare("select who from ksiazki where id=?") or die "Nie mozna spreparowac zapytania";
$sth->execute($id) or die "Nie mozna wykonac";
my $who_from_base = $sth->fetchrow_array or die "Nie mozna pobrac danych";
my $when_borrow='wzieta: ';
if ($who_from_base ne 'nikt') #Jesli jest zapisane, ze ksiazka jest wzieta przez kogos, to znaczy ze teraz zostaje zwrocona
{
$who='nikt';
$when_borrow='zwrocona: ';
}
my ($year,$mon,$mday,$hour,$min,$sec);
$year = localtime->year() + 1900;
$mon = localtime->mon() +1 ; #Miesiace zaczynamy liczyc od 1, a nie od 0, wiec zeby styczen mial numer 1 dodajemy 1
$mday = localtime->mday();
$hour = localtime->hour();
$min = localtime->min();
$sec = localtime->sec();
$when_borrow = $when_borrow."$hour:$min:$sec, $mday $mon $year";
$sth = $dbh->prepare("update ksiazki set state=not state,who=?,comment=? where id=?") or die "Nie mozna spreparowac zapytania";
$sth->execute($who,$when_borrow,$id) or die "Nie mozna wykonac zapytania";
return 0;
}
elsif ($can_borrow == 0)
elsif ($reserved == 1)
{
return "Ksiazka jest zarezerwowana, nie mozna jej wypozyczyc";
}
else
{
return 1;
}
else #Stalo sie cos dziwnego...
{
return "-1";
}
};
post '/res' => sub #do zrobienia, rezerwacja
{
#return params->{book_id};
return "Not implemented yet";
if (session('user'))
{
my $sth = $dbh->prepare("update ksiazki set reserved=1,who=?,comment='zarezerwowana:$hour:$min:$sec, $mday $mon $year' where id=?") or die "Nie mozna spreparowac zapytania";
my $user=session('user');
my $book=params->{book_id};
$sth->execute($user,$book) or die "Nie mozna wykonac";
return "Zarezerwowano ksiazke";
}
else
{
return "Zaloguj sie, aby rezerwowac ksiazki";
}
};
get '/login' => sub
{
template 'login.tt',
{
}
};
get '/logout' => sub
{
session->destroy;
};
post '/login' => sub
{
my $user=params->{user};
my $password=params->{password};
my $ua = LWP::UserAgent->new;
$ua->agent('Biblioteka Warszawskiego Hackerspace\'u');
my $req = HTTP::Request->new(POST => 'https://auth.hackerspace.pl');
$req->content_type('application/x-www-form-urlencoded');
$req->content("login=$user&password=$password");
my $res = $ua->request($req);
if ($res->is_success)
{
if ($res->content eq 'OK')
{
session user => $user;
return "Zalogowany jako $user";
}
else
{
return 'Zly login lub haslo!';
}
}
else
{
return $res->status_line;
}
};
dance;

Binary file not shown.

View File

@ -22,6 +22,7 @@ charset: "UTF-8"
#template: "simple"
template: 'mojo_template'
session: 'simple'
# template: "template_toolkit"
# engines:
@ -30,3 +31,9 @@ template: 'mojo_template'
# start_tag: '[%'
# end_tag: '%]'
#logger: 'file'
#log: 'core' # will log all messages, including messages from Dancer itself
#log: 'debug' # will log debug, info, warning and error messages
#log: 'info' # will log info, warning and error messages
#log: 'warning' # will log warning and error messages
#log: 'error' # will log error messages

View File

View File

@ -1,5 +1,8 @@
<html>
<body>
<a href="/login">logowanie</a>
<a href="/logout">wyloguj sie</a>
% (my @tab) = @{$_[0]->{'table'}};
% my $pool_id=0;
% my $pool_isbn=1;
@ -11,7 +14,7 @@
% my $pool_can_borrow=7;
% my $pool_reserved=8;
% my $pool_comment=9;
<table border=7>
<table border=7 bgcolor="lightyellow">
<tr><td>id</td><td>isbn</td><td>tytul</td><td>autor</td><td>wlasiciciel</td><td>jest?</td><td>kto?</td><td>mozna brac?</td><td>zarezerwowana?</td><td>komentarz</td></tr>
<tr>