add database on athlete page

merging_APE
Antoine PEREDERII 1 year ago
parent 8473b52a96
commit 6e935ce864

@ -12,10 +12,11 @@ $dotenv->safeLoad();
// const DB_PASSWORD = $_ENV['DB_PASSWORD'] ?? 'achanger'; // const DB_PASSWORD = $_ENV['DB_PASSWORD'] ?? 'achanger';
define("APP_ENV", 'development'); define("APP_ENV", 'development');
const DB_SERVER = 'pgsql';
const DB_HOST = 'localhost'; const DB_HOST = 'localhost';
const DB_DATABASE = 'heartTrack'; const DB_DATABASE = 'sae_3';
const DB_USER = 'toto'; const DB_USER = 'Perederii';
const DB_PASSWORD = 'achanger'; const DB_PASSWORD = '';
//const APP_ENV = 'console'; //const APP_ENV = 'console';
const DSN = "mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE; const DSN = DB_SERVER . ":host=" . DB_HOST . ";dbname=" . DB_DATABASE;

@ -2,6 +2,7 @@
namespace App\Controller; namespace App\Controller;
use Database\AthleteEntity;
use Database\AthleteMapper; use Database\AthleteMapper;
use Database\EntrainementGateway; use Database\EntrainementGateway;
use Database\EntrainementMapper; use Database\EntrainementMapper;
@ -80,7 +81,7 @@ class AthleteController extends BaseController
print("Nom invalide."); print("Nom invalide.");
} else { } else {
try { try {
$athleteGateway = new AthleteGateway(new Connexion("pgsql:host=localhost;dbname=sae_3", "Perederii", "")); $athleteGateway = new AthleteGateway(new Connexion(DSN, DB_USER, DB_PASSWORD));
$listSearch = $athleteGateway->getAthlete($username); $listSearch = $athleteGateway->getAthlete($username);
$map = new AthleteMapper(); $map = new AthleteMapper();
$athleteEntity = $map->athleteSqlToEntity($listSearch); $athleteEntity = $map->athleteSqlToEntity($listSearch);
@ -154,7 +155,7 @@ class AthleteController extends BaseController
public function exercice(): Response public function exercice(): Response
{ {
try { try {
$entrainementGateway = new EntrainementGateway(new Connexion("pgsql:host=localhost;dbname=sae_3", "Perederii", "")); $entrainementGateway = new EntrainementGateway(new Connexion(DSN, DB_USER, DB_PASSWORD));
$listSearch = $entrainementGateway->getEntrainements(); $listSearch = $entrainementGateway->getEntrainements();
$map = new EntrainementMapper(); $map = new EntrainementMapper();
$entrainementEntity = $map->entrainementSqlToEntity($listSearch); $entrainementEntity = $map->entrainementSqlToEntity($listSearch);
@ -162,12 +163,10 @@ class AthleteController extends BaseController
$listUsers = []; $listUsers = [];
$i = 0; $i = 0;
foreach ($entrainementEntity as $entity) { foreach ($entrainementEntity as $entity) {
$user = $map->entrainementEntityToModel($entity); $training = $map->entrainementEntityToModel($entity);
$listUsers[$i] = ['idathlete' => number_format($user->getId(), 1), 'nom' => $user->getNom(), $listUsers[$i] = ['idTraining' => number_format($training->getId(), 1), 'date' => $training->getDate()->format("D j F Y"),
'prenom' => $user->getPrenom(),'email' => $user->getEmail(), 'sexe' => $user->getSexe(), 'location' => $training->getLocation(), 'description' => $training->getDescription(),
'taille' => $user->getTaille(), 'poids' => $user->getPoids(), 'motdepasse' => $user->getMotDePasse(), 'feedback' => $training->getFeedback()];
'datenaissance' => $user->getDateNaissance(), 'iscoach' => $user->getRole(), 'img' => 'test',
'username' => $user->getUsername()];
$i++; $i++;
} }
@ -181,7 +180,7 @@ class AthleteController extends BaseController
'mails' => [], 'mails' => [],
'users' => [], 'users' => [],
'infoUser' => [], 'infoUser' => [],
'exos' => [], 'exos' => $listUsers,
'member' => [] 'member' => []
]); ]);
@ -222,12 +221,16 @@ class AthleteController extends BaseController
public function addFriend(string $username, IRequest $req): Response public function addFriend(string $username, IRequest $req): Response
{ {
try { try {
$athleteGateway = new AthleteGateway(new Connexion("pgsql:host=localhost;dbname=sae_3", "Perederii", "")); $athleteGateway = new AthleteGateway(new Connexion(DSN, DB_USER, DB_PASSWORD));
$map= new AthleteMapper();
$userSearched=$athleteGateway->getAthleteByFirstName($username); $userSearched=$athleteGateway->getAthleteByFirstName($username);
$currentUser=$this->container->get(UserManager::class)->getUserById(1); $currentUser=$map->athleteEntityToModel(new AthleteEntity($athleteGateway->getAthleteById(1)));
if ($userSearched!=null) {
$currentUser->addFriend($userSearched);
foreach($userSearched as $user) { foreach($userSearched as $user) {
$listUser[] = $map->athleteEntityToModel(new AthleteEntity($user));
}
if ($listUser!=null) {
foreach ($userSearched as $user) {
$currentUser->addFriend($user);
$users = ['nom' => $user->getNom(), 'prenom' => $user->getPrenom(), 'img' => 'test', 'username' => $user->getUsername()]; $users = ['nom' => $user->getNom(), 'prenom' => $user->getPrenom(), 'img' => 'test', 'username' => $user->getUsername()];
} }
} }
@ -305,24 +308,42 @@ class AthleteController extends BaseController
#[Route(path: '/friendlist', name: 'friendlist2', methods: ['GET'])] #[Route(path: '/friendlist', name: 'friendlist2', methods: ['GET'])]
public function friendlist2(): Response public function friendlist2(): Response
{ {
$athleteGateway = new AthleteGateway(new Connexion("pgsql:host=localhost;dbname=sae_3", "Perederii", "")); try {
$friendList = $athleteGateway->getListIdFriends(1); $athleteGateway = new AthleteGateway(new Connexion(DSN, DB_USER, DB_PASSWORD));
foreach ($userSearched as $friendList) { $map=new AthleteMapper();
$users = ['nom' => $user->getNom(), 'prenom' => $user->getPrenom(), 'img' => 'test', 'username' => $user->getUsername()]; $friendEntity = $athleteGateway->getListIdFriends(1/*currentUser->getId()*/);
foreach($friendEntity as $users) {
if ($users['idathlete1']==1/*currentUser->getId()*/){
$user=(int)$users['idathlete2'];
} else {
$user=(int)$users['idathlete1'];
} }
return $this->render('./page/friend.html.twig',[ $listUserEntity[] = $map->athleteSqlToEntity($athleteGateway->getAthleteById(1));
}
foreach ($listUserEntity as $user) {
$friendList[] = ['nom' => $user[0]->getNom(), 'prenom' => $user[0]->getPrenom(), 'img' => 'test', 'username' => 'test'];
}
$response = $this->render('./page/friend.html.twig',[
'css' => $this->preference->getCookie(), 'css' => $this->preference->getCookie(),
'pp' => "test2", 'pp' => "test2",
'user' => "Doe", 'user' => "Doe",
'role' => "Athlète", 'role' => "Athlète",
'friendship' => $list, 'friendship' => $friendList,
'analyzes' => [], 'analyzes' => [],
'mails' => [], 'mails' => [],
'users' => [], 'users' => $users,
'infoUser' => [], 'infoUser' => [],
'exos' => [], 'exos' => [],
'member' => [], 'member' => [],
]); ]);
} catch (\Throwable $th) {
throw $th;
return $this->render("addfriend.html.twig", ['tabError' => $taberror]);
}
return $response;
} }
// #[Route(path: '/delete-friend', name: 'delete-friend', methods: ['POST'])] // #[Route(path: '/delete-friend', name: 'delete-friend', methods: ['POST'])]

@ -5,6 +5,11 @@ namespace App\Controller;
use App\Container; use App\Container;
use App\Router\Request\IRequest; use App\Router\Request\IRequest;
use App\Router\Response\Response; use App\Router\Response\Response;
use Database\AthleteGateway;
use Database\AthleteMapper;
use Database\Connexion;
use Database\NotificationGateway;
use Database\NotificationMapper;
use Shared\Attributes\Route; use Shared\Attributes\Route;
use Twig\Environment; use Twig\Environment;
use Data\Core\Preferences; use Data\Core\Preferences;
@ -23,22 +28,43 @@ class SocialController extends BaseController
} }
#[Route(path: '/mail', name: 'mail', methods: ['GET'])] #[Route(path: '/notification', name: 'notification', methods: ['GET'])]
public function mail(): Response public function mail(): Response
{ {
return $this->render('./page/mail.html.twig',[ try {
$notificationGateway = new NotificationGateway(new Connexion(DSN, DB_USER, DB_PASSWORD));
$listSearch = $notificationGateway->getNotifications();
$map = new NotificationMapper();
$notificationEntity = $map->notificationSqlToEntity($listSearch);
$listUsers = [];
foreach ($notificationEntity as $entity) {
$notification = $map->notificationEntityToModel($entity);
$listUsers[] = ['idnotif' => $notification->getId(), 'message' => $notification->getMessage(),
'date' => $notification->getDate(),'statut' => $notification->getStatut(), 'urgence' => $notification->getUrgence(),
'idathlete' => $notification->getToUserId()];
}
$response = $this->render('./page/notification.html.twig',[
'css' => $this->preference->getCookie(), 'css' => $this->preference->getCookie(),
'pp' => "test2", 'pp' => "test2",
'user' => "Doe", 'user' => "Doe",
'role' => "Athlète", 'role' => "Athlète",
'friendship' => [], 'friendship' => [],
'analyzes' => [], 'analyzes' => [],
'mails' => [], 'mails' => $listUsers,
'users' => [], 'users' => [],
'infoUser' => [], 'infoUser' => [],
'exos' => [], 'exos' => [],
'member' => [] 'member' => []
]); ]);
} catch (\Throwable $th) {
throw $th;
return $this->render("addfriend.html.twig", ['tabError' => $taberror]);
}
return $response;
} }

@ -68,7 +68,7 @@
<div class="sb-nav-link-icon"><img src="/assets/img/coaching.png"></div> <div class="sb-nav-link-icon"><img src="/assets/img/coaching.png"></div>
Coaching Coaching
</a> </a>
<a class="nav-link" href="/mail"> <a class="nav-link" href="/notification">
<div class="sb-nav-link-icon"><img src="/assets/img/letter.png"></div> <div class="sb-nav-link-icon"><img src="/assets/img/letter.png"></div>
Messagerie Messagerie
</a> </a>

@ -25,18 +25,18 @@
<thead> <thead>
<tr> <tr>
<th>Date</th> <th>Date</th>
<th>Type</th> <th>Description</th>
<th>Intensité prévue</th> <th>Localisation</th>
<th>Status</th> <th>FeedBack</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for exo in exos %} {% for exo in exos %}
<tr> <tr>
<td>{{exo.date}}</td> <td>{{exo.date}}</td>
<td>{{exo.type}}</td> <td>{{exo.description}}</td>
<td>{{exo.intensite}}</td> <td>{{exo.location}}</td>
<td>{{exo.status}}</td> <td>{{exo.feedback}}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

@ -28,8 +28,8 @@
<tbody> <tbody>
{% for mail in mails %} {% for mail in mails %}
<tr> <tr>
<td><img src="/assets/img/verif/{{mail.lu}}.png" width="25px" height="25px"></td> <td><img src="/assets/img/verif/{{mail.idnotif}}.png" width="25px" height="25px"></td>
<td>{{mail.nom}} {{mail.prenom}}</td> <td>{{mail.urgence}} {{mail.statut}}</td>
<td><a href="#">{{mail.message}}</a></td> <td><a href="#">{{mail.message}}</a></td>
</tr> </tr>
{% endfor %} {% endfor %}

@ -21,7 +21,8 @@ class AthleteGateway {
public function getAthleteById(int $userId): array public function getAthleteById(int $userId): array
{ {
$query = "SELECT * FROM Athlete WHERE idAthlete = :id AND isCoach=FALSE"; $query = "SELECT * FROM Athlete WHERE idAthlete = :id AND isCoach=FALSE";
$params = [':id' => [$userId, PDO::PARAM_INT]]; $params = [':id' => $userId];
// log::dd($params);
$res = $this->connection->executeWithErrorHandling($query, $params); $res = $this->connection->executeWithErrorHandling($query, $params);
return $res; return $res;
} }

@ -20,9 +20,6 @@ class Connexion extends \PDO {
public function executeQuery(string $query, array $parameters = []) { public function executeQuery(string $query, array $parameters = []) {
$this->stmt = $this->prepare($query); $this->stmt = $this->prepare($query);
// foreach ($parameters as $name => $value) {
// $this->stmt->bindValue($name, $value[0], $value[1]);
// }
foreach ($parameters as $name => $value) { foreach ($parameters as $name => $value) {
$dataType = is_numeric($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR; $dataType = is_numeric($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
$bindValueResult = $this->stmt->bindValue($name, $value, $dataType); $bindValueResult = $this->stmt->bindValue($name, $value, $dataType);
@ -48,13 +45,24 @@ class Connexion extends \PDO {
public function getResults(): array { public function getResults(): array {
$results = $this->stmt->fetchAll(\PDO::FETCH_ASSOC); $results = $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
// Convertissez les colonnes "taille" et "poids" en nombres // Convertissez les colonnes "taille" et "poids" en nombres
foreach ($results as &$row) { foreach ($results as &$row) {
if($row['identrainement']) {
$row['identrainement'] = floatval(($row['identrainement']));
}
if($row['latitude']) {
$row['latitude'] = floatval(($row['latitude']));
}
if($row['longitude']) {
$row['longitude'] = floatval(($row['longitude']));
}
if($row['taille']) {
$row['taille'] = floatval($row['taille']); $row['taille'] = floatval($row['taille']);
}
if($row['poids']) {
$row['poids'] = floatval($row['poids']); $row['poids'] = floatval($row['poids']);
} }
}
return $results; return $results;
} }
} }

@ -2,6 +2,8 @@
namespace Database; namespace Database;
use Shared\Log;
class EntrainementGateway class EntrainementGateway
{ {
private Connexion $connection; private Connexion $connection;

@ -2,9 +2,11 @@
namespace Database; namespace Database;
use DateTime;
use Model\Athlete; use Model\Athlete;
use Model\Training; use Model\Training;
use Model\User; use Model\User;
use Shared\Log;
class EntrainementMapper class EntrainementMapper
{ {
@ -13,8 +15,9 @@ class EntrainementMapper
foreach ($data as $entrainementData) { foreach ($data as $entrainementData) {
$entrainement = new EntrainementEntity(); $entrainement = new EntrainementEntity();
if (isset($entrainementData['idEntrainement'])) {
$entrainement->setIdEntrainement($entrainementData['idEntrainement']); if (isset($entrainementData['identrainement'])) {
$entrainement->setIdEntrainement($entrainementData['identrainement']);
} }
if (isset($entrainementData['date'])) { if (isset($entrainementData['date'])) {
@ -37,8 +40,8 @@ class EntrainementMapper
$entrainement->setFeedback($entrainementData['feedback']); $entrainement->setFeedback($entrainementData['feedback']);
} }
if (isset($entrainementData['coachId'])) { if (isset($entrainementData['athleteid'])) {
$entrainement->setCoachId($entrainementData['coachId']); $entrainement->setCoachId($entrainementData['athleteid']);
} }
$entrainementEntities[] = $entrainement; $entrainementEntities[] = $entrainement;
@ -48,12 +51,13 @@ class EntrainementMapper
} }
public function entrainementEntityToModel(EntrainementEntity $entrainementEntity): Training { public function entrainementEntityToModel(EntrainementEntity $entrainementEntity): Training {
$date = new DateTime($entrainementEntity->getDate());
return new Training( return new Training(
$entrainementEntity->getIdEntrainement(), $entrainementEntity->getIdEntrainement(),
$entrainementEntity->getDate(), $date,
$entrainementEntity->getDescription(),
$entrainementEntity->getLatitude(), $entrainementEntity->getLatitude(),
$entrainementEntity->getLongitude(), $entrainementEntity->getLongitude(),
$entrainementEntity->getDescription(),
$entrainementEntity->getFeedback() $entrainementEntity->getFeedback()
); );
} }

@ -0,0 +1,63 @@
<?php
namespace Database;
use Shared\Log;
class NotificationEntity
{
private $idNotif;
private $message;
private $date;
private $statut;
private $urgence;
private $idAthlete;
public function getIdNotif(): int
{
return $this->idNotif;
}
public function getMessage()
{
return $this->message;
}
public function getDate()
{
return $this->date;
}
public function getStatut()
{
return $this->statut;
}
public function getUrgence()
{
return $this->urgence;
}
public function getIdAthlete()
{
return $this->idAthlete;
}
public function setIdNotif($idNotif)
{
$this->idNotif = $idNotif;
}
public function setMessage($message)
{
$this->message = $message;
}
public function setDate($date)
{
$this->date = $date;
}
public function setStatut($statut)
{
$this->statut = $statut;
}
public function setUrgence($urgence)
{
$this->urgence = $urgence;
}
public function setIdAthlete($idAthlete)
{
$this->idAthlete = $idAthlete;
}
}

@ -0,0 +1,19 @@
<?php
namespace Database;
class NotificationGateway
{
private Connexion $connection;
public function __construct(Connexion $connection) {
$this->connection = $connection;
}
public function getNotifications(): array
{
$query = "SELECT * FROM Notification";
$res = $this->connection->executeWithErrorHandling($query);
return $res;
}
}

@ -0,0 +1,74 @@
<?php
namespace Database;
use DateTime;
use Model\Notification;
use Model\Training;
use Shared\Log;
class NotificationMapper
{
public function notificationSqlToEntity(array $data): array
{
$notificationEntities = [];
foreach ($data as $notificationData) {
$notification = new NotificationEntity();
if (isset($notificationData['idnotif'])) {
$notification->setIdNotif($notificationData['idnotif']);
}
if (isset($notificationData['message'])) {
$notification->setMessage($notificationData['message']);
}
if (isset($notificationData['date'])) {
$notification->setDate($notificationData['date']);
}
if (isset($notificationData['statut'])) {
$notification->setStatut($notificationData['statut']);
}
if (isset($notificationData['urgence'])) {
$notification->setUrgence($notificationData['urgence']);
}
if (isset($notificationData['athleteid'])) {
$notification->setIdAthlete($notificationData['athleteid']);
}
$notificationEntities[] = $notification;
}
return $notificationEntities;
}
public function notificationEntityToModel(NotificationEntity $notificationEntity): Notification
{
$date = new DateTime($notificationEntity->getDate());
return new Notification(
$notificationEntity->getIdNotif(),
$notificationEntity->getMessage(),
$date,
$notificationEntity->getStatut(),
$notificationEntity->getUrgence(),
$notificationEntity->getIdAthlete()
);
}
public function notificationToEntity(Notification $notification): NotificationEntity
{
$notif = new NotificationEntity();
$notif->setIdNotif($notification->getId());
$notif->setMessage($notification->getMessage());
$notif->setDate($notification->getDate());
$notif->setStatut($notification->getStatut());
$notif->setUrgence($notification->getUrgence());
$notif->setIdAthlete($notification->getToUserId());
return $notif;
}
}

@ -2,8 +2,41 @@
namespace Model; namespace Model;
use DateTime;
class Notification class Notification
{ {
private int $idNotif;
private string $message;
private \DateTime $date;
private string $statut;
private string $urgence;
private int $toUserId;
/**
* @param string $type
* @param string $message
*/
public function __construct(
int $idNotif,
string $message,
DateTime $date,
string $statut,
string $urgence,
int $toUserId
)
{
$this->idNotif = $idNotif;
$this->message = $message;
$this->date = $date;
$this->statut = $statut;
$this->urgence = $urgence;
$this->toUserId =$toUserId;
}
public function getId(){ return $this->idNotif;}
public function getDate(){ return $this->date;}
public function getStatut(){ return $this->statut;}
public function getUrgence(){ return $this->urgence;}
/** /**
* @return string * @return string
*/ */
@ -36,10 +69,6 @@ class Notification
$this->message = $message; $this->message = $message;
} }
private string $type;
private string $message;
private int $toUserId;
/** /**
* @return int * @return int
*/ */
@ -55,16 +84,6 @@ class Notification
{ {
$this->toUserId = $toUserId; $this->toUserId = $toUserId;
} }
/**
* @param string $type
* @param string $message
*/
public function __construct(int $toUserId,string $type, string $message)
{
$this->type = $type;
$this->toUserId =$toUserId;
$this->message = $message;
}
public function __toString(): string public function __toString(): string
{ {
return var_export($this, true); return var_export($this, true);

@ -33,7 +33,7 @@ class Training
return $this->date; return $this->date;
} }
public function getLocation(): String { public function getLocation(): String {
return $this->longitude . $this->latitude; return $this->longitude . ", " . $this->latitude;
} }
public function getLatitude(): float { public function getLatitude(): float {
return $this->latitude; return $this->latitude;

Loading…
Cancel
Save