From 05654fe11f50a79b1f993ba61b66a63a5e0989bd Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Wed, 16 Dec 2015 20:24:52 -0800 Subject: [PATCH] Implementation of addPostalAddress(). All tests related to addPostalAddress now pass. ok 28 - addPostalAddress: dies for undefined id ok 29 - addPostalAddress: dies for non-numeric id ok 30 - addPostalAddress: postal address undefined fails ok 31 - addPostalAddress: type is not added when other input paramaters are invalid ok 32 - addPostalAddress: addPostalAddress of a valid formatted_address works. ok 33 - addPostalAddress: id returned is sane. --- Supporters/lib/Supporters.pm | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 24338d4..f9c4651 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -259,7 +259,38 @@ Returns the id value of the postal_address table entry. sub addPostalAddress($$$$) { my($self, $id, $formattedPostalAddress, $addressType) = @_; - return undef; + die "addPostalAddress: invalid id, $id" unless $self->_verifyId($id); + die "addPostalAddress: the formatted postal address must be defined" + unless defined $formattedPostalAddress; + + $self->_beginWork(); + + my $addressTypeId; + eval { + $addressTypeId = $self->addAddressType($emailAddressType); + }; + if ($@ or not defined $addressTypeId) { + my $err = $@; + $err = "addPostalAddress: unable to addAddressType" if (not defined $err); + $self->_rollback(); + die $@ if $@; + } + my $sth = $self->dbh->prepare("INSERT INTO postal_address(formatted_address, type_id, date_encountered)" . + "VALUES( ?, ?, date('now'))"); + + $sth->execute($formattedPostalAddress, $addressTypeId); + my $addressId = $self->dbh->last_insert_id("","","",""); + $sth->finish(); + + $sth = $self->dbh->prepare("INSERT INTO supporter_postal_address_mapping" . + "(supporter_id, postal_address_id) " . + "VALUES( ?, ?)"); + $sth->execute($id, $addressId); + $sth->finish(); + + $self->_commit(); + + return $addressId; } ######################################################################