Grosse Amélioration Gateways, Mappers, Entity Athlète et Coach
continuous-integration/drone/push Build is failing Details

WORK-KMO-Gateway
Kevin MONTEIRO 1 year ago
parent 09dafc90c0
commit af9821709c

@ -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;
}
}
?>

@ -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(),
@ -76,15 +105,18 @@ class AthleteGateway {
':poids' => $athlete->getPoids(),
':motDePasse' => $athlete->getMotDePasse(),
':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 = [
@ -97,12 +129,15 @@ class AthleteGateway {
':poids' => $newAthlete->getPoids(),
':motDePasse' => $newAthlete->getMotDePasse(),
':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 = [

@ -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;
}

@ -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;
}
}
?>

@ -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);

@ -8,34 +8,43 @@ use Model\Role;
use Model\Coach;
class CoachMapper {
public function map(array $data) {
public function coachSqlToEntity(array $data): array {
$coachEntities = [];
foreach ($data as $coachData) {
$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;
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;
}

@ -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