Merge remote-tracking branch 'origin/WORK-KMO-Gateway' into merged_PLE

WORK-APE
Paul LEVRAULT 1 year ago
commit ae9744cde6

@ -1,38 +1,39 @@
<?php <?php
namespace Database; namespace Database;
use \PDO;
class ActiviteGateway { class ActivityGateway {
private $connection; private Connexion $connection;
public function __construct(Connection $connection) { public function __construct(Connexion $connection) {
$this->connection = $connection; $this->connection = $connection;
} }
public function getActivite() { public function getActivity() {
$query = "SELECT * FROM Activite"; $query = "SELECT * FROM Activite";
return $this->connection->executeWithErrorHandling($query); return $this->connection->executeWithErrorHandling($query);
} }
public function getActiviteById(int $activiteId) { public function getActivityById(int $activityId) {
$query = "SELECT * FROM Activite WHERE idActivite = :id"; $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); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getActiviteByType(string $type) { public function getActivityByType(string $type) {
$query = "SELECT * FROM Activite WHERE type = :type"; $query = "SELECT * FROM Activite WHERE type = :type";
$params = [':type' => [$type, PDO::PARAM_STR]]; $params = [':type' => [$type, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getActiviteByDate(string $date) { public function getActivityByDate(string $date) {
$query = "SELECT * FROM Activite WHERE date = :date"; $query = "SELECT * FROM Activite WHERE date = :date";
$params = [':date' => [$date, PDO::PARAM_STR]]; $params = [':date' => [$date, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); 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"; $query = "SELECT * FROM Activite WHERE heureDebut >= :startTime AND heureFin <= :endTime";
$params = [ $params = [
':startTime' => [$startTime, PDO::PARAM_STR], ':startTime' => [$startTime, PDO::PARAM_STR],
@ -41,76 +42,76 @@ class ActiviteGateway {
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getActiviteByEffort(int $effortRessenti) { public function getActivityByEffort(int $effortRessenti) {
$query = "SELECT * FROM Activite WHERE effortRessenti = :effort"; $query = "SELECT * FROM Activite WHERE effortRessenti = :effort";
$params = [':effort' => [$effortRessenti, PDO::PARAM_INT]]; $params = [':effort' => [$effortRessenti, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getActiviteByVariability(int $variabilite) { public function getActivityByVariability(int $variabilite) {
$query = "SELECT * FROM Activite WHERE variabilite = :variability"; $query = "SELECT * FROM Activite WHERE variabilite = :variability";
$params = [':variability' => [$variabilite, PDO::PARAM_INT]]; $params = [':variability' => [$variabilite, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getActiviteByTemperature(int $temperatureMoyenne) { public function getActivityByTemperature(int $temperatureMoyenne) {
$query = "SELECT * FROM Activite WHERE temperatureMoyenne = :temperature"; $query = "SELECT * FROM Activite WHERE temperatureMoyenne = :temperature";
$params = [':temperature' => [$temperatureMoyenne, PDO::PARAM_INT]]; $params = [':temperature' => [$temperatureMoyenne, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); 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) $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)"; VALUES (:type, :date, :heureDebut, :heureDeFin, :effortRessenti, :variabilite, :variance, :ecartType, :moyenne, :maximum, :minimum, :temperatureMoyenne)";
$params = [ $params = [
':type' => $activite->getType(), ':type' => $activity->getType(),
':date' => $activite->getDate()->format('Y-m-d'), // Format date pour SQL ':date' => $activity->getDate()->format('Y-m-d'), // Format date pour SQL
':heureDebut' => $activite->getHeureDebut()->format('H:i:s'), // Format heure pour SQL ':heureDebut' => $activity->getHeureDebut()->format('H:i:s'), // Format heure pour SQL
':heureDeFin' => $activite->getHeureFin()->format('H:i:s'), // Format heure pour SQL ':heureDeFin' => $activity->getHeureFin()->format('H:i:s'), // Format heure pour SQL
':effortRessenti' => $activite->getEffortRessenti(), ':effortRessenti' => $activity->getEffortRessenti(),
':variabilite' => $activite->getVariabilite(), ':variabilite' => $activity->getVariabilite(),
':variance' => $activite->getVariance(), ':variance' => $activity->getVariance(),
':ecartType' => $activite->getEcartType(), ':ecartType' => $activity->getEcartType(),
':moyenne' => $activite->getMoyenne(), ':moyenne' => $activity->getMoyenne(),
':maximum' => $activite->getMaximum(), ':maximum' => $activity->getMaximum(),
':minimum' => $activite->getMinimum(), ':minimum' => $activity->getMinimum(),
':temperatureMoyenne' => $activite->getTemperatureMoyenne(), ':temperatureMoyenne' => $activity->getTemperatureMoyenne(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function updateActivite(ActiviteEntity $oldActivite, ActiviteEntity $newActivite) { public function updateActivity(ActivityEntity $oldActivity, ActivityEntity $newActivity) {
$query = "UPDATE Activite $query = "UPDATE Activite
SET type = :type, date = :date, heureDebut = :heureDebut, heureDeFin = :heureDeFin, 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 effortRessenti = :effortRessenti, variabilite = :variabilite, variance = :variance, ecartType = :ecartType, moyenne = :moyenne, maximum = :maximum, minimum = :minimum, temperatureMoyenne = :temperatureMoyenne
WHERE idActivite = :idActivite"; WHERE idActivite = :idActivite";
$params = [ $params = [
':idActivite' => $oldActivite->getIdActivite(), ':idActivite' => $oldActivity->getIdActivity(),
':type' => $newActivite->getType(), ':type' => $newActivity->getType(),
':date' => $newActivite->getDate()->format('Y-m-d'), // Format date pour SQL ':date' => $newActivity->getDate()->format('Y-m-d'), // Format date pour SQL
':heureDebut' => $newActivite->getHeureDebut()->format('H:i:s'), // Format heure pour SQL ':heureDebut' => $newActivity->getHeureDebut()->format('H:i:s'), // Format heure pour SQL
':heureDeFin' => $newActivite->getHeureFin()->format('H:i:s'), // Format heure pour SQL ':heureDeFin' => $newActivity->getHeureFin()->format('H:i:s'), // Format heure pour SQL
':effortRessenti' => $newActivite->getEffortRessenti(), ':effortRessenti' => $newActivity->getEffortRessenti(),
':variabilite' => $newActivite->getVariabilite(), ':variabilite' => $newActivity->getVariabilite(),
':variance' => $newActivite->getVariance(), ':variance' => $newActivity->getVariance(),
':ecartType' => $newActivite->getEcartType(), ':ecartType' => $newActivity->getEcartType(),
':moyenne' => $newActivite->getMoyenne(), ':moyenne' => $newActivity->getMoyenne(),
':maximum' => $newActivite->getMaximum(), ':maximum' => $newActivity->getMaximum(),
':minimum' => $newActivite->getMinimum(), ':minimum' => $newActivity->getMinimum(),
':temperatureMoyenne' => $newActivite->getTemperatureMoyenne(), ':temperatureMoyenne' => $newActivity->getTemperatureMoyenne(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function deleteActivite(int $idActivite) { public function deleteActivity(int $idActivity) {
$query = "DELETE FROM Activite WHERE idActivite = :idActivite"; $query = "DELETE FROM Activite WHERE idActivite = :idActivity";
$params = [ $params = [
':idActivite' => $idActivite, ':idActivity' => $idActivity,
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);

@ -1,34 +1,81 @@
<?php <?php
namespace Database; namespace Database;
use Model\Activite; use Model\Activity;
class ActiviteMapper { class ActivityMapper {
public function map(array $data):ActiviteEntity { public function activitySqlToEntity(array $data):array
$activite = new ActiviteEntity(); {
$activite->setIdActivite($data['idActivite']); $activityEntities = [];
$activite->setType($data['type']);
$activite->setDate($data['date']); foreach ($data as $activityData) {
$activite->setHeureDebut($data['heureDebut']); $activity = new ActivityEntity();
$activite->setHeureFin($data['heureFin']);
$activite->setEffortRessenti($data['effortRessenti']); if (isset($activityData['idActivity'])) {
$activite->setVariabilite($data['variabilite']); $activity->setIdActivity($data['idActivity']);
$activite->setVariance($data['variance']); }
$activite->setEcartType($data['ecartType']);
$activite->setMoyenne($data['moyenne']); if (isset($activityData['type'])) {
$activite->setMaximum($data['maximum']); $activity->setType($data['type']);
$activite->setMinimum($data['minimum']); }
$activite->setTemperatureMoyenne($data['temperatureMoyenne']);
if (isset($activityData['date'])) {
return $activite; $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( if (isset($activityData['minimum'])) {
$activiteEntity->getIdActivite(), $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->getType(),
$activiteEntity->getDate(), $activiteEntity->getDate(),
$activiteEntity->getHeureDebut(), $activiteEntity->getHeureDebut(),
@ -45,7 +92,27 @@ class ActiviteMapper {
return $act; 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;
}
} }
?> ?>

@ -2,8 +2,8 @@
namespace Database; namespace Database;
class ActiviteEntity { class ActivityEntity {
private $idActivite; private $idActivity;
private $type; private $type;
private $date; private $date;
private $heureDebut; private $heureDebut;
@ -18,7 +18,7 @@ class ActiviteEntity {
private $temperatureMoyenne; private $temperatureMoyenne;
// Getters // Getters
public function getIdActivite() { public function getIdActivity() {
return $this->idActivity; return $this->idActivity;
} }
@ -71,7 +71,7 @@ class ActiviteEntity {
} }
// Setters // Setters
public function setIdActivite($idActivity) { public function setIdActivity($idActivity) {
$this->idActivity = $idActivity; $this->idActivity = $idActivity;
} }

@ -12,6 +12,8 @@ class AthleteEntity {
private $poids; private $poids;
private $motDePasse; private $motDePasse;
private $dateNaissance; private $dateNaissance;
private $isCoach;
private $coachId;
// Getters // Getters
public function getIdAthlete() { public function getIdAthlete() {
@ -50,6 +52,14 @@ class AthleteEntity {
return $this->dateNaissance; return $this->dateNaissance;
} }
public function getIsCoach(){
return $this->isCoach;
}
public function getCoachId(){
return $this->coachId;
}
// Setters // Setters
public function setIdAthlete($idAthlete) { public function setIdAthlete($idAthlete) {
$this->idAthlete = $idAthlete; $this->idAthlete = $idAthlete;
@ -86,6 +96,14 @@ class AthleteEntity {
public function setDateNaissance($dateNaissance) { public function setDateNaissance($dateNaissance) {
$this->dateNaissance = $dateNaissance; $this->dateNaissance = $dateNaissance;
} }
public function setIsCoach($isCoach){
$this->isCoach = $isCoach;
}
public function setCoachId($coachId){
$this->coachId = $coachId;
}
} }
?> ?>

@ -4,68 +4,97 @@ namespace Database;
use \PDO; use \PDO;
class AthleteGateway { class AthleteGateway {
private $connection; private Connexion $connection;
public function __construct(Connexion $connection) { public function __construct(Connexion $connection) {
$this->connection = $connection; $this->connection = $connection;
} }
public function getAthlete() { public function getAthlete(): array
{
$query = "SELECT * FROM Athlete"; $query = "SELECT * FROM Athlete";
return $this->connection->executeWithErrorHandling($query); return $this->connection->executeWithErrorHandling($query);
} }
public function getAthleteById(int $userId) { public function getAthleteById(int $userId): array
{
$query = "SELECT * FROM Athlete WHERE idAthlete = :id"; $query = "SELECT * FROM Athlete WHERE idAthlete = :id";
$params = [':id' => [$userId, PDO::PARAM_INT]]; $params = [':id' => [$userId, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByName(string $name) { public function getAthleteByName(string $name): array
{
$query = "SELECT * FROM Athlete WHERE nom = :name"; $query = "SELECT * FROM Athlete WHERE nom = :name";
$params = [':name' => [$name, PDO::PARAM_STR]]; $params = [':name' => [$name, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByFirstName(string $firstName) { public function getAthleteByFirstName(string $firstName): array
{
$query = "SELECT * FROM Athlete WHERE prenom = :firstName"; $query = "SELECT * FROM Athlete WHERE prenom = :firstName";
$params = [':firstName' => [$firstName, PDO::PARAM_STR]]; $params = [':firstName' => [$firstName, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByEmail(string $email) { public function getAthleteByEmail(string $email): array
{
$query = "SELECT * FROM Athlete WHERE email = :email"; $query = "SELECT * FROM Athlete WHERE email = :email";
$params = [':email' => [$email, PDO::PARAM_STR]]; $params = [':email' => [$email, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByGender(string $gender) { public function getAthleteByGender(string $gender): array
{
$query = "SELECT * FROM Athlete WHERE sexe = :gender"; $query = "SELECT * FROM Athlete WHERE sexe = :gender";
$params = [':gender' => [$gender, PDO::PARAM_STR]]; $params = [':gender' => [$gender, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByHeight(int $height) { public function getAthleteByHeight(int $height): array
{
$query = "SELECT * FROM Athlete WHERE taille = :height"; $query = "SELECT * FROM Athlete WHERE taille = :height";
$params = [':height' => [$height, PDO::PARAM_INT]]; $params = [':height' => [$height, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByWeight(int $weight) { public function getAthleteByWeight(int $weight): array
{
$query = "SELECT * FROM Athlete WHERE poids = :weight"; $query = "SELECT * FROM Athlete WHERE poids = :weight";
$params = [':weight' => [$weight, PDO::PARAM_INT]]; $params = [':weight' => [$weight, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getAthleteByBirthDate(string $birthdate) { public function getAthleteByBirthDate(string $birthdate): array
{
$query = "SELECT * FROM Athlete WHERE dateNaissance = :birthdate"; $query = "SELECT * FROM Athlete WHERE dateNaissance = :birthdate";
$params = [':birthdate' => [$birthdate, PDO::PARAM_STR]]; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function addAthlete(AthleteEntity $athlete) { public function getAthleteByIsCoachTrue(): array
$query = "INSERT INTO Athlete (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance) {
VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance)"; $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 = [ $params = [
':nom' => $athlete->getNom(), ':nom' => $athlete->getNom(),
@ -76,15 +105,18 @@ class AthleteGateway {
':poids' => $athlete->getPoids(), ':poids' => $athlete->getPoids(),
':motDePasse' => $athlete->getMotDePasse(), ':motDePasse' => $athlete->getMotDePasse(),
':dateNaissance' => $athlete->getDateNaissance(), ':dateNaissance' => $athlete->getDateNaissance(),
':isCoach' => $athlete->getIsCoach(),
':coachId' => $athlete->getCoachId(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function updateAthlete(AthleteEntity $oldAthlete, AthleteEntity $newAthlete) { public function updateAthlete(AthleteEntity $oldAthlete, AthleteEntity $newAthlete): array
{
$query = "UPDATE Athlete $query = "UPDATE Athlete
SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe, 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"; WHERE idAthlete = :idAthlete";
$params = [ $params = [
@ -97,12 +129,15 @@ class AthleteGateway {
':poids' => $newAthlete->getPoids(), ':poids' => $newAthlete->getPoids(),
':motDePasse' => $newAthlete->getMotDePasse(), ':motDePasse' => $newAthlete->getMotDePasse(),
':dateNaissance' => $newAthlete->getDateNaissance(), ':dateNaissance' => $newAthlete->getDateNaissance(),
':isCoach' => $athlete->getIsCoach(),
':coachId' => $athlete->getCoachId(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function deleteAthlete(int $idAthlete) { public function deleteAthlete(int $idAthlete): array
{
$query = "DELETE FROM Athlete WHERE idAthlete = :idAthlete"; $query = "DELETE FROM Athlete WHERE idAthlete = :idAthlete";
$params = [ $params = [

@ -8,7 +8,7 @@ use Model\Role;
use Model\Athlete; use Model\Athlete;
class AthleteMapper { class AthleteMapper {
public function fromSqlToEntity(array $data): array { public function athleteSqlToEntity(array $data): array {
$athleteEntities = []; $athleteEntities = [];
foreach ($data as $athleteData) { foreach ($data as $athleteData) {
@ -49,6 +49,12 @@ class AthleteMapper {
if (isset($athleteData['dateNaissance'])) { if (isset($athleteData['dateNaissance'])) {
$athlete->setDateNaissance($athleteData['dateNaissance']); $athlete->setDateNaissance($athleteData['dateNaissance']);
} }
if (isset($athleteData['isCoach'])) {
$athlete->setIsCoach($athleteData['isCoach']);
}
if (isset($athleteData['coachId'])) {
$athlete->setCoachId($athleteData['coachId']);
}
$athleteEntities[] = $athlete; $athleteEntities[] = $athlete;
} }
@ -59,8 +65,8 @@ class AthleteMapper {
public function athleteEntityToModel(AthleteEntity $athleteEntity): User { public function athleteEntityToModel(AthleteEntity $athleteEntity): User {
$role = new Athlete(); // Utilisez la classe Athlete $role = new Athlete(); // Utilisez la classe Athlete
$dateSpecifique = $athleteEntity->getDateNaissance(); $dateSpecific = $athleteEntity->getDateNaissance();
$date = new DateTime($dateSpecifique); $date = new DateTime($dateSpecific);
$user = new User( $user = new User(
$athleteEntity->getIdAthlete(), $athleteEntity->getIdAthlete(),
@ -71,6 +77,7 @@ class AthleteMapper {
$athleteEntity->getSexe(), $athleteEntity->getSexe(),
$athleteEntity->getTaille(), $athleteEntity->getTaille(),
$athleteEntity->getPoids(), $athleteEntity->getPoids(),
$athleteEntity->getDateNaissance(),
$date, $date,
$role $role
); );
@ -90,6 +97,8 @@ class AthleteMapper {
$ath->setPoids($user->getPoids()); $ath->setPoids($user->getPoids());
$ath->setMotDePasse($user->getMotDePasse()); $ath->setMotDePasse($user->getMotDePasse());
$ath->setDateNaissance($user->getDateNaissance()); $ath->setDateNaissance($user->getDateNaissance());
$ath->setIsCoach(FALSE);
$ath->setCoachId(NULL);
return $ath; return $ath;
} }

@ -4,50 +4,15 @@ namespace Database;
class CoachEntity { class CoachEntity {
private $idCoach; private $idCoach;
private $nom; private $athleteId;
private $prenom;
private $email;
private $sexe;
private $taille;
private $poids;
private $motDePasse;
private $dateNaissance;
// Getters // Getters
public function getIdCoach() { public function getIdCoach() {
return $this->idCoach; return $this->idCoach;
} }
public function getNom() { public function getAthleteId() {
return $this->nom; return $this->athleteId;
}
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;
} }
// Setters // Setters
@ -55,37 +20,10 @@ class CoachEntity {
$this->idCoach = $idCoach; $this->idCoach = $idCoach;
} }
public function setNom($nom) { public function setAthleteId($athleteId) {
$this->nom = $nom; $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;
}
} }
?> ?>

@ -10,93 +10,96 @@ class CoachGateway {
$this->connection = $connection; $this->connection = $connection;
} }
public function getCoach() { public function getCoach(): array
{
$query = "SELECT * FROM Coach"; $query = "SELECT * FROM Coach";
return $this->connection->executeWithErrorHandling($query); return $this->connection->executeWithErrorHandling($query);
} }
public function getCoachById(int $userId) { public function getCoachById(int $userId): array
{
$query = "SELECT * FROM Coach WHERE idCoach = :id"; $query = "SELECT * FROM Coach WHERE idCoach = :id";
$params = [':id' => [$userId, PDO::PARAM_INT]]; $params = [':id' => [$userId, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getCoachByName(string $name) { public function getAthleteByCoachId(int $coachId): array
$query = "SELECT * FROM Coach WHERE nom = :name"; {
$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]]; $params = [':name' => [$name, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getCoachByFirstName(string $firstName) { public function getCoachByFirstName(string $firstName): array
$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]]; $params = [':firstName' => [$firstName, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getCoachByEmail(string $email) { public function getCoachByEmail(string $email): array
$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]]; $params = [':email' => [$email, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getCoachByGender(string $gender) { public function getCoachByGender(string $gender): array
$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]]; $params = [':gender' => [$gender, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getCoachByHeight(int $height) { public function getCoachByHeight(int $height): array
$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]]; $params = [':height' => [$height, PDO::PARAM_INT]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function getCoachByBirthDate(string $birthdate) { public function getCoachByBirthDate(string $birthdate): array
$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]]; $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function addCoach(CoachEntity $coach) { public function addCoach(CoachEntity $coach): array
$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 = [ $params = [
':nom' => $coach->getNom(), ':athleteId' => $coach->getAthleteId(),
':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); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach) { public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach): array
{
$query = "UPDATE Coach $query = "UPDATE Coach
SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe, SET athleteId = :athleteId
taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance
WHERE idCoach = :idCoach"; WHERE idCoach = :idCoach";
$params = [ $params = [
':idCoach' => $oldCoach->getIdCoach(), ':idCoach' => $oldCoach->getIdCoach(),
':nom' => $newCoach->getNom(), ':athleteId' => $newCoach->getAthleteId(),
':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); return $this->connection->executeWithErrorHandling($query, $params);
} }
public function deleteCoach(int $idCoach) { public function deleteCoach(int $idCoach): array
{
$query = "DELETE FROM Coach WHERE idCoach = :idCoach"; $query = "DELETE FROM Coach WHERE idCoach = :idCoach";
$params = [ $params = [

@ -1,6 +1,7 @@
<?php <?php
namespace Database; namespace Database;
use Model\CoachAthlete;
use Model\User; use Model\User;
use \PDO; use \PDO;
use \DateTime; use \DateTime;
@ -8,34 +9,48 @@ use Model\Role;
use Model\Coach; use Model\Coach;
class CoachMapper { class CoachMapper {
public function map(array $data) { public function coachSqlToEntity(array $data): array {
$coach = new CoachEntity(); $coachEntities = [];
$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; foreach ($data as $coachData) {
$coach = new CoachEntity();
if (isset($coachData['idCoach'])) {
$coach->setIdCoach($coachData['idCoach']);
}
if (isset($coachData['athleteId'])) {
$coach->setAthleteId($coachData['athleteId']);
}
$coachEntities[] = $coach;
}
return $coachEntities;
} }
public function CoachEntityToModel(CoachEntity $coachEntity):User{ public function CoachEntityToModel(CoachEntity $coachEntity):User{
$role = "Coach"; $role = new CoachAthlete();
$idCoach = $coachEntity->getIdCoach();
$ath = getAthleteByCoachId($idCoach);
$athlete = athleteSqlToEntity($ath);
$dateSpecific = $athlete->getDateNaissance();
$date = new DateTime($dateSpecific);
$user = new User( $user = new User(
$coachEntity->getIdCoach(), $coachEntity->getIdCoach(),
$coachEntity->getNom(), $athlete->getNom(),
$coachEntity->getPrenom(), $athlete->getPrenom(),
$coachEntity->getEmail(), $athlete->getEmail(),
$coachEntity->getMotDePasse(), $athlete->getMotDePasse(),
$coachEntity->getSexe(), $athlete->getSexe(),
$coachEntity->getTaille(), $athlete->getTaille(),
$coachEntity->getPoids(), $athlete->getPoids(),
$coachEntity->getDateNaissance(), $athlete->getDateNaissance(),
$date,
$role $role
); );
@ -46,17 +61,10 @@ class CoachMapper {
$coach = new CoachEntity(); $coach = new CoachEntity();
$coach->setIdCoach($user->getId()); $coach->setIdCoach($user->getId());
$coach->setNom($user->getNom()); $coach->setAthleteId($user->getId());
$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());
return $coach; return $coach;
} }
} }
?> ?>

@ -0,0 +1,217 @@
<?php
use PHPUnit\Framework\TestCase;
//use Database\{Connexion, AthleteGateway,AthleteEntity};
use Database\AthleteEntity;
use Database\AthleteGateway;
use Database\Connexion;
use Database\AthleteMapper;
use Database\CoachGateway;
use Database\CoachEntity;
use Database\CoachMapper;
class GatewayTest extends TestCase {
//Partie concernant les Athlètes
public function testGetAthlete() {
//$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);
$athleteGateway = new AthleteGateway($connexion);
$result = $athleteGateway->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);
}*/
}

@ -0,0 +1,43 @@
<?php
use PHPUnit\Framework\TestCase;
use Model\User;
use Database\AthleteEntity;
use Database\AthleteGateway;
use Database\Connexion;
use Database\AthleteMapper;
class MapperTest extends TestCase {
public function testMapperAthlete() {
//$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);
$athleteGateway = new AthleteGateway($connexion);
$result = $athleteGateway->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());
}
}
}
Loading…
Cancel
Save