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
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Main',
'user_id' => 1,
'defaut' => true,
),
array(
'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',
'adresse' => '4 rue Ménars',
'adresse2' => null,
......@@ -63,9 +35,7 @@ class AddressSeeder extends AbstractSeed
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Third address',
'user_id' => 2,
'defaut' => false,
),
);
......
......@@ -17,7 +17,7 @@ environments:
development:
adapter: mysql
host: localhost
name: soutien
name: don
user: soutien
pass: 'soutien'
port: 3306
......
......@@ -7,27 +7,23 @@ class AddressCreateCommand
private $addressId;
private $userId;
private $name;
private $alias;
private $address;
private $address2;
private $postalCode;
private $city;
private $country;
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->name = $name;
$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 getUserId()
......@@ -40,11 +36,6 @@ class AddressCreateCommand
return $this->name;
}
public function getAlias()
{
return $this->alias;
}
public function getAddress()
{
return $this->address;
......@@ -74,9 +65,4 @@ class AddressCreateCommand
{
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
public function findByUserId($userId)
{
$userId = (int) $userId;
$addresses = [];
$stmt = $this->connection->executeQuery('SELECT * FROM adresses WHERE user_id = :user_id', ['user_id' => $userId]);
while ($address = $stmt->fetch()) {
$addresses[$address['id']] = $address;
}
return $addresses;
$addresse = $stmt->fetch();
return $addresse;
}
}
......@@ -19,9 +19,7 @@ class AddressHandler
}
/**
* Delete an address. If there's a default adress, we should replace it in
* dons and contreparties. If there's none and the adress is used
* in contreparties, we should raise an error.
* Delete an address.
*
* @param AddressDeleteCommand $command
*/
......@@ -33,63 +31,12 @@ class AddressHandler
$addressId = $command->getAddressId();
// Let's check if the address is used
if ($this->addressUsed($addressId) == true) {
// First lets' check if there is a default Address we could use
$default = $this->getDefaultAddress($user_id);
$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();
};
// The address is used somehow
throw new AddressUsedException();
};
$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.
*
......@@ -98,64 +45,24 @@ EOF;
public function handleAddressCreateCommand(AddressCreateCommand $command)
{
$userId = $command->getUserId();
$alias = $command->getAlias();
if ($this->addressExists($userId, $alias)) {
throw new AddressAlreadyExistsException();
}
if ($isDefault = $command->isDefault()) {
$this->undefaultAddresses($userId);
}
$query =<<<EOF
INSERT INTO adresses(user_id, nom, alias, adresse, adresse2, codepostal, ville, etat, pays, defaut)
VALUES (:user_id, :name, :alias, :address, :address2, :postal_code, :city, :state, :country, :default)
REPLACE INTO adresses(user_id, nom, adresse, adresse2, codepostal, ville, etat, pays)
VALUES (:user_id, :name, :address, :address2, :postal_code, :city, :state, :country)
EOF;
$stmt = $this->connection->prepare($query);
$stmt->bindValue('user_id', $command->getUserId());
$stmt->bindValue('name', $command->getName());
$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('state', $command->getState());
$stmt->bindValue('country', $command->getCountry());
$stmt->bindValue('default', $isDefault);
$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.
*
......@@ -164,13 +71,12 @@ EOF;
*
* @return bool
*/
private function addressExists($userId, $alias)
private function addressExists($userId)
{
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,
'alias' => $alias,
],
0
);
......
......@@ -8,23 +8,21 @@ class AddressFinderTest extends FunctionalTest
{
public function testFindByUserId()
{
$addresses = $this->container['address_finder']->findByUserId(1);
$this->assertCount(3, $addresses);
$addresse = $this->container['address_finder']->findByUserId(1);
$this->assertCount(9, $addresse);
// Check the third address which have an id of 4
// Check the address have an ID of 1
$expectedAddress = [
'id' => '4',
'nom' => 'Third address',
'adresse' => '3 rue Ménars',
'id' => '1',
'nom' => 'Main',
'adresse' => '1 rue Ménars',
'adresse2' => null,
'codepostal' => '75003',
'codepostal' => '75001',
'ville' => 'Paris',
'etat' => null,
'pays' => 'France',
'alias' => 'Third address',
'user_id' => '1',
'defaut' => '0',
];
$this->assertEquals($expectedAddress, $addresses[4]);
$this->assertEquals($expectedAddress, $addresse);
}
}
......@@ -27,25 +27,18 @@ class AddressHandlerTest extends FunctionalTest
$this->assertFalse($this->addressExists(2));
}
public function testAddressUpdate()
public function testAddressCreate()
{
$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->assertTrue($this->isDefaultAddress(2));
$this->container['command_handler']->handle(new AddressCreateCommand(1, 1, '6 rue Ménars', '', 75002, 'Paris', 'France', 'IDF'));
}
public function testAddressCreateWithSpecialChars()
{
$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 = [
'nom' => 'New address',
......@@ -55,15 +48,12 @@ class AddressHandlerTest extends FunctionalTest
'ville' => 'Saint Brieuc',
'etat' => 'Côtes d\'Armor',
'pays' => 'France',
'alias' => 'updated',
'user_id' => '1',
'defaut' => '1',
];
$latestAddress = $this->getLatestAddress();
unset($latestAddress['id']);
$this->assertFalse($this->isDefaultAddress(1));
$this->assertSame($expectedAddress, $latestAddress);
}
......@@ -79,18 +69,6 @@ class AddressHandlerTest extends FunctionalTest
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.
*
......
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