67 lines
1.8 KiB
Perl
Executable File
67 lines
1.8 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
use warnings;
|
|
use strict;
|
|
use Dancer;
|
|
use DBI;
|
|
|
|
#Struktura bazy
|
|
#id, int; isbn, int; tytul, string; autor, string; owner, string;
|
|
#state, bool (czy pozyczona, 1=dostepna, 0=wzieta); can_borrow, bool (mozna brac ze soba? 1=tak, 0=nie)
|
|
|
|
my $dbfile = 'ksiazki.db';
|
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
|
|
|
|
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 $type = $sth->fetchrow_array or die "Nie mozna pobrac danych";
|
|
if ($type == 1)
|
|
{
|
|
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";
|
|
|
|
if ($who_from_base ne 'nikt')
|
|
{
|
|
$who='nikt'
|
|
}
|
|
$sth = $dbh->prepare("update ksiazki set state=not state,who=? where id=?") or die "Nie mozna spreparowac zapytania";
|
|
$sth->execute($who,$id) or die "Nie mozna wykonac zapytania";
|
|
return 0;
|
|
}
|
|
elsif ($type == 0)
|
|
{
|
|
return 1;
|
|
}
|
|
else #Stalo sie cos dziwnego...
|
|
{
|
|
return "-1";
|
|
}
|
|
};
|
|
|
|
post '/res' => sub #do zrobienia, rezerwacja
|
|
{
|
|
#return params->{book_id};
|
|
return "Not implemented yet";
|
|
};
|
|
|
|
dance;
|