diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 698aa8a..b6e3d65 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -196,6 +196,70 @@ sub addEmailAddress($$$$) { } ###################################################################### +=begin getRequestType + +Arguments: + +=over + +=item type + + A string describing the request. + +=back + +Returns the id value of the request_type entry. undef is returned if there +is no request of that type. + +=cut + +sub getRequestType($$) { + my($self, $type) = @_; + + return undef if not defined $type; + my $val = $self->dbh()->selectall_hashref("SELECT id, type FROM request_type WHERE type = '$type'", 'type'); + return $val->{$type}{id} if (defined $val and defined $val->{$type} and defined $val->{$type}{id}); + return undef; +} +###################################################################### + +=begin addRequestType + +Arguments: + +=over + +=item type + + A string describing the request. die()'s if not defined. + +=back + +Returns the id value of the request_type entry. If the type already exists, +it is simply returned. + +=cut + +sub addRequestType($$) { + my($self, $requestType) = @_; + + die "addRequestType: undefined request type." unless defined $requestType; + + my $requestId = $self->getRequestType($requestType); + return $requestId if (defined $requestId); + + $self->dbh->begin_work(); + + my $sth = $self->dbh->prepare("INSERT INTO request_type(type) VALUES(?)"); + + $sth->execute($requestType); + $requestId = $self->dbh->last_insert_id("","","",""); + $sth->finish(); + $self->dbh->commit(); + return $requestId; +} +###################################################################### + =head1 Non-Public Methods These methods are part of the internal implementation are not recommended for diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 5d24d1d..0728c2a 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -5,7 +5,7 @@ use strict; use warnings; -use Test::More tests => 25; +use Test::More tests => 31; use Test::Exception; use Scalar::Util qw(looks_like_number); @@ -118,6 +118,30 @@ ok($same = $sp->addAddressType("paypal payer"), "addAddressType: lookup works"); ok($same == $paypalPayerAddressType, "addAddressType: lookup returns same as the basic add"); +=item addAddressType/getRequestType + +=cut + +dies_ok { $sp->addRequestType(undef); } + "addRequestType: undef argument dies."; + +my $requestTypeId; + +ok( (not defined $sp->getRequestType('t-shirt-0')), "getRequestType: returns undef when not found"); + +lives_ok { $requestTypeId = $sp->addRequestType('t-shirt-0'); } + "addRequestType: succeeds on add"; + +ok( (defined $requestTypeId and looks_like_number($requestTypeId) and $requestTypeId > 0), + "addRequestType: id is a number"); + +my $testSameRequestType; + +lives_ok { $testSameRequestType = $sp->addRequestType('t-shirt-0'); } + "addRequestType: succeeds on add when type already exists"; + +is $requestTypeId, $testSameRequestType, + "addRequestType: lookup first of existing request type before adding."; =back