From 00c772492b3112bc56b3670c63a70020f6847bed Mon Sep 17 00:00:00 2001 From: anperederi Date: Sat, 18 Nov 2023 17:55:06 +0100 Subject: [PATCH] :sparkles: :construction: :poop: add coach management --- Sources/src/console/Console.php | 120 +++++++++++++++--- Sources/src/data/model/Athlete.php | 20 +++ Sources/src/data/model/Coach.php | 8 +- Sources/src/data/model/CoachAthlete.php | 25 +++- Sources/src/data/model/Role.php | 24 +++- Sources/src/data/model/User.php | 11 ++ .../src/data/model/manager/CoachManager.php | 56 +++++++- Sources/src/data/stub/StubData.php | 16 ++- .../data/stub/repository/UserRepository.php | 17 ++- Sources/src/data/stub/service/AuthService.php | 25 ++-- 10 files changed, 273 insertions(+), 49 deletions(-) diff --git a/Sources/src/console/Console.php b/Sources/src/console/Console.php index 80798791..8051e1f2 100755 --- a/Sources/src/console/Console.php +++ b/Sources/src/console/Console.php @@ -197,15 +197,91 @@ function registerUser(DataManager $model) } } +function displayManagementArray() +{ + clearScreen(); + echo "\n--- Menu Management Groupe Athlete ---\n"; + echo "1. Ajouter un athlète\n"; + echo "2. Supprimer un athlète\n"; + echo "3. Voir tout les athlètes de la liste\n"; + echo "0. Retour au menu du coach\n"; + echo "Choisissez une option: "; +} + +function ArrayMenu(DataManager $model) +{ + do { + displayManagementArray(); + $coachChoice = trim(fgets(STDIN)); + + switch ($coachChoice) { + case '1': + echo "Renseignez le surnom de l'utilisateur : "; + $username = trim(fgets(STDIN)); + $model->coachMgr->addUser($model->userMgr->currentUser); + sleep(10); + break; + case '2': + echo "jsp pour l'instant pour supr un athlete !\n"; + sleep(2); + break; + case '3': + $usersArray = $model->coachMgr->getUsersList(); + if (!empty($usersArray)) { + foreach ($usersArray as $value) { + echo $value->to_String() . "\n"; + } + } else { + echo "Aucun utilisateur dans la liste\n"; + } + sleep(10); + break; + case '0': + return; + default : + echo "Option invalide. Veuillez réessayer.\n"; + sleep(2); + break; + } + } while($coachChoice); +} +function CoachMenu(DataManager $model) +{ + do { + displayCoachMenu(); + $coachChoice = trim(fgets(STDIN)); + + switch ($coachChoice) { + case '1': + $userRole = $model->coachMgr->getRole(); + if (!empty($userRole)) { + echo $userRole; + } else { + echo "Aucun utilisateur dans la liste\n"; + } + sleep(2); + break; + case '2': + echo "jsp pour l'instant pour les stats !\n"; + sleep(2); + return; + case '3': + echo "jsp pour l'instant pour les analyses !\n"; + sleep(2); + return; + case '4': + ArrayMenu($model); + break; + case '0': // Quitter + return; + default: + echo "Option invalide. Veuillez réessayer.\n"; + sleep(2); + break; + } + } while($coachChoice); +} -// const auth = getAuth(); -// signInWithEmailAndPassword(auth, email, password) -// const auth = getAuth(); -// signOut(auth).then(() => { -// // Sign-out successful. -// }).catch((error) => { -// // An error happened. -// }); while (true) { $loggedIn = false; @@ -215,14 +291,19 @@ while (true) { switch ($choice) { case '1': // Se connecter - if (loginUser($model)) { - $loggedIn = true; - } + $model->userMgr->login("Doe", "password123"); + $loggedIn = true; + +// if (loginUser($model)) { +// $loggedIn = true; +// } break; case '2': // S'inscrire - if (registerUser($model)) { - $loggedIn = true; - } + $model->userMgr->login("athlete", "hello321"); + $loggedIn = true; +// if (registerUser($model)) { +// $loggedIn = true; +// } break; case '0': // Quitter @@ -263,9 +344,13 @@ while (true) { break; case '5': // Athlètes (pour les Coachs seulement) - displayCoachMenu(); - $coachChoice = trim(fgets(STDIN)); - // TODO: Ajouter la logique pour les options de coach ici. + if($model->userMgr->currentUser->getRole() instanceof \Model\Coach) { + CoachMenu($model); + // TODO: Ajouter la logique pour les options de coach ici. + } else { + echo "Vous n'avez pas accès à cette section ! (il faut etre coach)\n"; + sleep(2); + } break; case '6': // Paramètres @@ -289,4 +374,5 @@ while (true) { } } } + ?> \ No newline at end of file diff --git a/Sources/src/data/model/Athlete.php b/Sources/src/data/model/Athlete.php index c0587000..417dba47 100644 --- a/Sources/src/data/model/Athlete.php +++ b/Sources/src/data/model/Athlete.php @@ -4,6 +4,26 @@ namespace Model; class Athlete extends Role { // Attributs spécifiques a l'athlete si nécessaire + public function addUser(\Model\User $user): void + { + if($this->CheckAdd($user)){ + $this->usersList[] = $user; + } else { + echo "Impossible de demander en ami autre que des athletes\n"; + } + } + private function CheckAdd(User $user) + { + if($user instanceof \Model\Athlete){ + return true; + } else { + return false; + } + } + + public function getUsersList(): array{ + return $this->usersList; + } } ?> \ No newline at end of file diff --git a/Sources/src/data/model/Coach.php b/Sources/src/data/model/Coach.php index 9f470448..da1255e6 100644 --- a/Sources/src/data/model/Coach.php +++ b/Sources/src/data/model/Coach.php @@ -1,8 +1,12 @@ \ No newline at end of file diff --git a/Sources/src/data/model/CoachAthlete.php b/Sources/src/data/model/CoachAthlete.php index b7578a85..b185e2b1 100644 --- a/Sources/src/data/model/CoachAthlete.php +++ b/Sources/src/data/model/CoachAthlete.php @@ -1,7 +1,30 @@ CheckAdd($user)){ + $this->usersList[] = $user; + } else { + echo "Impossible d'ajouter d'autres membres que des athletes !\n"; + } + } + public function CheckAdd(User $user) : bool { + if($user instanceof \Model\Athlete){ + return true; + } else { + return false; + } + } + + public function getUsersList(): ?array { + if (!empty($this->usersList)) { + return $this->usersList; + } else { + echo "Vous n'avez pas de groupe\n"; + return null; + } + } } \ No newline at end of file diff --git a/Sources/src/data/model/Role.php b/Sources/src/data/model/Role.php index 6e9a15ff..d6bb4c4d 100644 --- a/Sources/src/data/model/Role.php +++ b/Sources/src/data/model/Role.php @@ -1,8 +1,30 @@ usersList[] +// } + + /** + * @return void + */ + public abstract function getUsersList(): ?array; } diff --git a/Sources/src/data/model/User.php b/Sources/src/data/model/User.php index 3cfccc65..8db25538 100644 --- a/Sources/src/data/model/User.php +++ b/Sources/src/data/model/User.php @@ -6,6 +6,7 @@ use Model\Role; class User{ private int $id; + private String $username; private string $nom; private string $prenom; private string $email; @@ -18,6 +19,7 @@ class User{ public function __construct( int $id, + String $username, string $nom, string $prenom, string $email, @@ -29,6 +31,7 @@ class User{ Role $role ) { $this->id = $id; + $this->username = $username; $this->nom = $nom; $this->prenom = $prenom; $this->email = $email; @@ -48,6 +51,14 @@ class User{ $this->id = $id; } + public function getUsername(): int { + return $this->username; + } + + public function setUsername(int $username): void { + $this->username = $username; + } + public function getNom(): string { return $this->nom; } diff --git a/Sources/src/data/model/manager/CoachManager.php b/Sources/src/data/model/manager/CoachManager.php index ae6f6509..ccb3f0eb 100644 --- a/Sources/src/data/model/manager/CoachManager.php +++ b/Sources/src/data/model/manager/CoachManager.php @@ -2,15 +2,61 @@ namespace Manager; +use Model\Coach; +use Model\Role; use Model\User; +use Network\IAuthService; use Stub\AuthService; class CoachManager { - private User currentUser; - function __construct() { - if (isset(authService->currentUser)) { - this->currentUser = authService->currentUser; + private IAuthService $authService; + public ?User $currentUser = null; // Initialisé à null + function __construct(IAuthService $authService) + { + $this->authService = $authService; + + // Assurez-vous que authService n'est pas null avant d'accéder à currentUser + if ($this->authService && $this->authService->currentUser) { + $this->currentUser = $this->authService->currentUser; + } + } + + public function getRole(): ?Role + { + // Assurez-vous que currentUser n'est pas null avant d'accéder à getRole() + if ($this->currentUser && $this->currentUser->getRole()) { + return $this->currentUser->getRole(); + } + + return null; + } + + public function getUsersList(): ?Role + { + // Assurez-vous que currentUser n'est pas null avant d'accéder à getRole() + if ($this->currentUser && $this->currentUser->getRole()->getUsersList()) { + return $this->currentUser->getRole(); } + + return null; } -} \ No newline at end of file + +// faire validation !!! + public function addUser(User $user): void + { + // Assurez-vous que currentUser n'est pas null avant d'accéder à getRole() + if ($this->currentUser && $this->currentUser->getRole()) { + $this->currentUser->getRole()->addUser($user); + } elseif($this->currentUser && !$this->currentUser->getRole()) { + echo "Impossible d'ajouter l'utilisateur. role est null."; + } elseif(!$this->currentUser) { + echo "Impossible d'ajouter l'utilisateur. currentUser est null."; + } else { + echo "gros probleme !"; + } + } + +} + +?> \ No newline at end of file diff --git a/Sources/src/data/stub/StubData.php b/Sources/src/data/stub/StubData.php index 03b73d2f..dc0053d1 100644 --- a/Sources/src/data/stub/StubData.php +++ b/Sources/src/data/stub/StubData.php @@ -5,17 +5,19 @@ use Stub\AuthService; use Manager\{CoachManager, DataManager, UserManager}; use Stub\UserRepository; -class StubData extends DataManager{ - public function __construct(){ +class StubData extends DataManager { + public function __construct() + { $userRepository = new \Stub\UserRepository(); - $authService = new \Stub\AuthService($userRepository,new HashPassword()); + $authService = new \Stub\AuthService($userRepository, new \Shared\HashPassword()); + $this->userMgr = new UserManager($authService); -// if($this->userMgr->currentUser->Role ) // is an instance of ... - $this->coachMgr->currentUser = new CoachManager($authService); -// if(user.Role is instance of Coach) { -// this->CoachMgr = new CoachManager(); + +// if($this->userMgr->currentUser->getRole() instanceof \Model\Coach){ + $this->coachMgr = new CoachManager($authService); // } } } + ?> \ No newline at end of file diff --git a/Sources/src/data/stub/repository/UserRepository.php b/Sources/src/data/stub/repository/UserRepository.php index 1ecb443e..ebbb5f81 100644 --- a/Sources/src/data/stub/repository/UserRepository.php +++ b/Sources/src/data/stub/repository/UserRepository.php @@ -4,6 +4,7 @@ namespace Stub; use Model\Athlete; use Model\Coach; +use Model\CoachAthlete; use Model\User; use Repository\IUserRepository; @@ -11,11 +12,13 @@ class UserRepository implements IUserRepository { private array $users = []; public function __construct() { - $this->users[] = new User(1, "Doe", "John", "john.doe@example.com", "password123", 'M', 1.80, 75, new \DateTime("1985-05-15"), new Coach()); - $this->users[] = new User(2, "Smith", "Jane", "jane.smith@example.com", "secure456", 'F', 1.65, 60, new \DateTime("1990-03-10"), new Athlete()); - $this->users[] = new User(3, "Martin", "Paul", "paul.martin@example.com", "super789", 'M', 1.75, 68, new \DateTime("1988-08-20"), new Coach()); - $this->users[] = new User(4, "Brown", "Anna", "anna.brown@example.com", "test000", 'F', 1.70, 58, new \DateTime("1992-11-25"), new Athlete()); - $this->users[] = new User(5, "Lee", "Bruce", "bruce.lee@example.com", "hello321", 'M', 1.72, 70, new \DateTime("1970-02-05"), new Athlete()); + $this->users[] = new User(1, "Doe12", "Doe", "John", "john.doe@example.com", "password123", 'M', 1.80, 75, new \DateTime("1985-05-15"), new CoachAthlete()); + $this->users[] = new User(2, "Smith", "Smith", "Jane", "jane.smith@example.com", "secure456", 'F', 1.65, 60, new \DateTime("1990-03-10"), new Athlete()); + $this->users[] = new User(3, "Martin", "Martin", "Paul", "paul.martin@example.com", "super789", 'M', 1.75, 68, new \DateTime("1988-08-20"), new CoachAthlete()); + $this->users[] = new User(4, "Brown", "Brown", "Anna", "anna.brown@example.com", "test000", 'F', 1.70, 58, new \DateTime("1992-11-25"), new Athlete()); + $this->users[] = new User(5, "Lee", "Lee", "Bruce", "bruce.lee@example.com", "hello321", 'M', 1.72, 70, new \DateTime("1970-02-05"), new Athlete()); + $this->users[] = new User(6, "c", "c", "c", "coach@example.com", "c1", 'M', 1.80, 75, new \DateTime("1985-05-15"), new CoachAthlete()); + $this->users[] = new User(7, "athlete", "athlete", "a", "athlete@example.com", "hello321", 'F', 1.55, 67, new \DateTime("1999-09-30"), new Athlete()); } public function getItemById(int $id): ?User { @@ -65,12 +68,12 @@ class UserRepository implements IUserRepository { } // should have type here - public function AddItem( $user): void { + public function AddItem($user): void { $this->users[] = $user; } // should have type here - public function DeleteItem( $user): bool { + public function DeleteItem($user): bool { $index = array_search($user, $this->users); if ($index !== false) { unset($this->users[$index]); diff --git a/Sources/src/data/stub/service/AuthService.php b/Sources/src/data/stub/service/AuthService.php index 0b8ba248..7eb32a95 100644 --- a/Sources/src/data/stub/service/AuthService.php +++ b/Sources/src/data/stub/service/AuthService.php @@ -3,6 +3,7 @@ namespace Stub; use Model\Coach; use Model\Athlete; +use Model\CoachAthlete; use Model\User; use Shared\Exception\NotImplementedException; use Network\IAuthService; @@ -12,7 +13,7 @@ class AuthService implements IAuthService { private $userRepository; private $passwordHasher; - private $currentUser; + public $currentUser; public function __construct(UserRepository $userRepository, IHashPassword $passwordHasher) { $this->userRepository = $userRepository; @@ -20,13 +21,13 @@ class AuthService implements IAuthService { } public function login(string $username,string $password): ?User { - $user = $this->userRepository->GetItemByName($username,0,1); + $this->currentUser = $this->userRepository->GetItemByName($username,0,1); - if ($user == null || !$user instanceof User) { + if($this->currentUser == null || !$this->currentUser instanceof User) { throw new \Exception('Unable to find user with that name'); } - if ($user->isValidPassword($password)) { - return this->currentUser = $user; + if($this->currentUser->isValidPassword($password)) { + return $this->currentUser; } return null; } @@ -40,7 +41,8 @@ class AuthService implements IAuthService { if ($existingUser != null || $existingUser instanceof User ) { throw new \Exception('User already exists'); } - + + $username = $data['username']; $prenom = $data['prenom']; $nom = $data['nom']; $email = $data['email']; @@ -51,13 +53,14 @@ class AuthService implements IAuthService { $roleName = $data['roleName']; $role = null; if($roleName == "Coach"){ - $role = new Coach(); + $role = new CoachAthlete(); } else if($roleName == "Athlete"){ $role = new Athlete(); } $user = new User( random_int(0, 100), + $username, $nom, $prenom, $email, @@ -78,7 +81,11 @@ class AuthService implements IAuthService { public function logoutUser(): void { - print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! logout method not implemented !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - return; + if ($this->currentUser) { + $this->currentUser = null; // Déconnexion en assignant null à currentUser + echo "Déconnexion réussie.\n"; + } else { + echo "Aucun utilisateur connecté.\n"; + } } } \ No newline at end of file