Commit 86670dba authored by okhin's avatar okhin 🚴

Remove default (and derived functions) and alias from the addresses

parent b84284d0
...@@ -24,38 +24,10 @@ class AddressSeeder extends AbstractSeed ...@@ -24,38 +24,10 @@ class AddressSeeder extends AbstractSeed
'ville' => 'Paris', 'ville' => 'Paris',
'etat' => null, 'etat' => null,
'pays' => 'France', 'pays' => 'France',
'alias' => 'Main',
'user_id' => 1, 'user_id' => 1,
'defaut' => true,
), ),
array( array(
'id' => 2, 'id' => 2,
'nom' => 'Second address',
'adresse' => '2 rue Ménars',
'adresse2' => null,
'codepostal' => 75002,
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Second address',
'user_id' => 1,
'defaut' => false,
),
array(
'id' => 4,
'nom' => 'Third address',
'adresse' => '3 rue Ménars',
'adresse2' => null,
'codepostal' => 75003,
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Third address',
'user_id' => 1,
'defaut' => false,
),
array(
'id' => 5,
'nom' => 'Bob address', 'nom' => 'Bob address',
'adresse' => '4 rue Ménars', 'adresse' => '4 rue Ménars',
'adresse2' => null, 'adresse2' => null,
...@@ -63,9 +35,7 @@ class AddressSeeder extends AbstractSeed ...@@ -63,9 +35,7 @@ class AddressSeeder extends AbstractSeed
'ville' => 'Paris', 'ville' => 'Paris',
'etat' => null, 'etat' => null,
'pays' => 'France', 'pays' => 'France',
'alias' => 'Third address',
'user_id' => 2, 'user_id' => 2,
'defaut' => false,
), ),
); );
......
...@@ -17,7 +17,7 @@ environments: ...@@ -17,7 +17,7 @@ environments:
development: development:
adapter: mysql adapter: mysql
host: localhost host: localhost
name: soutien name: don
user: soutien user: soutien
pass: 'soutien' pass: 'soutien'
port: 3306 port: 3306
......
...@@ -7,27 +7,23 @@ class AddressCreateCommand ...@@ -7,27 +7,23 @@ class AddressCreateCommand
private $addressId; private $addressId;
private $userId; private $userId;
private $name; private $name;
private $alias;
private $address; private $address;
private $address2; private $address2;
private $postalCode; private $postalCode;
private $city; private $city;
private $country; private $country;
private $state; private $state;
private $isDefault;
public function __construct($userId, $name, $alias, $address, $address2, $postalCode, $city, $country, $state, $isDefault) public function __construct($userId, $name, $address, $address2, $postalCode, $city, $country, $state)
{ {
$this->userId = $userId; $this->userId = $userId;
$this->name = $name; $this->name = $name;
$this->alias = $alias;
$this->address = $address; $this->address = $address;
$this->address2 = $address2; $this->address2 = $address2;
$this->postalCode = $postalCode; $this->postalCode = $postalCode;
$this->city = $city; $this->city = $city;
$this->country = $country; $this->country = $country;
$this->state = $state; $this->state = $state;
$this->isDefault = $isDefault;
} }
public function getUserId() public function getUserId()
...@@ -40,11 +36,6 @@ class AddressCreateCommand ...@@ -40,11 +36,6 @@ class AddressCreateCommand
return $this->name; return $this->name;
} }
public function getAlias()
{
return $this->alias;
}
public function getAddress() public function getAddress()
{ {
return $this->address; return $this->address;
...@@ -74,9 +65,4 @@ class AddressCreateCommand ...@@ -74,9 +65,4 @@ class AddressCreateCommand
{ {
return $this->state; return $this->state;
} }
public function isDefault()
{
return $this->isDefault;
}
} }
<?php
namespace LQDN\Command;
class AddressUpdateCommand
{
private $addressId;
private $userId;
private $alias;
private $address;
private $address2;
private $postalCode;
private $city;
private $country;
private $state;
private $isDefault;
public function __construct($addressId, $userId, $alias, $address, $address2, $postalCode, $city, $country, $state, $isDefault)
{
$this->addressId = $addressId;
$this->userId = $userId;
$this->alias = $alias;
$this->address = $address;
$this->address2 = $address2;
$this->postalCode = $postalCode;
$this->city = $city;
$this->country = $country;
$this->state = $state;
$this->isDefault = $isDefault;
}
public function getAddressId()
{
return $this->addressId;
}
public function getUserId()
{
return $this->userId;
}
public function getAlias()
{
return $this->alias;
}
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;
}
public function isDefault()
{
return $this->isDefault;
}
}
...@@ -23,12 +23,8 @@ class AddressFinder ...@@ -23,12 +23,8 @@ class AddressFinder
public function findByUserId($userId) public function findByUserId($userId)
{ {
$userId = (int) $userId; $userId = (int) $userId;
$addresses = [];
$stmt = $this->connection->executeQuery('SELECT * FROM adresses WHERE user_id = :user_id', ['user_id' => $userId]); $stmt = $this->connection->executeQuery('SELECT * FROM adresses WHERE user_id = :user_id', ['user_id' => $userId]);
while ($address = $stmt->fetch()) { $addresse = $stmt->fetch();
$addresses[$address['id']] = $address; return $addresse;
}
return $addresses;
} }
} }
...@@ -19,9 +19,7 @@ class AddressHandler ...@@ -19,9 +19,7 @@ class AddressHandler
} }
/** /**
* Delete an address. If there's a default adress, we should replace it in * Delete an address.
* dons and contreparties. If there's none and the adress is used
* in contreparties, we should raise an error.
* *
* @param AddressDeleteCommand $command * @param AddressDeleteCommand $command
*/ */
...@@ -33,63 +31,12 @@ class AddressHandler ...@@ -33,63 +31,12 @@ class AddressHandler
$addressId = $command->getAddressId(); $addressId = $command->getAddressId();
// Let's check if the address is used // Let's check if the address is used
if ($this->addressUsed($addressId) == true) { if ($this->addressUsed($addressId) == true) {
// First lets' check if there is a default Address we could use // The address is used somehow
$default = $this->getDefaultAddress($user_id); throw new AddressUsedException();
$params = ['id' => $command->getAddressId(),
'default_id' => $default];
if ($default != '' and $default != $addressId) {
// We have a default address which is not the current one
$this->connection->executeUpdate("UPDATE dons SET adresse_id = :default_id WHERE adresse_id = :id", $params);
$this->connection->executeUpdate("UPDATE contreparties SET adresse_id= :default_id WHERE adresse_id = :id", $params);
} else {
// We do not have a default address and the address is used somehow
throw new AddressUsedException();
};
}; };
$this->connection->executeUpdate("DELETE FROM adresses WHERE id = :id", ['id' => $addressId]); $this->connection->executeUpdate("DELETE FROM adresses WHERE id = :id", ['id' => $addressId]);
} }
/**
* Update an address.
*
* @param AddressUpdateCommand $command
*/
public function handleAddressUpdateCommand(AddressUpdateCommand $command)
{
$userId = $command->getUserId();
if ($isDefault = $command->isDefault()) {
$this->undefaultAddresses($userId);
}
$query = <<<EOF
UPDATE adresses
SET
alias = :alias,
adresse = :address,
adresse2 = :address2,
codepostal = :postal_code,
ville = :city,
pays = :country,
etat = :state,
defaut = :default
WHERE id = :address_id
AND user_id = :user_id;
EOF;
$stmt = $this->connection->prepare($query);
$stmt->bindValue('alias', $command->getAlias());
$stmt->bindValue('address', $command->getAddress());
$stmt->bindValue('address2', $command->getAddress2());
$stmt->bindValue('postal_code', $command->getPostalCode());
$stmt->bindValue('city', $command->getCity());
$stmt->bindValue('country', $command->getCountry());
$stmt->bindValue('state', $command->getState());
$stmt->bindValue('default', $command->isDefault());
$stmt->bindValue('address_id', $command->getAddressId());
$stmt->bindValue('user_id', $command->getUserId());
$stmt->execute();
}
/** /**
* Create an address. * Create an address.
* *
...@@ -98,64 +45,24 @@ EOF; ...@@ -98,64 +45,24 @@ EOF;
public function handleAddressCreateCommand(AddressCreateCommand $command) public function handleAddressCreateCommand(AddressCreateCommand $command)
{ {
$userId = $command->getUserId(); $userId = $command->getUserId();
$alias = $command->getAlias();
if ($this->addressExists($userId, $alias)) {
throw new AddressAlreadyExistsException();
}
if ($isDefault = $command->isDefault()) {
$this->undefaultAddresses($userId);
}
$query =<<<EOF $query =<<<EOF
INSERT INTO adresses(user_id, nom, alias, adresse, adresse2, codepostal, ville, etat, pays, defaut) REPLACE INTO adresses(user_id, nom, adresse, adresse2, codepostal, ville, etat, pays)
VALUES (:user_id, :name, :alias, :address, :address2, :postal_code, :city, :state, :country, :default) VALUES (:user_id, :name, :address, :address2, :postal_code, :city, :state, :country)
EOF; EOF;
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
$stmt->bindValue('user_id', $command->getUserId()); $stmt->bindValue('user_id', $command->getUserId());
$stmt->bindValue('name', $command->getName()); $stmt->bindValue('name', $command->getName());
$stmt->bindValue('alias', $command->getAlias());
$stmt->bindValue('address', $command->getAddress()); $stmt->bindValue('address', $command->getAddress());
$stmt->bindValue('address2', $command->getAddress2()); $stmt->bindValue('address2', $command->getAddress2());
$stmt->bindValue('postal_code', $command->getPostalCode()); $stmt->bindValue('postal_code', $command->getPostalCode());
$stmt->bindValue('city', $command->getCity()); $stmt->bindValue('city', $command->getCity());
$stmt->bindValue('state', $command->getState()); $stmt->bindValue('state', $command->getState());
$stmt->bindValue('country', $command->getCountry()); $stmt->bindValue('country', $command->getCountry());
$stmt->bindValue('default', $isDefault);
$stmt->execute(); $stmt->execute();
} }
/**
* Undefault addresses of a given user.
*
* @param int $userId
*/
private function undefaultAddresses($userId)
{
$this->connection->executeUpdate("UPDATE adresses SET defaut = FALSE WHERE user_id = :user_id", [
'user_id' => $userId,
]);
}
/**
* Get the default address of a given user.
*
* @param int $userId
*
* @return int
*/
private function getDefaultAddress($userId)
{
return (int) $this->connection->fetchColumn(
"SELECT id FROM adresses WHERE user_id = :user_id AND defaut = TRUE",
[
'user_id' => $userId,
],
0
);
}
/** /**
* Check if an address already exists. * Check if an address already exists.
* *
...@@ -164,13 +71,12 @@ EOF; ...@@ -164,13 +71,12 @@ EOF;
* *
* @return bool * @return bool
*/ */
private function addressExists($userId, $alias) private function addressExists($userId)
{ {
return (bool) $this->connection->fetchColumn( return (bool) $this->connection->fetchColumn(
"SELECT 1 FROM adresses WHERE user_id = :user_id AND alias = :alias", "SELECT 1 FROM adresses WHERE user_id = :user_id",
[ [
'user_id' => $userId, 'user_id' => $userId,
'alias' => $alias,
], ],
0 0
); );
......
...@@ -8,23 +8,21 @@ class AddressFinderTest extends FunctionalTest ...@@ -8,23 +8,21 @@ class AddressFinderTest extends FunctionalTest
{ {
public function testFindByUserId() public function testFindByUserId()
{ {
$addresses = $this->container['address_finder']->findByUserId(1); $addresse = $this->container['address_finder']->findByUserId(1);
$this->assertCount(3, $addresses); $this->assertCount(9, $addresse);
// Check the third address which have an id of 4 // Check the address have an ID of 1
$expectedAddress = [ $expectedAddress = [
'id' => '4', 'id' => '1',
'nom' => 'Third address', 'nom' => 'Main',
'adresse' => '3 rue Ménars', 'adresse' => '1 rue Ménars',
'adresse2' => null, 'adresse2' => null,
'codepostal' => '75003', 'codepostal' => '75001',
'ville' => 'Paris', 'ville' => 'Paris',
'etat' => null, 'etat' => null,
'pays' => 'France', 'pays' => 'France',
'alias' => 'Third address',
'user_id' => '1', 'user_id' => '1',
'defaut' => '0',
]; ];
$this->assertEquals($expectedAddress, $addresses[4]); $this->assertEquals($expectedAddress, $addresse);
} }
} }
...@@ -27,25 +27,18 @@ class AddressHandlerTest extends FunctionalTest ...@@ -27,25 +27,18 @@ class AddressHandlerTest extends FunctionalTest
$this->assertFalse($this->addressExists(2)); $this->assertFalse($this->addressExists(2));
} }
public function testAddressUpdate() public function testAddressCreate()
{ {
$this->assertTrue($this->addressExists(1)); $this->assertTrue($this->addressExists(1));
$this->assertTrue($this->isDefaultAddress(1));
$this->assertTrue($this->addressExists(2));
$this->assertFalse($this->isDefaultAddress(2));
$this->container['command_handler']->handle(new AddressUpdateCommand(2, 1, 'updated', '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF', true));
$this->assertFalse($this->isDefaultAddress(1)); $this->container['command_handler']->handle(new AddressCreateCommand(1, 1, '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF'));
$this->assertTrue($this->isDefaultAddress(2));
} }
public function testAddressCreateWithSpecialChars() public function testAddressCreateWithSpecialChars()
{ {
$this->assertTrue($this->addressExists(1)); $this->assertTrue($this->addressExists(1));
$this->assertTrue($this->isDefaultAddress(1));
$this->container['command_handler']->handle(new AddressCreateCommand(1, 'New address', 'updated', '6 rue Ménars', '', 22000, 'Saint Brieuc', 'France', 'Côtes d\'Armor', true)); $this->container['command_handler']->handle(new AddressCreateCommand(1, 'New address', '6 rue Ménars', '', 22000, 'Saint Brieuc', 'France', 'Côtes d\'Armor'));
$expectedAddress = [ $expectedAddress = [
'nom' => 'New address', 'nom' => 'New address',
...@@ -55,15 +48,12 @@ class AddressHandlerTest extends FunctionalTest ...@@ -55,15 +48,12 @@ class AddressHandlerTest extends FunctionalTest
'ville' => 'Saint Brieuc', 'ville' => 'Saint Brieuc',
'etat' => 'Côtes d\'Armor', 'etat' => 'Côtes d\'Armor',
'pays' => 'France', 'pays' => 'France',
'alias' => 'updated',
'user_id' => '1', 'user_id' => '1',
'defaut' => '1',
]; ];
$latestAddress = $this->getLatestAddress(); $latestAddress = $this->getLatestAddress();
unset($latestAddress['id']); unset($latestAddress['id']);
$this->assertFalse($this->isDefaultAddress(1));
$this->assertSame($expectedAddress, $latestAddress); $this->assertSame($expectedAddress, $latestAddress);
} }
...@@ -79,18 +69,6 @@ class AddressHandlerTest extends FunctionalTest ...@@ -79,18 +69,6 @@ class AddressHandlerTest extends FunctionalTest
return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM adresses WHERE id = $id"); return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM adresses WHERE id = $id");
} }
/**
* Check if an address is marked as default.
*
* @param int $id
*
* @return bool
*/
private function isDefaultAddress($id)
{
return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM adresses WHERE id = $id and defaut=true");
}
/** /**
* Retrieve latest address. * Retrieve latest address.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment