From 5b91d09faa340a85b154a3fcd1342cafcc443f6b Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 14 Nov 2008 16:12:58 +0000 Subject: [PATCH] Speichern eines Buches abgeschlossen --- lib/FrBr/Books/Controller/Books.pm | 13 +- lib/FrBr/Books/Util/Book.pm | 230 ++++++++++++++++++++++++++++- 2 files changed, 241 insertions(+), 2 deletions(-) diff --git a/lib/FrBr/Books/Controller/Books.pm b/lib/FrBr/Books/Controller/Books.pm index 6166a23..17239e2 100644 --- a/lib/FrBr/Books/Controller/Books.pm +++ b/lib/FrBr/Books/Controller/Books.pm @@ -155,7 +155,7 @@ sub form_new : Path('new') { $self->bookdata_session2stash($c); $c->stash->{'book_edit'}{'title'} = "Neues Buch" unless $c->stash->{'book_edit'}{'title'}; - unless ( $c->request->params->{'book_form_sent'} ) { + unless ( $c->request->params->{'book_form_sent'} and $c->request->params->{'do_save'} ) { return 1; } @@ -179,6 +179,17 @@ sub do_save_book : Private { return 1 unless $self->check_formparams( $c, $book ); + eval { + die "Speichern des Buchs mißlungen." unless save_book( $c, $book ); + }; + if ( $@ ) { + $c->stash->{'error_message'} = $@; + return undef; + } + + $c->stash->{'template'} = 'books/save_success.tt2'; + delete $c->session->{'book_data_edit'} if exists $c->session->{'book_data_edit'}; + return 1; } diff --git a/lib/FrBr/Books/Util/Book.pm b/lib/FrBr/Books/Util/Book.pm index 20bb780..75669ea 100644 --- a/lib/FrBr/Books/Util/Book.pm +++ b/lib/FrBr/Books/Util/Book.pm @@ -284,6 +284,7 @@ sub save_book { $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; my $storage = $c->stash->{'storage'}; + $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4; my $save_func = sub { @@ -362,6 +363,10 @@ ENDE return undef unless $book_id; + save_buch_autoren( $c, $book_id, $book->{'autoren'} ); + save_buch_kategorien( $c, $book_id, $book->{'kategorien'} ); + save_buch_serien( $c, $book_id, $book->{'serien'} ); + return $book_id; } @@ -380,7 +385,230 @@ sub save_buch_autoren { my $K = ( caller(0) )[3] . "(): "; - + my $storage = $c->stash->{'storage'}; + $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4; + my $dbh = $storage->dbh(); + $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2; + + my $anzahl = 0; + $anzahl = scalar( @$autor_ids ) if $autor_ids and ref($autor_ids) and ref($autor_ids) eq 'ARRAY'; + + my $in = ''; + $in = '(' . join( ", ", map { $dbh->quote($_) } @$autor_ids ) . ")" if $anzahl; + + my $sql; + $sql = <debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n"; + warn $text; + } + + my $sth = $dbh->prepare($sql); + $sth->execute( @$qparams ); + + return 1 unless $anzahl; + + my $i = 0; + + $sql = <prepare($sql); + + for my $autor_id ( @$autor_ids ) { + + $qparams = []; + push @$qparams, $book_id; + push @$qparams, $autor_id; + push @$qparams, $i; + push @$qparams, $i; + + if ( $storage->debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n"; + warn $text; + } + + $sth->execute( @$qparams ); + + $i++; + } + +} + +#----------------------------------------------------------------------------------- + +=head2 save_buch_serien( $c, $book_id, @$serien_ids ) + +=cut + +sub save_buch_serien { + + my $c = shift; + my $book_id = shift; + my $serien_ids = shift; + + my $K = ( caller(0) )[3] . "(): "; + + my $storage = $c->stash->{'storage'}; + $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4; + my $dbh = $storage->dbh(); + $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2; + + my $anzahl = 0; + $anzahl = scalar( @$serien_ids ) if $serien_ids and ref($serien_ids) and ref($serien_ids) eq 'ARRAY'; + + my $in = ''; + $in = '(' . join( ", ", map { $dbh->quote($_) } @$serien_ids ) . ")" if $anzahl; + + my $sql; + $sql = <debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n"; + warn $text; + } + + my $sth = $dbh->prepare($sql); + $sth->execute( @$qparams ); + + return 1 unless $anzahl; + + my $i = 1; + + $sql = <prepare($sql); + + for my $serien_id ( @$serien_ids ) { + + $qparams = []; + push @$qparams, $book_id; + push @$qparams, $serien_id; + push @$qparams, $i; + push @$qparams, $i; + + if ( $storage->debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n"; + warn $text; + } + + $sth->execute( @$qparams ); + + $i++; + } + +} + +#----------------------------------------------------------------------------------- + +=head2 save_buch_kategorien( $c, $book_id, @$kategorie_ids ) + +=cut + +sub save_buch_kategorien { + + my $c = shift; + my $book_id = shift; + my $kategorie_ids = shift; + + my $K = ( caller(0) )[3] . "(): "; + + my $storage = $c->stash->{'storage'}; + $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4; + my $dbh = $storage->dbh(); + $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2; + + my $anzahl = 0; + $anzahl = scalar( @$kategorie_ids ) if $kategorie_ids and ref($kategorie_ids) and ref($kategorie_ids) eq 'ARRAY'; + + my $in = ''; + $in = '(' . join( ", ", map { $dbh->quote($_) } @$kategorie_ids ) . ")" if $anzahl; + + my $sql; + $sql = <debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n"; + warn $text; + } + + my $sth = $dbh->prepare($sql); + $sth->execute( @$qparams ); + + return 1 unless $anzahl; + + $sql = <prepare($sql); + + for my $kategorie_id ( @$kategorie_ids ) { + + $qparams = []; + push @$qparams, $book_id; + push @$qparams, $kategorie_id; + + if ( $storage->debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n"; + warn $text; + } + + $sth->execute( @$qparams ); + + } } -- 2.39.5