Biblioteka/bin/app.pl.backup

64 lines
1.3 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, 0=dostepna, 1=wzieta); type, 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");
$sth->execute();
my @id;
my @isbn;
my @title;
my @author;
my @owner;
my @state;
my @type;
my $result=''; #Poniewaz trzeba zwrocic string, wartosci kolejno pobierane z bazy,
#beda dodawane do zmiennej ze stringiem, na razie pustym
my $i=0;
while (($id[$i],$isbn[$i],$title[$i],$author[$i],$owner[$i],$state[$i],$type[$i])=$sth->fetchrow_array)
{
$result=$result.$id[$i].'|'.$isbn[$i].'|'.$title[$i].'|'.$author[$i].'|'.$owner[$i].'|'.$state[$i].'|'.$type[$i].'<br>'."\n";
$i++;
}
return $result;
};
get '/req/:book' => sub
{
my $id=params->{book};
my $sth;
$sth=$dbh->prepare("select type from ksiazki where id=$id");
$sth->execute();
my $type = $sth->fetchrow_array;
if ($type == 1)
{
$sth = $dbh->prepare("update ksiazki set state=not state where id=?");
$sth->execute($id);
return 0;
}
elsif ($type == 0)
{
return 1;
}
else #Stalo sie cos dziwnego...
{
return "OMGLOLWTF";
}
};
dance;