summaryrefslogtreecommitdiffstats
path: root/bin/app.pl
blob: 7842b0f59ace060a031ba5e3e9aee77a5347bfda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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 $sth=$dbh->do("select * from ksiazki");	
	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
{
	my $id=params->{book};
	my $sth;
	
	$sth=$dbh->prepare("select type 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)
	{
		$sth = $dbh->prepare("update ksiazki set state=not state where id=?") or die "Nie mozna spreparowac zapytania";
		$sth->execute($id) or die "Nie mozna wykonac zapytania";
		return 0;
	}
	elsif ($type == 0)
	{
		return 1;
	}
	else #Stalo sie cos dziwnego...
	{
		return "-1";
	}
};


dance;