From af9821709cdfd01a42c98faa0a39e5939ffa397f Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Wed, 6 Dec 2023 15:18:03 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Grosse=20Am=C3=A9lioration=20Gateways,=20Ma?= =?UTF-8?q?ppers,=20Entity=20Athl=C3=A8te=20et=20Coach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/data/core/database/AthleteEntity.php | 18 ++ .../src/data/core/database/AthleteGateway.php | 69 ++++-- .../src/data/core/database/AthleteMapper.php | 10 +- .../src/data/core/database/CoachEntity.php | 72 +----- .../src/data/core/database/CoachGateway.php | 37 +-- .../src/data/core/database/CoachMapper.php | 62 ++--- Sources/tests/GatewayTest.php | 217 ++++++++++++++++++ Sources/tests/MapperTest.php | 43 ++++ 8 files changed, 387 insertions(+), 141 deletions(-) create mode 100644 Sources/tests/GatewayTest.php create mode 100644 Sources/tests/MapperTest.php diff --git a/Sources/src/data/core/database/AthleteEntity.php b/Sources/src/data/core/database/AthleteEntity.php index 812543c8..334a66c8 100644 --- a/Sources/src/data/core/database/AthleteEntity.php +++ b/Sources/src/data/core/database/AthleteEntity.php @@ -12,6 +12,8 @@ class AthleteEntity { private $poids; private $motDePasse; private $dateNaissance; + private $isCoach; + private $coachId; // Getters public function getIdAthlete() { @@ -50,6 +52,14 @@ class AthleteEntity { return $this->dateNaissance; } + public function getIsCoach(){ + return $this->isCoach; + } + + public function getCoachId(){ + return $this->coachId; + } + // Setters public function setIdAthlete($idAthlete) { $this->idAthlete = $idAthlete; @@ -86,6 +96,14 @@ class AthleteEntity { public function setDateNaissance($dateNaissance) { $this->dateNaissance = $dateNaissance; } + + public function setIsCoach($isCoach){ + $this->isCoach = $isCoach; + } + + public function setCoachId($coachId){ + $this->coachId = $coachId; + } } ?> diff --git a/Sources/src/data/core/database/AthleteGateway.php b/Sources/src/data/core/database/AthleteGateway.php index 7b2dc45e..6684630b 100644 --- a/Sources/src/data/core/database/AthleteGateway.php +++ b/Sources/src/data/core/database/AthleteGateway.php @@ -10,62 +10,91 @@ class AthleteGateway { $this->connection = $connection; } - public function getAthlete() { + public function getAthlete(): array + { $query = "SELECT * FROM Athlete"; return $this->connection->executeWithErrorHandling($query); } - public function getAthleteById(int $userId) { + public function getAthleteById(int $userId): array + { $query = "SELECT * FROM Athlete WHERE idAthlete = :id"; $params = [':id' => [$userId, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByName(string $name) { + public function getAthleteByName(string $name): array + { $query = "SELECT * FROM Athlete WHERE nom = :name"; $params = [':name' => [$name, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByFirstName(string $firstName) { + public function getAthleteByFirstName(string $firstName): array + { $query = "SELECT * FROM Athlete WHERE prenom = :firstName"; $params = [':firstName' => [$firstName, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByEmail(string $email) { + public function getAthleteByEmail(string $email): array + { $query = "SELECT * FROM Athlete WHERE email = :email"; $params = [':email' => [$email, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByGender(string $gender) { + public function getAthleteByGender(string $gender): array + { $query = "SELECT * FROM Athlete WHERE sexe = :gender"; $params = [':gender' => [$gender, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByHeight(int $height) { + public function getAthleteByHeight(int $height): array + { $query = "SELECT * FROM Athlete WHERE taille = :height"; $params = [':height' => [$height, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByWeight(int $weight) { + public function getAthleteByWeight(int $weight): array + { $query = "SELECT * FROM Athlete WHERE poids = :weight"; $params = [':weight' => [$weight, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getAthleteByBirthDate(string $birthdate) { + public function getAthleteByBirthDate(string $birthdate): array + { $query = "SELECT * FROM Athlete WHERE dateNaissance = :birthdate"; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function addAthlete(AthleteEntity $athlete) { - $query = "INSERT INTO Athlete (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance) - VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance)"; + public function getAthleteByIsCoachTrue(): array + { + $query = "SELECT * FROM Athlete WHERE isCoach = TRUE"; + return $this->connection->executeWithErrorHandling($query); + } + + public function getAthleteByIsCoachFalse(): array + { + $query = "SELECT * FROM Athlete WHERE isCoach = FALSE"; + return $this->connection->executeWithErrorHandling($query); + } + + public function getAthleteByCoachId(int $coachId): array + { + $query = "SELECT * FROM Athlete WHERE coachId = :coachId"; + $params = [':coachId' => [$coachId, PDO::PARAM_INT]]; + return $this->connection->executeWithErrorHandling($query, $params); + } + + public function addAthlete(AthleteEntity $athlete): array + { + $query = "INSERT INTO Athlete (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance, isCoach, coachId) + VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance, :isCoach, :coachId)"; $params = [ ':nom' => $athlete->getNom(), @@ -75,16 +104,19 @@ class AthleteGateway { ':taille' => $athlete->getTaille(), ':poids' => $athlete->getPoids(), ':motDePasse' => $athlete->getMotDePasse(), - ':dateNaissance' => $athlete->getDateNaissance(), + ':dateNaissance' => $athlete->getDateNaissance(), + ':isCoach' => $athlete->getIsCoach(), + ':coachId' => $athlete->getCoachId(), ]; return $this->connection->executeWithErrorHandling($query, $params); } - public function updateAthlete(AthleteEntity $oldAthlete, AthleteEntity $newAthlete) { + public function updateAthlete(AthleteEntity $oldAthlete, AthleteEntity $newAthlete): array + { $query = "UPDATE Athlete SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe, - taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance + taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance, isCoach = :isCoach, coachId = :coachId WHERE idAthlete = :idAthlete"; $params = [ @@ -96,13 +128,16 @@ class AthleteGateway { ':taille' => $newAthlete->getTaille(), ':poids' => $newAthlete->getPoids(), ':motDePasse' => $newAthlete->getMotDePasse(), - ':dateNaissance' => $newAthlete->getDateNaissance(), + ':dateNaissance' => $newAthlete->getDateNaissance(), + ':isCoach' => $athlete->getIsCoach(), + ':coachId' => $athlete->getCoachId(), ]; return $this->connection->executeWithErrorHandling($query, $params); } - public function deleteAthlete(int $idAthlete) { + public function deleteAthlete(int $idAthlete): array + { $query = "DELETE FROM Athlete WHERE idAthlete = :idAthlete"; $params = [ diff --git a/Sources/src/data/core/database/AthleteMapper.php b/Sources/src/data/core/database/AthleteMapper.php index 16f5f0be..55e0a216 100644 --- a/Sources/src/data/core/database/AthleteMapper.php +++ b/Sources/src/data/core/database/AthleteMapper.php @@ -8,7 +8,7 @@ use Model\Role; use Model\Athlete; class AthleteMapper { - public function fromSqlToEntity(array $data): array { + public function athleteSqlToEntity(array $data): array { $athleteEntities = []; foreach ($data as $athleteData) { @@ -49,6 +49,12 @@ class AthleteMapper { if (isset($athleteData['dateNaissance'])) { $athlete->setDateNaissance($athleteData['dateNaissance']); } + if (isset($athleteData['isCoach'])) { + $athlete->setIsCoach($athleteData['isCoach']); + } + if (isset($athleteData['coachId'])) { + $athlete->setCoachId($athleteData['coachId']); + } $athleteEntities[] = $athlete; } @@ -90,6 +96,8 @@ class AthleteMapper { $ath->setPoids($user->getPoids()); $ath->setMotDePasse($user->getMotDePasse()); $ath->setDateNaissance($user->getDateNaissance()); + $ath->setIsCoach(FALSE); + $ath->setCoachId(NULL); return $ath; } diff --git a/Sources/src/data/core/database/CoachEntity.php b/Sources/src/data/core/database/CoachEntity.php index 03af8b1c..a1648402 100644 --- a/Sources/src/data/core/database/CoachEntity.php +++ b/Sources/src/data/core/database/CoachEntity.php @@ -4,50 +4,15 @@ namespace Database; class CoachEntity { private $idCoach; - private $nom; - private $prenom; - private $email; - private $sexe; - private $taille; - private $poids; - private $motDePasse; - private $dateNaissance; + private $athleteId; // Getters public function getIdCoach() { return $this->idCoach; } - public function getNom() { - return $this->nom; - } - - public function getPrenom() { - return $this->prenom; - } - - public function getEmail() { - return $this->email; - } - - public function getSexe() { - return $this->sexe; - } - - public function getTaille() { - return $this->taille; - } - - public function getPoids() { - return $this->poids; - } - - public function getMotDePasse() { - return $this->motDePasse; - } - - public function getDateNaissance() { - return $this->dateNaissance; + public function getAthleteId() { + return $this->athleteId; } // Setters @@ -55,37 +20,10 @@ class CoachEntity { $this->idCoach = $idCoach; } - public function setNom($nom) { - $this->nom = $nom; + public function setAthleteId($athleteId) { + $this->athleteId = $athleteId; } - public function setPrenom($prenom) { - $this->prenom = $prenom; - } - - public function setEmail($email) { - $this->email = $email; - } - - public function setSexe($sexe) { - $this->sexe = $sexe; - } - - public function setTaille($taille) { - $this->taille = $taille; - } - - public function setPoids($poids) { - $this->poids = $poids; - } - - public function setMotDePasse($motDePasse) { - $this->motDePasse = $motDePasse; - } - - public function setDateNaissance($dateNaissance) { - $this->dateNaissance = $dateNaissance; - } } ?> diff --git a/Sources/src/data/core/database/CoachGateway.php b/Sources/src/data/core/database/CoachGateway.php index 5f795799..5d6afebd 100644 --- a/Sources/src/data/core/database/CoachGateway.php +++ b/Sources/src/data/core/database/CoachGateway.php @@ -22,54 +22,47 @@ class CoachGateway { } public function getCoachByName(string $name) { - $query = "SELECT * FROM Coach WHERE nom = :name"; + $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.nom = :name"; $params = [':name' => [$name, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } public function getCoachByFirstName(string $firstName) { - $query = "SELECT * FROM Coach WHERE prenom = :firstName"; + $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.prenom = :firstName"; $params = [':firstName' => [$firstName, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } public function getCoachByEmail(string $email) { - $query = "SELECT * FROM Coach WHERE email = :email"; + $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.email = :email"; $params = [':email' => [$email, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } public function getCoachByGender(string $gender) { - $query = "SELECT * FROM Coach WHERE sexe = :gender"; + $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.sexe = :gender"; $params = [':gender' => [$gender, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } public function getCoachByHeight(int $height) { - $query = "SELECT * FROM Coach WHERE taille = :height"; + $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.taille = :height"; $params = [':height' => [$height, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } public function getCoachByBirthDate(string $birthdate) { - $query = "SELECT * FROM Coach WHERE dateNaissance = :birthdate"; + $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.dateNaissance = :birthdate"; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } public function addCoach(CoachEntity $coach) { - $query = "INSERT INTO Coach (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance) - VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance)"; + $query = "INSERT INTO Coach (athleteId) + VALUES (:athleteId)"; $params = [ - ':nom' => $coach->getNom(), - ':prenom' => $coach->getPrenom(), - ':email' => $coach->getEmail(), - ':sexe' => $coach->getSexe(), - ':taille' => $coach->getTaille(), - ':poids' => $coach->getPoids(), - ':motDePasse' => $coach->getMotDePasse(), - ':dateNaissance' => $coach->getDateNaissance(), + ':athleteId' => $coach->getAthleteId(), ]; return $this->connection->executeWithErrorHandling($query, $params); @@ -77,20 +70,12 @@ class CoachGateway { public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach) { $query = "UPDATE Coach - SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe, - taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance + SET athleteId = :athleteId WHERE idCoach = :idCoach"; $params = [ ':idCoach' => $oldCoach->getIdCoach(), - ':nom' => $newCoach->getNom(), - ':prenom' => $newCoach->getPrenom(), - ':email' => $newCoach->getEmail(), - ':sexe' => $newCoach->getSexe(), - ':taille' => $newCoach->getTaille(), - ':poids' => $newCoach->getPoids(), - ':motDePasse' => $newCoach->getMotDePasse(), - ':dateNaissance' => $newCoach->getDateNaissance(), + ':athleteId' => $newCoach->getAthleteId(), ]; return $this->connection->executeWithErrorHandling($query, $params); diff --git a/Sources/src/data/core/database/CoachMapper.php b/Sources/src/data/core/database/CoachMapper.php index 76284918..3fd35c7f 100644 --- a/Sources/src/data/core/database/CoachMapper.php +++ b/Sources/src/data/core/database/CoachMapper.php @@ -8,34 +8,43 @@ use Model\Role; use Model\Coach; class CoachMapper { - public function map(array $data) { - $coach = new CoachEntity(); - $coach->setIdCoach($data['idCoach']); - $coach->setNom($data['nom']); - $coach->setPrenom($data['prenom']); - $coach->setEmail($data['email']); - $coach->setSexe($data['sexe']); - $coach->setTaille($data['taille']); - $coach->setPoids($data['poids']); - $coach->setMotDePasse($data['motDePasse']); - $coach->setDateNaissance($data['dateNaissance']); - - return $coach; + public function coachSqlToEntity(array $data): array { + $coachEntities = []; + + foreach ($data as $coachData) { + $coach = new CoachEntity(); + + if (isset($coachData['idCoach'])) { + $coach->setIdCoach($coachData['idCoach']); + } + + if (isset($coachData['athleteId'])) { + $coach->setAthleteId($coachData['athleteId']); + } + + $coachEntities[] = $athlete; + } + + return $coachEntities; } public function CoachEntityToModel(CoachEntity $coachEntity):User{ - $role = "Coach"; + $role = new Coach(); + + $dateSpecifique = $athleteEntity->getDateNaissance(); + $date = new DateTime($dateSpecifique); $user = new User( $coachEntity->getIdCoach(), - $coachEntity->getNom(), - $coachEntity->getPrenom(), - $coachEntity->getEmail(), - $coachEntity->getMotDePasse(), - $coachEntity->getSexe(), - $coachEntity->getTaille(), - $coachEntity->getPoids(), - $coachEntity->getDateNaissance(), + //$coachEntity->getNom(), + //$coachEntity->getPrenom(), + //$coachEntity->getEmail(), + //$coachEntity->getMotDePasse(), //A MODIFIER : Doit recup les valeurs dans Athlete qui correspond + //$coachEntity->getSexe(), + //$coachEntity->getTaille(), + //$coachEntity->getPoids(), + //$coachEntity->getDateNaissance(), + $date, $role ); @@ -46,14 +55,7 @@ class CoachMapper { $coach = new CoachEntity(); $coach->setIdCoach($user->getId()); - $coach->setNom($user->getNom()); - $coach->setPrenom($user->getPrenom()); - $coach->setEmail($user->getEmail()); - $coach->setSexe($user->getSexe()); - $coach->setTaille($user->getTaille()); - $coach->setPoids($user->getPoids()); - $coach->setMotDePasse($user->getMotDePasse()); - $coach->setDateNaissance($user->getDateNaissance()); + $coach->setAthleteId($user->getId()); //A MODIFIER !!! doit recup Id Athlete qui correspond return $coach; } diff --git a/Sources/tests/GatewayTest.php b/Sources/tests/GatewayTest.php new file mode 100644 index 00000000..f16bf9c8 --- /dev/null +++ b/Sources/tests/GatewayTest.php @@ -0,0 +1,217 @@ +getAthlete(); + //var_dump($result); + } + + /* Fonctionne mais en commentaire pour pas add et del a chaque fois + public function testAddAthlete(){ + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + + $athleteGateway = new AthleteGateway($connexion); + + $dateSpecifique = "2023-11-26"; + $timestamp = strtotime($dateSpecifique); + $dateSQL = date("Y-m-d", $timestamp); + + $athleteEntity = new AthleteEntity(); + $athleteEntity->setNom('John'); + $athleteEntity->setPrenom('Doe'); + $athleteEntity->setIdAthlete(1234); + $athleteEntity->setEmail('kevin.monteiro@gmail.fr'); + $athleteEntity->setSexe('H'); + $athleteEntity->setTaille(169); + $athleteEntity->setPoids(69); + $athleteEntity->setMotDePasse('motdepasse'); + $athleteEntity->setDateNaissance($dateSQL); + + $result2 = $athleteGateway->addAthlete($athleteEntity); + } + + + public function testDeleteAthlete(){ + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + $athleteGateway = new AthleteGateway($connexion); + $result = $athleteGateway->deleteAthlete( //idAthlete ); + var_dump($result); + + }*/ + + public function testUpdateAthlete(){ + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + $athleteGateway = new AthleteGateway($connexion); + + $dateSpecifique = "2004-08-26"; + $timestamp = strtotime($dateSpecifique); + $dateSQL = date("Y-m-d", $timestamp); + + $athleteEntity = new AthleteEntity(); + $athleteEntity->setNom('John'); + $athleteEntity->setPrenom('Doe'); + $athleteEntity->setIdAthlete(13); + $athleteEntity->setEmail('kevin.monteiro@gmail.fr'); + $athleteEntity->setSexe('H'); + $athleteEntity->setTaille(169); + $athleteEntity->setPoids(69); + $athleteEntity->setMotDePasse('motdepasse'); + $athleteEntity->setDateNaissance($dateSQL); + $athleteEntity->setIsCoach(FALSE); + $athleteEntity->setCoachId(NULL); + + $athleteEntity2 = new AthleteEntity(); + $athleteEntity2->setNom('Monteiro'); + $athleteEntity2->setPrenom('Kevin'); + $athleteEntity2->setIdAthlete(13); + $athleteEntity2->setEmail('kevin.monteiro@gmail.fr'); + $athleteEntity2->setSexe('H'); + $athleteEntity2->setTaille(169); + $athleteEntity2->setPoids(69); + $athleteEntity2->setMotDePasse('motdepasse'); + $athleteEntity2->setDateNaissance($dateSQL); + $athleteEntity2->setIsCoach(TRUE); + $athleteEntity2->setCoachId(1); + + $result = $athleteGateway->updateAthlete($athleteEntity, $athleteEntity2); + } + + //Partie concernant les Coachs + + public function testGetCoach() { + + //$dsn = "pgsql:host=londres;port=8888;dbname=dbkemonteiro2;user=kemonteiro2;password=Mdp"; + + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + + $coachGateway = new CoachGateway($connexion); + $result = $coachGateway->getCoach(); + var_dump($result); + } + /* + //Fonctionne PAS A PARTIR DE LA + public function testAddCoach(){ + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + + $coachGateway = new CoachGateway($connexion); + + $dateSpecifique = "2023-11-26"; + $timestamp = strtotime($dateSpecifique); + $dateSQL = date("Y-m-d", $timestamp); + + $coachEntity = new CoachEntity(); + $coachEntity->setNom('John'); + $coachEntity->setPrenom('Doe'); + $coachEntity->setIdCoach(1234); + $coachEntity->setEmail('kevin.monteiro@gmail.fr'); + $coachEntity->setSexe('H'); + $coachEntity->setTaille(169); + $coachEntity->setPoids(69); + $coachEntity->setMotDePasse('motdepasse'); + $coachEntity->setDateNaissance($dateSQL); + + $result2 = $coachGateway->addCoach($coachEntity); + } + + + public function testDeleteAthlete(){ + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + $athleteGateway = new AthleteGateway($connexion); + $result = $athleteGateway->deleteAthlete( //idAthlete ); + var_dump($result); + + }*/ + /* + public function testUpdateAthlete(){ + $dsn = "mysql:host=londres;dbname=dbkemonteiro2;"; + $username = "kemonteiro2"; + $password = "#Phpmyadmin63"; + + $connexion = new Connexion($dsn,$username,$password); + + $athleteGateway = new AthleteGateway($connexion); + + $dateSpecifique = "2004-08-26"; + $timestamp = strtotime($dateSpecifique); + $dateSQL = date("Y-m-d", $timestamp); + + $athleteEntity = new AthleteEntity(); + $athleteEntity->setNom('John'); + $athleteEntity->setPrenom('Doe'); + $athleteEntity->setIdAthlete(13); + $athleteEntity->setEmail('kevin.monteiro@gmail.fr'); + $athleteEntity->setSexe('H'); + $athleteEntity->setTaille(169); + $athleteEntity->setPoids(69); + $athleteEntity->setMotDePasse('motdepasse'); + $athleteEntity->setDateNaissance($dateSQL); + + $athleteEntity2 = new AthleteEntity(); + $athleteEntity2->setNom('Monteiro'); + $athleteEntity2->setPrenom('Kevin'); + $athleteEntity2->setIdAthlete(13); + $athleteEntity2->setEmail('kevin.monteiro@gmail.fr'); + $athleteEntity2->setSexe('H'); + $athleteEntity2->setTaille(169); + $athleteEntity2->setPoids(69); + $athleteEntity2->setMotDePasse('motdepasse'); + $athleteEntity2->setDateNaissance($dateSQL); + + $result = $athleteGateway->updateAthlete($athleteEntity, $athleteEntity2); + }*/ +} diff --git a/Sources/tests/MapperTest.php b/Sources/tests/MapperTest.php new file mode 100644 index 00000000..2e5565a0 --- /dev/null +++ b/Sources/tests/MapperTest.php @@ -0,0 +1,43 @@ +getAthlete(); + + $map = new AthleteMapper (); + //SQL To AthleteEntity + $athleteEntity = $map->athleteSqlToEntity($result); + + + foreach($athleteEntity as $ath){ + + $result = $ath->getNom(); + var_dump($result); + //Pour chaque AthleteEntity : Athlete Entity To User avec Role Athlete(Model) + $user = $map->athleteEntityToModel($ath); + var_dump($user->getId()); + //Pour chaque Athlete du Model -> Athlete Entity + $res = $map->athleteToEntity($user); + var_dump($res->getIdAthlete()); + } + } +} \ No newline at end of file From aba0924f647c04e5c38379d4781ad8318aa9c222 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Thu, 7 Dec 2023 14:26:12 +0100 Subject: [PATCH 2/3] Gateways, Mappers et Entity pour Athlete et Coach a tester mais fonctionnel normalement --- .../src/data/core/database/AthleteMapper.php | 5 ++- .../src/data/core/database/CoachGateway.php | 40 ++++++++++++++----- .../src/data/core/database/CoachMapper.php | 34 +++++++++------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/Sources/src/data/core/database/AthleteMapper.php b/Sources/src/data/core/database/AthleteMapper.php index 55e0a216..2938a71a 100644 --- a/Sources/src/data/core/database/AthleteMapper.php +++ b/Sources/src/data/core/database/AthleteMapper.php @@ -65,8 +65,8 @@ class AthleteMapper { public function athleteEntityToModel(AthleteEntity $athleteEntity): User { $role = new Athlete(); // Utilisez la classe Athlete - $dateSpecifique = $athleteEntity->getDateNaissance(); - $date = new DateTime($dateSpecifique); + $dateSpecific = $athleteEntity->getDateNaissance(); + $date = new DateTime($dateSpecific); $user = new User( $athleteEntity->getIdAthlete(), @@ -77,6 +77,7 @@ class AthleteMapper { $athleteEntity->getSexe(), $athleteEntity->getTaille(), $athleteEntity->getPoids(), + $athleteEntity->getDateNaissance(), $date, $role ); diff --git a/Sources/src/data/core/database/CoachGateway.php b/Sources/src/data/core/database/CoachGateway.php index 5d6afebd..63fc9875 100644 --- a/Sources/src/data/core/database/CoachGateway.php +++ b/Sources/src/data/core/database/CoachGateway.php @@ -10,54 +10,70 @@ class CoachGateway { $this->connection = $connection; } - public function getCoach() { + public function getCoach(): array + { $query = "SELECT * FROM Coach"; return $this->connection->executeWithErrorHandling($query); } - public function getCoachById(int $userId) { + public function getCoachById(int $userId): array + { $query = "SELECT * FROM Coach WHERE idCoach = :id"; $params = [':id' => [$userId, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getCoachByName(string $name) { + public function getAthleteByCoachId(int $coachId): array + { + $query = "SELECT * FROM Athlete a, Coach c WHERE a.coachId = :id AND a.isCoach = TRUE"; + $params = [':id' => [$coachId, PDO::PARAM_INT]]; + return $this->connection->executeWithErrorHandling($query, $params); + } + + public function getCoachByName(string $name): array + { $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.nom = :name"; $params = [':name' => [$name, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getCoachByFirstName(string $firstName) { + public function getCoachByFirstName(string $firstName): array + { $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.prenom = :firstName"; $params = [':firstName' => [$firstName, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getCoachByEmail(string $email) { + public function getCoachByEmail(string $email): array + { $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.email = :email"; $params = [':email' => [$email, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getCoachByGender(string $gender) { + public function getCoachByGender(string $gender): array + { $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.sexe = :gender"; $params = [':gender' => [$gender, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getCoachByHeight(int $height) { + public function getCoachByHeight(int $height): array + { $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.taille = :height"; $params = [':height' => [$height, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getCoachByBirthDate(string $birthdate) { + public function getCoachByBirthDate(string $birthdate): array + { $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.dateNaissance = :birthdate"; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function addCoach(CoachEntity $coach) { + public function addCoach(CoachEntity $coach): array + { $query = "INSERT INTO Coach (athleteId) VALUES (:athleteId)"; @@ -68,7 +84,8 @@ class CoachGateway { return $this->connection->executeWithErrorHandling($query, $params); } - public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach) { + public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach): array + { $query = "UPDATE Coach SET athleteId = :athleteId WHERE idCoach = :idCoach"; @@ -81,7 +98,8 @@ class CoachGateway { return $this->connection->executeWithErrorHandling($query, $params); } - public function deleteCoach(int $idCoach) { + public function deleteCoach(int $idCoach): array + { $query = "DELETE FROM Coach WHERE idCoach = :idCoach"; $params = [ diff --git a/Sources/src/data/core/database/CoachMapper.php b/Sources/src/data/core/database/CoachMapper.php index 3fd35c7f..f3fd48ec 100644 --- a/Sources/src/data/core/database/CoachMapper.php +++ b/Sources/src/data/core/database/CoachMapper.php @@ -1,6 +1,7 @@ setAthleteId($coachData['athleteId']); } - $coachEntities[] = $athlete; + $coachEntities[] = $coach; } return $coachEntities; } public function CoachEntityToModel(CoachEntity $coachEntity):User{ - $role = new Coach(); + $role = new CoachAthlete(); - $dateSpecifique = $athleteEntity->getDateNaissance(); - $date = new DateTime($dateSpecifique); + $idCoach = $coachEntity->getIdCoach(); + + $ath = getAthleteByCoachId($idCoach); + $athlete = athleteSqlToEntity($ath); + + $dateSpecific = $athlete->getDateNaissance(); + $date = new DateTime($dateSpecific); $user = new User( $coachEntity->getIdCoach(), - //$coachEntity->getNom(), - //$coachEntity->getPrenom(), - //$coachEntity->getEmail(), - //$coachEntity->getMotDePasse(), //A MODIFIER : Doit recup les valeurs dans Athlete qui correspond - //$coachEntity->getSexe(), - //$coachEntity->getTaille(), - //$coachEntity->getPoids(), - //$coachEntity->getDateNaissance(), + $athlete->getNom(), + $athlete->getPrenom(), + $athlete->getEmail(), + $athlete->getMotDePasse(), + $athlete->getSexe(), + $athlete->getTaille(), + $athlete->getPoids(), + $athlete->getDateNaissance(), $date, $role ); @@ -55,10 +61,10 @@ class CoachMapper { $coach = new CoachEntity(); $coach->setIdCoach($user->getId()); - $coach->setAthleteId($user->getId()); //A MODIFIER !!! doit recup Id Athlete qui correspond + $coach->setAthleteId($user->getId()); return $coach; } } - ?> + From fe12112ce6b9cd2c886279e8381a50f08a4ad59f Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Thu, 7 Dec 2023 15:17:33 +0100 Subject: [PATCH 3/3] Finition gateway Athlete Coach et Activite a tester mais normalement fonctionnel --- .../data/core/database/ActiviteGateway.php | 85 ++++++------- .../src/data/core/database/ActiviteMapper.php | 119 ++++++++++++++---- ...{ActiviteEntity.php => ActivityEntity.php} | 8 +- .../src/data/core/database/AthleteGateway.php | 2 +- 4 files changed, 141 insertions(+), 73 deletions(-) rename Sources/src/data/core/database/{ActiviteEntity.php => ActivityEntity.php} (94%) diff --git a/Sources/src/data/core/database/ActiviteGateway.php b/Sources/src/data/core/database/ActiviteGateway.php index 853cffa0..ed6db38c 100644 --- a/Sources/src/data/core/database/ActiviteGateway.php +++ b/Sources/src/data/core/database/ActiviteGateway.php @@ -1,38 +1,39 @@ connection = $connection; } - public function getActivite() { + public function getActivity() { $query = "SELECT * FROM Activite"; return $this->connection->executeWithErrorHandling($query); } - public function getActiviteById(int $activiteId) { + public function getActivityById(int $activityId) { $query = "SELECT * FROM Activite WHERE idActivite = :id"; - $params = [':id' => [$activiteId, PDO::PARAM_INT]]; + $params = [':id' => [$activityId, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getActiviteByType(string $type) { + public function getActivityByType(string $type) { $query = "SELECT * FROM Activite WHERE type = :type"; $params = [':type' => [$type, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getActiviteByDate(string $date) { + public function getActivityByDate(string $date) { $query = "SELECT * FROM Activite WHERE date = :date"; $params = [':date' => [$date, PDO::PARAM_STR]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getActiviteByTimeRange(string $startTime, string $endTime) { + public function getActivityByTimeRange(string $startTime, string $endTime) { $query = "SELECT * FROM Activite WHERE heureDebut >= :startTime AND heureFin <= :endTime"; $params = [ ':startTime' => [$startTime, PDO::PARAM_STR], @@ -41,76 +42,76 @@ class ActiviteGateway { return $this->connection->executeWithErrorHandling($query, $params); } - public function getActiviteByEffort(int $effortRessenti) { + public function getActivityByEffort(int $effortRessenti) { $query = "SELECT * FROM Activite WHERE effortRessenti = :effort"; $params = [':effort' => [$effortRessenti, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getActiviteByVariability(int $variabilite) { + public function getActivityByVariability(int $variabilite) { $query = "SELECT * FROM Activite WHERE variabilite = :variability"; $params = [':variability' => [$variabilite, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function getActiviteByTemperature(int $temperatureMoyenne) { + public function getActivityByTemperature(int $temperatureMoyenne) { $query = "SELECT * FROM Activite WHERE temperatureMoyenne = :temperature"; $params = [':temperature' => [$temperatureMoyenne, PDO::PARAM_INT]]; return $this->connection->executeWithErrorHandling($query, $params); } - public function addActivite(ActiviteEntity $activite) { + public function addActivity(ActivityEntity $activity) { $query = "INSERT INTO Activite (type, date, heureDebut, heureDeFin, effortRessenti, variabilite, variance, ecartType, moyenne, maximum, minimum, temperatureMoyenne) VALUES (:type, :date, :heureDebut, :heureDeFin, :effortRessenti, :variabilite, :variance, :ecartType, :moyenne, :maximum, :minimum, :temperatureMoyenne)"; $params = [ - ':type' => $activite->getType(), - ':date' => $activite->getDate()->format('Y-m-d'), // Format date pour SQL - ':heureDebut' => $activite->getHeureDebut()->format('H:i:s'), // Format heure pour SQL - ':heureDeFin' => $activite->getHeureFin()->format('H:i:s'), // Format heure pour SQL - ':effortRessenti' => $activite->getEffortRessenti(), - ':variabilite' => $activite->getVariabilite(), - ':variance' => $activite->getVariance(), - ':ecartType' => $activite->getEcartType(), - ':moyenne' => $activite->getMoyenne(), - ':maximum' => $activite->getMaximum(), - ':minimum' => $activite->getMinimum(), - ':temperatureMoyenne' => $activite->getTemperatureMoyenne(), + ':type' => $activity->getType(), + ':date' => $activity->getDate()->format('Y-m-d'), // Format date pour SQL + ':heureDebut' => $activity->getHeureDebut()->format('H:i:s'), // Format heure pour SQL + ':heureDeFin' => $activity->getHeureFin()->format('H:i:s'), // Format heure pour SQL + ':effortRessenti' => $activity->getEffortRessenti(), + ':variabilite' => $activity->getVariabilite(), + ':variance' => $activity->getVariance(), + ':ecartType' => $activity->getEcartType(), + ':moyenne' => $activity->getMoyenne(), + ':maximum' => $activity->getMaximum(), + ':minimum' => $activity->getMinimum(), + ':temperatureMoyenne' => $activity->getTemperatureMoyenne(), ]; return $this->connection->executeWithErrorHandling($query, $params); } - public function updateActivite(ActiviteEntity $oldActivite, ActiviteEntity $newActivite) { + public function updateActivity(ActivityEntity $oldActivity, ActivityEntity $newActivity) { $query = "UPDATE Activite SET type = :type, date = :date, heureDebut = :heureDebut, heureDeFin = :heureDeFin, effortRessenti = :effortRessenti, variabilite = :variabilite, variance = :variance, ecartType = :ecartType, moyenne = :moyenne, maximum = :maximum, minimum = :minimum, temperatureMoyenne = :temperatureMoyenne WHERE idActivite = :idActivite"; $params = [ - ':idActivite' => $oldActivite->getIdActivite(), - ':type' => $newActivite->getType(), - ':date' => $newActivite->getDate()->format('Y-m-d'), // Format date pour SQL - ':heureDebut' => $newActivite->getHeureDebut()->format('H:i:s'), // Format heure pour SQL - ':heureDeFin' => $newActivite->getHeureFin()->format('H:i:s'), // Format heure pour SQL - ':effortRessenti' => $newActivite->getEffortRessenti(), - ':variabilite' => $newActivite->getVariabilite(), - ':variance' => $newActivite->getVariance(), - ':ecartType' => $newActivite->getEcartType(), - ':moyenne' => $newActivite->getMoyenne(), - ':maximum' => $newActivite->getMaximum(), - ':minimum' => $newActivite->getMinimum(), - ':temperatureMoyenne' => $newActivite->getTemperatureMoyenne(), + ':idActivite' => $oldActivity->getIdActivity(), + ':type' => $newActivity->getType(), + ':date' => $newActivity->getDate()->format('Y-m-d'), // Format date pour SQL + ':heureDebut' => $newActivity->getHeureDebut()->format('H:i:s'), // Format heure pour SQL + ':heureDeFin' => $newActivity->getHeureFin()->format('H:i:s'), // Format heure pour SQL + ':effortRessenti' => $newActivity->getEffortRessenti(), + ':variabilite' => $newActivity->getVariabilite(), + ':variance' => $newActivity->getVariance(), + ':ecartType' => $newActivity->getEcartType(), + ':moyenne' => $newActivity->getMoyenne(), + ':maximum' => $newActivity->getMaximum(), + ':minimum' => $newActivity->getMinimum(), + ':temperatureMoyenne' => $newActivity->getTemperatureMoyenne(), ]; return $this->connection->executeWithErrorHandling($query, $params); } - public function deleteActivite(int $idActivite) { - $query = "DELETE FROM Activite WHERE idActivite = :idActivite"; + public function deleteActivity(int $idActivity) { + $query = "DELETE FROM Activite WHERE idActivite = :idActivity"; $params = [ - ':idActivite' => $idActivite, + ':idActivity' => $idActivity, ]; return $this->connection->executeWithErrorHandling($query, $params); diff --git a/Sources/src/data/core/database/ActiviteMapper.php b/Sources/src/data/core/database/ActiviteMapper.php index d2097e10..52e5a872 100644 --- a/Sources/src/data/core/database/ActiviteMapper.php +++ b/Sources/src/data/core/database/ActiviteMapper.php @@ -1,34 +1,81 @@ setIdActivite($data['idActivite']); - $activite->setType($data['type']); - $activite->setDate($data['date']); - $activite->setHeureDebut($data['heureDebut']); - $activite->setHeureFin($data['heureFin']); - $activite->setEffortRessenti($data['effortRessenti']); - $activite->setVariabilite($data['variabilite']); - $activite->setVariance($data['variance']); - $activite->setEcartType($data['ecartType']); - $activite->setMoyenne($data['moyenne']); - $activite->setMaximum($data['maximum']); - $activite->setMinimum($data['minimum']); - $activite->setTemperatureMoyenne($data['temperatureMoyenne']); - - return $activite; - } +use Model\Activity; + +class ActivityMapper { + public function activitySqlToEntity(array $data):array + { + $activityEntities = []; + + foreach ($data as $activityData) { + $activity = new ActivityEntity(); + + if (isset($activityData['idActivity'])) { + $activity->setIdActivity($data['idActivity']); + } + + if (isset($activityData['type'])) { + $activity->setType($data['type']); + } + + if (isset($activityData['date'])) { + $activity->setDate($data['date']); + } + + if (isset($activityData['heureDebut'])) { + $activity->setHeureDebut($data['heureDebut']); + } + + if (isset($activityData['heureFin'])) { + $activity->setHeureFin($data['heureFin']); + } + + if (isset($activityData['effortRessenti'])) { + $activity->setEffortRessenti($data['effortRessenti']); + } + + if (isset($activityData['variabilite'])) { + $activity->setVariabilite($data['variabilite']); + } + + if (isset($activityData['variance'])) { + $activity->setVariance($data['variance']); + } + + if (isset($activityData['ecartType'])) { + $activity->setEcartType($data['ecartType']); + } - //public function ActiviteEntityToModel(ActiviteEntity entity): Activite; + if (isset($activityData['moyenne'])) { + $activity->setMoyenne($data['moyenne']); + } - public function ActiviteEntityToModel(ActiviteEntity $activiteEntity):Activite{ + if (isset($activityData['maximum'])) { + $activity->setMaximum($data['maximum']); + } - $act = new Activite( - $activiteEntity->getIdActivite(), + if (isset($activityData['minimum'])) { + $activity->setMinimum($data['minimum']); + } + + if (isset($activityData['temperatureMoyenne'])) { + $activity->setTemperatureMoyenne($data['temperatureMoyenne']); + } + + $activityEntities[] = $activity; + } + + return $activityEntities; + } + + /** + * @throws \Exception + */ + public function ActivityEntityToModel(ActivityEntity $activiteEntity):Activity{ + + $act = new Activity( + $activiteEntity->getIdActivity(), $activiteEntity->getType(), $activiteEntity->getDate(), $activiteEntity->getHeureDebut(), @@ -45,7 +92,27 @@ class ActiviteMapper { return $act; } - //public function ActiviteToEntity(Activite model): ActiviteEntity; + //public function ActivityToEntity(Activity model): ActivityEntity; + + public function activityToEntity(Activity $act):ActivityEntity{ + + $act = new ActivityEntity(); + $act->setIdActivity($act->getIdActivity()()); + $act->setType($act->getType()); + $act->setDate($act->getDate()); + $act->setHeureDebut($act->getHeureDebut()); + $act->setHeureFin($act->getHeureFin()); + $act->setEffortRessenti($act->getEffortRessenti()); + $act->setVariabilite($act->getVariabilite()); + $act->setVariance($act->getVariance()); + $act->setEcartType($act->getEcartType()); + $act->setMoyenne($act->getMoyenne()); + $act->setMaximum($act->getMaximum()); + $act->setMinimum($act->getMinimum()); + $act->setTemperatureMoyenne($act->getTemperatureMoyenne()); + + return $act; + } } ?> diff --git a/Sources/src/data/core/database/ActiviteEntity.php b/Sources/src/data/core/database/ActivityEntity.php similarity index 94% rename from Sources/src/data/core/database/ActiviteEntity.php rename to Sources/src/data/core/database/ActivityEntity.php index d392751e..6eda351d 100644 --- a/Sources/src/data/core/database/ActiviteEntity.php +++ b/Sources/src/data/core/database/ActivityEntity.php @@ -2,8 +2,8 @@ namespace Database; -class ActiviteEntity { - private $idActivite; +class ActivityEntity { + private $idActivity; private $type; private $date; private $heureDebut; @@ -18,7 +18,7 @@ class ActiviteEntity { private $temperatureMoyenne; // Getters - public function getIdActivite() { + public function getIdActivity() { return $this->idActivity; } @@ -71,7 +71,7 @@ class ActiviteEntity { } // Setters - public function setIdActivite($idActivity) { + public function setIdActivity($idActivity) { $this->idActivity = $idActivity; } diff --git a/Sources/src/data/core/database/AthleteGateway.php b/Sources/src/data/core/database/AthleteGateway.php index 6684630b..35badb05 100644 --- a/Sources/src/data/core/database/AthleteGateway.php +++ b/Sources/src/data/core/database/AthleteGateway.php @@ -4,7 +4,7 @@ namespace Database; use \PDO; class AthleteGateway { - private $connection; + private Connexion $connection; public function __construct(Connexion $connection) { $this->connection = $connection;