From 7af4e472ec21106bb304857677046756d97c142e Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Mon, 18 Dec 2023 08:46:44 +0100 Subject: [PATCH] Modif Gateway --- .../data/core/database/ActiviteGateway.php | 33 +++-- .../src/data/core/database/AthleteGateway.php | 30 +++-- .../src/data/core/database/CoachEntity.php | 93 ++++++++++++-- .../src/data/core/database/CoachGateway.php | 120 ++++++++++-------- .../src/data/core/database/CoachMapper.php | 89 +++++++++---- 5 files changed, 249 insertions(+), 116 deletions(-) diff --git a/Sources/src/data/core/database/ActiviteGateway.php b/Sources/src/data/core/database/ActiviteGateway.php index ed6db38c..f8d80ab5 100644 --- a/Sources/src/data/core/database/ActiviteGateway.php +++ b/Sources/src/data/core/database/ActiviteGateway.php @@ -12,25 +12,29 @@ class ActivityGateway { public function getActivity() { $query = "SELECT * FROM Activite"; - return $this->connection->executeWithErrorHandling($query); + $res = $this->connection->executeWithErrorHandling($query); + return $res; } public function getActivityById(int $activityId) { $query = "SELECT * FROM Activite WHERE idActivite = :id"; $params = [':id' => [$activityId, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getActivityByType(string $type) { $query = "SELECT * FROM Activite WHERE type = :type"; $params = [':type' => [$type, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getActivityByDate(string $date) { $query = "SELECT * FROM Activite WHERE date = :date"; $params = [':date' => [$date, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getActivityByTimeRange(string $startTime, string $endTime) { @@ -39,25 +43,29 @@ class ActivityGateway { ':startTime' => [$startTime, PDO::PARAM_STR], ':endTime' => [$endTime, PDO::PARAM_STR] ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getActivityByEffort(int $effortRessenti) { $query = "SELECT * FROM Activite WHERE effortRessenti = :effort"; $params = [':effort' => [$effortRessenti, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getActivityByVariability(int $variabilite) { $query = "SELECT * FROM Activite WHERE variabilite = :variability"; $params = [':variability' => [$variabilite, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getActivityByTemperature(int $temperatureMoyenne) { $query = "SELECT * FROM Activite WHERE temperatureMoyenne = :temperature"; $params = [':temperature' => [$temperatureMoyenne, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function addActivity(ActivityEntity $activity) { @@ -79,7 +87,8 @@ class ActivityGateway { ':temperatureMoyenne' => $activity->getTemperatureMoyenne(), ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function updateActivity(ActivityEntity $oldActivity, ActivityEntity $newActivity) { @@ -104,7 +113,8 @@ class ActivityGateway { ':temperatureMoyenne' => $newActivity->getTemperatureMoyenne(), ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function deleteActivity(int $idActivity) { @@ -114,7 +124,8 @@ class ActivityGateway { ':idActivity' => $idActivity, ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } } diff --git a/Sources/src/data/core/database/AthleteGateway.php b/Sources/src/data/core/database/AthleteGateway.php index 8bada5e3..dd880af4 100644 --- a/Sources/src/data/core/database/AthleteGateway.php +++ b/Sources/src/data/core/database/AthleteGateway.php @@ -30,49 +30,56 @@ class AthleteGateway { { $query = "SELECT * FROM Athlete WHERE nom = :name AND isCoach=FALSE"; $params = [':name' => [$name, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getAthleteByFirstName(string $firstName): array { $query = "SELECT * FROM Athlete WHERE prenom = :firstName AND isCoach=FALSE"; $params = [':firstName' => [$firstName, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getAthleteByEmail(string $email): array { $query = "SELECT * FROM Athlete WHERE email = :email AND isCoach=FALSE"; $params = [':email' => [$email, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getAthleteByGender(string $gender): array { $query = "SELECT * FROM Athlete WHERE sexe = :gender AND isCoach=FALSE"; $params = [':gender' => [$gender, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getAthleteByHeight(int $height): array { $query = "SELECT * FROM Athlete WHERE taille = :height AND isCoach=FALSE"; $params = [':height' => [$height, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getAthleteByWeight(int $weight): array { $query = "SELECT * FROM Athlete WHERE poids = :weight AND isCoach=FALSE"; $params = [':weight' => [$weight, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function getAthleteByBirthDate(string $birthdate): array { $query = "SELECT * FROM Athlete WHERE dateNaissance = :birthdate AND isCoach=FALSE"; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function addAthlete(AthleteEntity $athlete): array @@ -92,7 +99,8 @@ class AthleteGateway { ':isCoach' => $athlete->getIsCoach(), ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function updateAthlete(AthleteEntity $oldAthlete, AthleteEntity $newAthlete): array @@ -115,7 +123,8 @@ class AthleteGateway { ':isCoach' => $newAthlete->getIsCoach(), ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } public function deleteAthlete(int $idAthlete): array @@ -126,7 +135,8 @@ class AthleteGateway { ':idAthlete' => $idAthlete, ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } diff --git a/Sources/src/data/core/database/CoachEntity.php b/Sources/src/data/core/database/CoachEntity.php index a1648402..ea59cf85 100644 --- a/Sources/src/data/core/database/CoachEntity.php +++ b/Sources/src/data/core/database/CoachEntity.php @@ -3,27 +3,98 @@ namespace Database; class CoachEntity { - private $idCoach; - private $athleteId; + private $idAthlete; + private $nom; + private $prenom; + private $email; + private $sexe; + private $taille; + private $poids; + private $motDePasse; + private $dateNaissance; + private $isCoach; // Getters - public function getIdCoach() { - return $this->idCoach; + public function getIdAthlete() { + return $this->idAthlete; } - public function getAthleteId() { - return $this->athleteId; + 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 getIsCoach(){ + return $this->isCoach; } // Setters - public function setIdCoach($idCoach) { - $this->idCoach = $idCoach; + public function setIdAthlete($idAthlete) { + $this->idAthlete = $idAthlete; } - public function setAthleteId($athleteId) { - $this->athleteId = $athleteId; + public function setNom($nom) { + $this->nom = $nom; } + 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; + } + + public function setIsCoach($isCoach){ + $this->isCoach = $isCoach; + } } -?> +?> \ No newline at end of file diff --git a/Sources/src/data/core/database/CoachGateway.php b/Sources/src/data/core/database/CoachGateway.php index 63fc9875..cd1f3fa2 100644 --- a/Sources/src/data/core/database/CoachGateway.php +++ b/Sources/src/data/core/database/CoachGateway.php @@ -10,103 +10,111 @@ class CoachGateway { $this->connection = $connection; } - public function getCoach(): array - { - $query = "SELECT * FROM Coach"; - return $this->connection->executeWithErrorHandling($query); + public function getCoach() { + $query = "SELECT * FROM Athlete WHERE isCoach = TRUE;"; + $res = $this->connection->executeWithErrorHandling($query); + return $res; } - public function getCoachById(int $userId): array - { - $query = "SELECT * FROM Coach WHERE idCoach = :id"; + public function getCoachById(int $userId) { + $query = "SELECT * FROM Athlete WHERE idAthlete = :id AND isCoach = TRUE"; $params = [':id' => [$userId, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - 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"; + public function getCoachByName(string $name) { + $query = "SELECT * FROM Athlete WHERE nom = :name AND isCoach = TRUE"; $params = [':name' => [$name, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function getCoachByFirstName(string $firstName): array - { - $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.prenom = :firstName"; + public function getCoachByFirstName(string $firstName) { + $query = "SELECT * FROM Athlete WHERE prenom = :firstName AND isCoach = TRUE"; $params = [':firstName' => [$firstName, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function getCoachByEmail(string $email): array - { - $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.email = :email"; + public function getCoachByEmail(string $email) { + $query = "SELECT * FROM Athlete WHERE email = :email AND isCoach = TRUE"; $params = [':email' => [$email, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function getCoachByGender(string $gender): array - { - $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.sexe = :gender"; + public function getCoachByGender(string $gender) { + $query = "SELECT * FROM Athlete WHERE sexe = :gender AND isCoach = TRUE"; $params = [':gender' => [$gender, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function getCoachByHeight(int $height): array - { - $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.taille = :height"; + public function getCoachByHeight(int $height) { + $query = "SELECT * FROM Athlete WHERE taille = :height AND isCoach = TRUE"; $params = [':height' => [$height, PDO::PARAM_INT]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function getCoachByBirthDate(string $birthdate): array - { - $query = "SELECT * FROM Coach c, Athlete a WHERE c.athleteId = a.idAthlete AND a.dateNaissance = :birthdate"; + public function getCoachByBirthDate(string $birthdate) { + $query = "SELECT * FROM Athlete WHERE dateNaissance = :birthdate AND isCoach = TRUE"; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function addCoach(CoachEntity $coach): array - { - $query = "INSERT INTO Coach (athleteId) - VALUES (:athleteId)"; + public function addCoach(CoachEntity $coach) { + $query = "INSERT INTO Athlete (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance, isCoach) + VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance, TRUE)"; $params = [ - ':athleteId' => $coach->getAthleteId(), + ':nom' => $coach->getNom(), + ':prenom' => $coach->getPrenom(), + ':email' => $coach->getEmail(), + ':sexe' => $coach->getSexe(), + ':taille' => $coach->getTaille(), + ':poids' => $coach->getPoids(), + ':motDePasse' => $coach->getMotDePasse(), + ':dateNaissance' => $coach->getDateNaissance(), ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach): array - { - $query = "UPDATE Coach - SET athleteId = :athleteId + public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach) { + $query = "UPDATE Athlete + SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe, + taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance, isCoach = TRUE WHERE idCoach = :idCoach"; $params = [ - ':idCoach' => $oldCoach->getIdCoach(), - ':athleteId' => $newCoach->getAthleteId(), + ':idAthlete' => $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(), ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } - public function deleteCoach(int $idCoach): array - { - $query = "DELETE FROM Coach WHERE idCoach = :idCoach"; + public function deleteCoach(int $idCoach) { + $query = "DELETE FROM Athlete WHERE idAthlete = :idCoach"; $params = [ ':idCoach' => $idCoach, ]; - return $this->connection->executeWithErrorHandling($query, $params); + $res = $this->connection->executeWithErrorHandling($query, $params); + return $res; } } \ No newline at end of file diff --git a/Sources/src/data/core/database/CoachMapper.php b/Sources/src/data/core/database/CoachMapper.php index f3fd48ec..8c0d9e64 100644 --- a/Sources/src/data/core/database/CoachMapper.php +++ b/Sources/src/data/core/database/CoachMapper.php @@ -1,12 +1,12 @@ setIdAthlete($coachData['idathlete']); + } - if (isset($coachData['idCoach'])) { - $coach->setIdCoach($coachData['idCoach']); + if (isset($coachData['nom'])) { + $coach->setNom($coachData['nom']); } - if (isset($coachData['athleteId'])) { - $coach->setAthleteId($coachData['athleteId']); + if (isset($coachData['prenom'])) { + $coach->setPrenom($coachData['prenom']); } - $coachEntities[] = $coach; - } + if (isset($coachData['email'])) { + $coach->setEmail($coachData['email']); + } - return $coachEntities; - } + if (isset($coachData['sexe'])) { + $coach->setSexe($coachData['sexe']); + } + + if (isset($coachData['taille'])) { + $coach->setTaille($coachData['taille']); + } + + if (isset($coachData['poids'])) { + $coach->setPoids($coachData['poids']); + } - public function CoachEntityToModel(CoachEntity $coachEntity):User{ - $role = new CoachAthlete(); + if (isset($coachData['motdepasse'])) { + $coach->setMotDePasse($coachData['motdepasse']); + } - $idCoach = $coachEntity->getIdCoach(); + if (isset($coachData['datenaissance'])) { + $coach->setDateNaissance($coachData['datenaissance']); + } - $ath = getAthleteByCoachId($idCoach); - $athlete = athleteSqlToEntity($ath); + if (isset($coachData['iscoach'])) { + $coach->setIsCoach($coachData['iscoach']); + } - $dateSpecific = $athlete->getDateNaissance(); - $date = new DateTime($dateSpecific); + $coachEntities[] = $coach; + } + return $coachEntities; + } + + public function coachEntityToModel(CoachEntity $coachEntity): User { + $role = new Coach(); + $date = new DateTime($coachEntity->getDateNaissance()); $user = new User( - $coachEntity->getIdCoach(), - $athlete->getNom(), - $athlete->getPrenom(), - $athlete->getEmail(), - $athlete->getMotDePasse(), - $athlete->getSexe(), - $athlete->getTaille(), - $athlete->getPoids(), - $athlete->getDateNaissance(), + $coachEntity->getIdAthlete(), + $coachEntity->getNom(), + $coachEntity->getPrenom(), + "myUsername", + $coachEntity->getEmail(), + $coachEntity->getMotDePasse(), + $coachEntity->getSexe(), + $coachEntity->getTaille(), + $coachEntity->getPoids(), $date, $role ); @@ -57,14 +80,24 @@ class CoachMapper { return $user; } - public function CoachToEntity(User $user):CoachEntity{ + public function coachToEntity(User $user):CoachEntity{ $coach = new CoachEntity(); - $coach->setIdCoach($user->getId()); - $coach->setAthleteId($user->getId()); + $coach->setIdAthlete($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->setIsCoach(TRUE); return $coach; } + } + ?>