diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 004ee9c..a103202 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -300,7 +300,8 @@ Arguments: =item descriptionListRef A list reference to the list of configuration descriptions to associate - with this requestId. + with this requestId. Duplicates aren't permitted in this list, and + die()'s if duplicates exist. =back @@ -323,7 +324,17 @@ sub addRequestConfigurations($$$) { unless defined $requestType; my %descriptions; - + my $sth = $self->dbh->prepare("INSERT INTO request_configuration(request_type_id, description) " . + "VALUES(?, ?)"); + foreach my $description (@{$descriptionListRef}) { + if (defined $descriptions{$description}) { + $self->dbh->rollback(); + die "addRequestConfigurations: attempt to create duplicate request_configuration \"$description\" for requestType, \"$requestType\""; + } + $sth->execute($requestId, $description); + $descriptions{$description} = $self->dbh->last_insert_id("","","",""); + } + $sth->finish(); $self->dbh->commit(); return { $requestId => \%descriptions }; } diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 7410d6c..69c0c20 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -5,7 +5,7 @@ use strict; use warnings; -use Test::More tests => 38; +use Test::More tests => 49; use Test::Exception; use Scalar::Util qw(looks_like_number); @@ -176,13 +176,13 @@ my @sizeList = qw/LadiesS LadiesM LadiesL LadiesXL MenS MenM MenL MenXL Men2XL/; my $tShirt0Data; -lives_ok { $tShirt0Data = $sp->addRequestConfigurations('t-shirt-0') } +lives_ok { $tShirt0Data = $sp->addRequestConfigurations('t-shirt-0', \@sizeList) } "addRequestConfigurations: existing requestType with configuration runs."; is( keys %{$tShirt0Data}, ($tShirt0RequestTypeId), "addRequestConfigurations: reuses same requestTypeId on add of configurations"); -my $cnt; +my $cnt = 0; foreach my $size (@sizeList) { ok( (defined $tShirt0Data->{$tShirt0RequestTypeId}{$size} and looks_like_number($tShirt0Data->{$tShirt0RequestTypeId}{$size}) and diff --git a/sql/supporters-schema.sql b/sql/supporters-schema.sql index 0a112e2..c93602e 100644 --- a/sql/supporters-schema.sql +++ b/sql/supporters-schema.sql @@ -29,6 +29,9 @@ CREATE TABLE "request_configuration" ( "description" varchar(100) NOT NULL ); +CREATE UNIQUE INDEX request_configuration__single_description + ON request_configuration(request_type_id, description); + DROP TABLE IF EXISTS "fulfillment"; CREATE TABLE "fulfillment" (