143 lines
3.5 KiB
Perl
Executable File
143 lines
3.5 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
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";
|
|
$sth->execute() or die "Nie mozna wykonac zapytania";
|
|
my @result;
|
|
(@result)=@{$sth->fetchall_arrayref} or die "blad przy pobieraniu danych";
|
|
|
|
# |kolejne rekordy|kolejne pola rekordow
|
|
#return ${$result[2]}[4];
|
|
|
|
template 'main_page.tt',
|
|
{
|
|
'table' => \@result
|
|
}
|
|
};
|
|
|
|
get '/req/:book' => sub
|
|
{
|
|
#TODO:Zamienic if'y na odpowiednie zapytania sql
|
|
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"; #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: ';
|
|
}
|
|
|
|
$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 ($reserved == 1)
|
|
{
|
|
return "Ksiazka jest zarezerwowana, nie mozna jej wypozyczyc";
|
|
}
|
|
|
|
else
|
|
{
|
|
return 1;
|
|
}
|
|
};
|
|
|
|
post '/res' => sub #do zrobienia, rezerwacja
|
|
{
|
|
#return params->{book_id};
|
|
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;
|