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

@ -5,6 +5,11 @@ namespace App\Controller;
use App\Container;
use App\Router\Request\IRequest;
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 Twig\Environment;
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
{
return $this->render('./page/mail.html.twig',[
'css' => $this->preference->getCookie(),
'pp' => "test2",
'user' => "Doe",
'role' => "Athlète",
'friendship' => [],
'analyzes' => [],
'mails' => [],
'users' => [],
'infoUser' => [],
'exos' => [],
'member' => []
]);
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(),
'pp' => "test2",
'user' => "Doe",
'role' => "Athlète",
'friendship' => [],
'analyzes' => [],
'mails' => $listUsers,
'users' => [],
'infoUser' => [],
'exos' => [],
'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>
Coaching
</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>
Messagerie
</a>

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

@ -26,7 +26,7 @@
<table id="datatablesSimple" class="datatable-table">
{% if friendship is empty %}
<p>Vous n'avez aucun ami.</p>
{% else %}
{% else %}
<thead>
<tr>
<th></th>

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

@ -21,7 +21,8 @@ class AthleteGateway {
public function getAthleteById(int $userId): array
{
$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);
return $res;
}

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

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

@ -2,9 +2,11 @@
namespace Database;
use DateTime;
use Model\Athlete;
use Model\Training;
use Model\User;
use Shared\Log;
class EntrainementMapper
{
@ -13,8 +15,9 @@ class EntrainementMapper
foreach ($data as $entrainementData) {
$entrainement = new EntrainementEntity();
if (isset($entrainementData['idEntrainement'])) {
$entrainement->setIdEntrainement($entrainementData['idEntrainement']);
if (isset($entrainementData['identrainement'])) {
$entrainement->setIdEntrainement($entrainementData['identrainement']);
}
if (isset($entrainementData['date'])) {
@ -37,8 +40,8 @@ class EntrainementMapper
$entrainement->setFeedback($entrainementData['feedback']);
}
if (isset($entrainementData['coachId'])) {
$entrainement->setCoachId($entrainementData['coachId']);
if (isset($entrainementData['athleteid'])) {
$entrainement->setCoachId($entrainementData['athleteid']);
}
$entrainementEntities[] = $entrainement;
@ -48,12 +51,13 @@ class EntrainementMapper
}
public function entrainementEntityToModel(EntrainementEntity $entrainementEntity): Training {
$date = new DateTime($entrainementEntity->getDate());
return new Training(
$entrainementEntity->getIdEntrainement(),
$entrainementEntity->getDate(),
$entrainementEntity->getDescription(),
$date,
$entrainementEntity->getLatitude(),
$entrainementEntity->getLongitude(),
$entrainementEntity->getDescription(),
$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;
use DateTime;
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
*/
@ -36,10 +69,6 @@ class Notification
$this->message = $message;
}
private string $type;
private string $message;
private int $toUserId;
/**
* @return int
*/
@ -55,16 +84,6 @@ class Notification
{
$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
{
return var_export($this, true);

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

Loading…
Cancel
Save