diff --git a/bin/app.pl b/bin/app.pl index b450be9..8149913 100755 --- a/bin/app.pl +++ b/bin/app.pl @@ -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; diff --git a/bin/ksiazki.db b/bin/ksiazki.db index 26113d5..2677920 100644 Binary files a/bin/ksiazki.db and b/bin/ksiazki.db differ diff --git a/config.yml b/config.yml index 2b2e432..b37c886 100644 --- a/config.yml +++ b/config.yml @@ -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 diff --git a/views/ksiazki.db b/views/ksiazki.db deleted file mode 100644 index e69de29..0000000 diff --git a/views/main_page.tt b/views/main_page.tt index ce0978b..7380a8a 100644 --- a/views/main_page.tt +++ b/views/main_page.tt @@ -1,5 +1,8 @@ + logowanie + wyloguj sie + % (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; - +
idisbntytulautorwlasicicieljest?kto?mozna brac?zarezerwowana?komentarz