diff --git a/app/controller/perso.php b/app/controller/perso.php index f5d3410becc28c87c6edc95a0134aefb1ddb1ff5..66cc6ed4b398ebb23f5488dea7e8b92e86703a1c 100644 --- a/app/controller/perso.php +++ b/app/controller/perso.php @@ -287,6 +287,27 @@ class Perso extends Controller } $f3->push('SESSION.message', _("Adresse ajoutée à votre profil")); break; + case 'UPDATE': + try { + $f3->get('container')['command_handler']->handle( + new AddressUpdateCommand( + \Utils::asl($f3->get('id')), + \Utils::asl($f3->get('SESSION.id')), + \Utils::asl($f3->get('nom')), + \Utils::asl($f3->get('adresse')), + \Utils::asl($f3->get('adresse2')), + \Utils::asl($f3->get('codepostal')), + \Utils::asl($f3->get('ville')), + \Utils::asl($f3->get('pays')), + \Utils::asl($f3->get('state')) + ) + ); + } catch (AddressAlreadyExistsException $e) { + $f3->set("error", _("Cette adresse existe déjà.")); + $f3->error('403'); + } + $f3->push('SESSION.message', _("Adresse du profil modifiée.")); + break; case 'DELETE': try { $f3->get('container')['command_handler']->handle( diff --git a/app/view/user/perso.html b/app/view/user/perso.html index d5d3aa7d64ea56528c266aa03facf83d8c68cdf1..524f8e4aca56325bacb16728f430fd4b6bcd1351 100644 --- a/app/view/user/perso.html +++ b/app/view/user/perso.html @@ -79,8 +79,15 @@ <h3>{{ _("Renseigner votre adresse.")}}</h3> <form method="POST" action="{{ 'adresses' | alias }}" id="create-adress-form"> <input type="hidden" name="csrf" value="{{ @CSRF }}" /> - <input type="hidden" name="action" value="ADD" /> + <check if="{{ @adresse }}"> + <true> + <input type="hidden" name="action" value="UPDATE" /> <input type="hidden" name="id" value="{{ @@adresse.id }}" /> + </true> + <false> + <input type="hidden" name="action" value="ADD" /> + </false> + </check> <div class="form-group"> <label for="nom">{{ _("Destinataire") }}</label> <input type="text" class="form-control" name="nom" value="{{ @@adresse.nom }}"> diff --git a/src/LQDN/Command/AddressUpdateCommand.php b/src/LQDN/Command/AddressUpdateCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..e094fd336ab88de8890006dab06971c33d300e46 --- /dev/null +++ b/src/LQDN/Command/AddressUpdateCommand.php @@ -0,0 +1,74 @@ +<?php + +namespace LQDN\Command; + +class AddressUpdateCommand +{ + private $addressId; + private $userId; + private $name; + private $address; + private $address2; + private $postalCode; + private $city; + private $country; + private $state; + + public function __construct($addressId, $userId, $name, $address, $address2, $postalCode, $city, $country, $state) + { + $this->addressId = $addressId; + $this->userId = $userId; + $this->name = $name; + $this->address = $address; + $this->address2 = $address2; + $this->postalCode = $postalCode; + $this->city = $city; + $this->country = $country; + $this->state = $state; + } + + public function getAddressId() + { + return $this->addressId; + } + + public function getUserId() + { + return $this->userId; + } + + public function getName() + { + return $this->name; + } + + public function getAddress() + { + return $this->address; + } + + public function getAddress2() + { + return $this->address2; + } + + public function getPostalCode() + { + return $this->postalCode; + } + + public function getCity() + { + return $this->city; + } + + public function getCountry() + { + return $this->country; + } + + public function getState() + { + return $this->state; + } +} diff --git a/src/LQDN/Handler/AddressHandler.php b/src/LQDN/Handler/AddressHandler.php index cbc247ffbdd087a00037f2b71e3508b3fa20af04..96eb32e6f2a317cf182b785fbded89cff4483ac1 100644 --- a/src/LQDN/Handler/AddressHandler.php +++ b/src/LQDN/Handler/AddressHandler.php @@ -63,6 +63,41 @@ EOF; $stmt->execute(); } + /** + * Update an address. + * + * @param AddressUpdateCommand $command + */ + public function handleAddressUpdateCommand(AddressUpdateCommand $command) + { + $addressId = $command->getAddressId(); + $userId = $command->getUserId(); + + // Let's check if the address is used + if ($this->addressUsed($addressId) == true) { + // The address is used somehow + throw new AddressUsedException(); + }; + + $query =<<<EOF +UPDATE adresses +SET nom = :name, adresse = :address, adresse2 = :address2, codepostal = :postal_code, ville = :city, etat = :state, pays = :country +WHERE id = :id and user_id = :user_id +EOF; + + $stmt = $this->connection->prepare($query); + $stmt->bindValue('id', $command->getAddressId()); + $stmt->bindValue('user_id', $command->getUserId()); + $stmt->bindValue('name', $command->getName()); + $stmt->bindValue('address', $command->getAddress()); + $stmt->bindValue('address2', $command->getAddress2()); + $stmt->bindValue('postal_code', $command->getPostalCode()); + $stmt->bindValue('city', $command->getCity()); + $stmt->bindValue('state', $command->getState()); + $stmt->bindValue('country', $command->getCountry()); + $stmt->execute(); + } + /** * Check if an address already exists. * diff --git a/tests/functional/Handler/AddressHandlerTest.php b/tests/functional/Handler/AddressHandlerTest.php index 81558168925e6fdf564d75d46eff1847d3c4bf72..a5798a53cbdc9523a9c74188a137c7898851d32c 100644 --- a/tests/functional/Handler/AddressHandlerTest.php +++ b/tests/functional/Handler/AddressHandlerTest.php @@ -61,6 +61,30 @@ class AddressHandlerTest extends FunctionalTest $this->assertSame($expectedAddress, $latestAddress); } + public function testAddressUpdate() + { + $this->assertTrue($this->addressExists(1)); + + $latestAddress = $this->getLatestAddress(); + + $this->container['command_handler']->handle(new AddressUpdateCommand($latestAddress['id'], 1, 'LQDN', '115 rue de Ménilmontant', '', 75020, 'Paris', 'France', 'IDF')); + + $expectedAddress = [ + 'id' => $latestAddress['id'], + 'user_id' => '1', + 'nom' => 'LQDN', + 'adresse' => '115 rue de Ménilmontant', + 'adresse2' => '', + 'codepostal' => '75020', + 'ville' => 'Paris', + 'pays' => 'France', + 'etat' => 'IDF', + ]; + + $updatedAddress = $this->getLatestAddress(); + $this->assertSame($expectedAddress, $updatedAddress); + } + /** * Check if an address exists in DB. *