From c1c10c364c4bad5e6d8b5383dc6f167ac8fc9e77 Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Wed, 22 Nov 2023 14:30:49 +0100 Subject: [PATCH 1/3] conflicts correction --- public/index.php | 1 + sql/setup-tables.sql | 15 +++- src/Controller/Sub/TeamController.php | 41 ++++++++-- src/Data/Account.php | 2 - src/Data/Member.php | 22 +++++- src/Data/Team.php | 4 + src/Gateway/AuthGateway.php | 47 ++++++++++++ src/Gateway/TeamGateway.php | 42 +++++++++-- src/Model/TeamModel.php | 22 ++++-- src/Views/add_member.html.twig | 103 ++++++++++++++++++++++++++ src/Views/delete_member.html.twig | 73 ++++++++++++++++++ src/Views/display_team.html.twig | 11 ++- 12 files changed, 355 insertions(+), 28 deletions(-) create mode 100644 src/Gateway/AuthGateway.php create mode 100644 src/Views/add_member.html.twig create mode 100644 src/Views/delete_member.html.twig diff --git a/public/index.php b/public/index.php index cd4868d..d8dd098 100644 --- a/public/index.php +++ b/public/index.php @@ -11,6 +11,7 @@ use App\Controller\FrontController; use App\Session\PhpSessionHandle; $basePath = get_public_path(); + $frontController = new FrontController($basePath); $frontController->run(PhpSessionHandle::init()); diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index e105a1e..06bb32b 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -22,6 +22,15 @@ CREATE TABLE Tactic FOREIGN KEY (owner) REFERENCES Account ); +CREATE TABLE FormEntries(name varchar, description varchar); + +CREATE TABLE AccountUser( + id integer PRIMARY KEY AUTOINCREMENT, + username varchar, + hash varchar, + email varchar unique +); + CREATE TABLE Team ( id integer PRIMARY KEY AUTOINCREMENT, @@ -36,9 +45,9 @@ CREATE TABLE User id integer PRIMARY KEY AUTOINCREMENT ); -CREATE TABLE Member -( - idTeam integer, + +CREATE TABLE Member( + idTeam integer, idMember integer, role char(1) CHECK (role IN ('C', 'P')), FOREIGN KEY (idTeam) REFERENCES Team (id), diff --git a/src/Controller/Sub/TeamController.php b/src/Controller/Sub/TeamController.php index c3991a3..d85075a 100644 --- a/src/Controller/Sub/TeamController.php +++ b/src/Controller/Sub/TeamController.php @@ -11,23 +11,32 @@ use App\Validation\Validators; class TeamController { private TeamModel $model; + private Environment $twig; /** * @param TeamModel $model + * @param Environment $twig */ - public function __construct(TeamModel $model) { + public function __construct(TeamModel $model, Environment $twig) { $this->model = $model; + $this->twig = $twig; } public function displaySubmitTeam(): HttpResponse { return ViewHttpResponse::twig("insert_team.html.twig", []); } - /** - * @param array $request - * @return HttpResponse - */ + + public function displayAddMember() : HttpResponse { + return ViewHttpResponse::twig("add_member.html.twig", []); + } + + public function displayDeleteMember() : HttpResponse { + return ViewHttpResponse::twig("delete_member.html.twig", []); + } + public function submitTeam(array $request): HttpResponse { + $errors = []; $request = HttpRequest::from($request, $errors, [ @@ -80,4 +89,26 @@ class TeamController { $result = $this->model->displayTeam($id); return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); } + + public function addMember(array $request) : HttpResponse { + $errors = []; + + $request = HttpRequest::from($request, $errors, [ + "team" => [Validators::isInteger()], + "mail" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + ]); + + return $this->getTeam($this->model->addMember($request['mail'], intval($request['team']), $request['role'])); + } + + public function deleteMember(array $request) : HttpResponse { + $errors = []; + + $request = HttpRequest::from($request, $errors, [ + "team" => [Validators::isInteger()], + "mail" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + ]); + + return $this->getTeam($this->model->deleteMember($request['mail'], intval($request['team']))); + } } diff --git a/src/Data/Account.php b/src/Data/Account.php index 0ed4339..6a61563 100755 --- a/src/Data/Account.php +++ b/src/Data/Account.php @@ -2,8 +2,6 @@ namespace App\Data; -use http\Exception\InvalidArgumentException; - /** * Base class of a user account. * Contains the private information that we don't want diff --git a/src/Data/Member.php b/src/Data/Member.php index b415f65..64beb51 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -7,9 +7,14 @@ namespace App\Data; */ class Member { /** - * @var int The member's user id + * @var AccountUser The member's user account */ - private int $userId; + private AccountUser $user; + + /** + * @var int The member's team id + */ + private int $teamId; /** * @var MemberRole the member's role @@ -20,8 +25,9 @@ class Member { * @param int $userId * @param MemberRole $role */ - public function __construct(int $userId, MemberRole $role) { - $this->userId = $userId; + public function __construct(Account $user, int $teamId, MemberRole $role) { + $this->user = $user; + $this->teamId = $teamId; $this->role = $role; } @@ -39,4 +45,12 @@ class Member { public function getRole(): MemberRole { return $this->role; } + + /** + * @return int + */ + public function getTeamId(): int + { + return $this->teamId; + } } diff --git a/src/Data/Team.php b/src/Data/Team.php index e50ad40..a359722 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -72,4 +72,8 @@ class Team { return $this->members; } + public function addMember(Member $m) { + $this->members[] = $m; + } + } diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php new file mode 100644 index 0000000..3370d26 --- /dev/null +++ b/src/Gateway/AuthGateway.php @@ -0,0 +1,47 @@ +con = $con; + } + + + public function mailExist(string $email): bool { + return $this->getUserFields($email) != null; + } + + + public function insertAccount(string $username, string $hash, string $email): void { + $this->con->exec("INSERT INTO AccountUser(username, hash, email) VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash' => [$hash, PDO::PARAM_STR],':email' => [$email, PDO::PARAM_STR]]); + } + + public function getUserHash(string $email): string { + $results = $this->con->fetch("SELECT hash FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); + return $results[0]['hash']; + } + + + /** + * @param string $email + * @return array|null + */ + public function getUserFields(string $email): ?array { + $results = $this->con->fetch("SELECT username,email FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); + $firstRow = $results[0] ?? null; + return $firstRow; + } + + + + +} diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index c3d22dc..6c13571 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -24,10 +24,19 @@ class TeamGateway { ); } - /** - * @param string $name - * @return array[] - */ + + public function insertMember(int $idTeam, int $idMember, string $role) { + $this->con->exec( + "INSERT INTO Member(idTeam, idMember, role) VALUES (:idTeam , :idMember, :role)", + [ + ":idTeam" => [$idTeam, PDO::PARAM_INT], + ":idMember" => [$idMember, PDO::PARAM_INT], + ":role" => [$role, PDO::PARAM_STR] + ] + ); + } + + public function listByName(string $name): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", @@ -69,11 +78,30 @@ class TeamGateway { */ public function getMembersById(int $id): array { return $this->con->fetch( - "SELECT m.role,u.id FROM User u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", - [ - ":id" => [$id, PDO::PARAM_INT], + "SELECT u.id,m.role,u.email,u.username FROM AccountUser u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", + [ + ":id" => [$id, PDO::PARAM_INT] ] ); } + public function getMemberIdByMail($mail) : array { + return $this->con->fetch( + "SELECT id FROM AccountUser WHERE email = :mail", + [ + ":mail" => [$mail, PDO::PARAM_STR] + ] + ); + } + + public function deleteMember(int $idTeam, int $idMember) { + $this->con->exec( + "DELETE FROM Member WHERE idTeam = :idTeam AND idMember = :idMember", + [ + ":idTeam" => [$idTeam, PDO::PARAM_INT], + ":idMember" => [$idMember, PDO::PARAM_INT], + ] + ); + } + } diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 65f22a7..5a3ce5f 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -24,10 +24,14 @@ class TeamModel { return intval($result[0]['id']); } - /** - * @param string $name - * @return Team[] - */ + + public function addMember(string $mail, int $teamId, string $role) : int { + $result = $this->gateway->getMemberIdByMail($mail)[0]; + $memberId = intval($result['id']); + $this->gateway->insertMember($teamId, $memberId, $role); + return $teamId; + } + public function listByName(string $name): array { $teams = []; $results = $this->gateway->listByName($name); @@ -47,8 +51,16 @@ class TeamModel { } else { $role = MemberRole::player(); } - $members[] = new Member($row['id'], $role); + $members[] = new Member($row['id'], $id, $role); } return new Team(intval($result['id']), $result['name'], $result['picture'], Color::from($result['mainColor']), Color::from($result['secondColor']), $members); } + + public function deleteMember(string $mail, int $teamId) : int { + $result = $this->gateway->getMemberIdByMail($mail)[0]; + $memberId = intval($result['id']); + $this->gateway->deleteMember($teamId, $memberId); + return $teamId; + } + } diff --git a/src/Views/add_member.html.twig b/src/Views/add_member.html.twig new file mode 100644 index 0000000..7faef23 --- /dev/null +++ b/src/Views/add_member.html.twig @@ -0,0 +1,103 @@ + + + + + Ajouter un membre + + + + +
+

Ajouter un membre à votre équipe

+
+
+ + + + + +
+ Rôle du membre dans l'équipe : +
+ + +
+
+ + +
+
+ +
+
+ +
+
+
+ + + \ No newline at end of file diff --git a/src/Views/delete_member.html.twig b/src/Views/delete_member.html.twig new file mode 100644 index 0000000..9be5803 --- /dev/null +++ b/src/Views/delete_member.html.twig @@ -0,0 +1,73 @@ + + + + + Ajouter un membre + + + + +
+

Supprimez un membre de votre équipe

+
+
+ + + + +
+
+ +
+
+
+ + + \ No newline at end of file diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index ada8566..f1207db 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -54,6 +54,7 @@ height: 80px; width: 80px; } + @@ -72,8 +73,14 @@

Couleur principale :

Couleur secondaire :

- {% for m in team.members %} -

m.id

+ + {% for m in team.listMembers() %} +

{{ m.getUserId() }}

+ {% if m.getRole() == 'C' %} +

: Coach

+ {% else %} +

: Joueur

+ {% endif %} {% endfor %} -- 2.36.3 From 2a1db0e3d48f9403daf1053c9423d88e7e9157ef Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Wed, 22 Nov 2023 14:53:53 +0100 Subject: [PATCH 2/3] routes modification in twig --- src/Controller/Sub/TeamController.php | 5 +---- src/Controller/UserController.php | 2 ++ src/Views/add_member.html.twig | 2 +- src/Views/delete_member.html.twig | 2 +- src/Views/display_teams.html.twig | 4 ++-- src/Views/list_team_by_name.html.twig | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Controller/Sub/TeamController.php b/src/Controller/Sub/TeamController.php index d85075a..adefa41 100644 --- a/src/Controller/Sub/TeamController.php +++ b/src/Controller/Sub/TeamController.php @@ -11,15 +11,12 @@ use App\Validation\Validators; class TeamController { private TeamModel $model; - private Environment $twig; /** * @param TeamModel $model - * @param Environment $twig */ - public function __construct(TeamModel $model, Environment $twig) { + public function __construct(TeamModel $model) { $this->model = $model; - $this->twig = $twig; } public function displaySubmitTeam(): HttpResponse { diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index c2b9041..72b7d1c 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -60,4 +60,6 @@ class UserController extends VisitorController { $ctrl = new Sub\TeamController($model); return $ctrl->getTeam($id); } + + } diff --git a/src/Views/add_member.html.twig b/src/Views/add_member.html.twig index 7faef23..ef74da2 100644 --- a/src/Views/add_member.html.twig +++ b/src/Views/add_member.html.twig @@ -73,7 +73,7 @@

Ajouter un membre à votre équipe

-
+
diff --git a/src/Views/delete_member.html.twig b/src/Views/delete_member.html.twig index 9be5803..a478284 100644 --- a/src/Views/delete_member.html.twig +++ b/src/Views/delete_member.html.twig @@ -56,7 +56,7 @@

Supprimez un membre de votre équipe

- +
diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig index 47b3cbc..bf89909 100644 --- a/src/Views/display_teams.html.twig +++ b/src/Views/display_teams.html.twig @@ -10,7 +10,7 @@

Aucune équipe n'a été trouvée

Chercher une équipe

- +
@@ -22,7 +22,7 @@
{% else %} {% for t in teams %} -
+

Nom de l'équipe : {{ t.name }}

logo de l'équipe
diff --git a/src/Views/list_team_by_name.html.twig b/src/Views/list_team_by_name.html.twig index 1165da3..0ea1905 100644 --- a/src/Views/list_team_by_name.html.twig +++ b/src/Views/list_team_by_name.html.twig @@ -62,7 +62,7 @@

Chercher une équipe

- +
-- 2.36.3 From 9640b7083990fbbf66253030f51db28b984c6fa6 Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Wed, 22 Nov 2023 15:18:12 +0100 Subject: [PATCH 3/3] fix --- sql/setup-tables.sql | 18 ------------------ src/Controller/UserController.php | 17 +++++++++++++++++ src/Data/Member.php | 6 +++--- src/Gateway/TeamGateway.php | 4 ++-- src/Model/TeamModel.php | 1 + src/Views/display_team.html.twig | 2 +- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 06bb32b..b13712f 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -24,12 +24,6 @@ CREATE TABLE Tactic CREATE TABLE FormEntries(name varchar, description varchar); -CREATE TABLE AccountUser( - id integer PRIMARY KEY AUTOINCREMENT, - username varchar, - hash varchar, - email varchar unique -); CREATE TABLE Team ( @@ -40,11 +34,6 @@ CREATE TABLE Team secondColor varchar ); -CREATE TABLE User -( - id integer PRIMARY KEY AUTOINCREMENT -); - CREATE TABLE Member( idTeam integer, @@ -53,10 +42,3 @@ CREATE TABLE Member( FOREIGN KEY (idTeam) REFERENCES Team (id), FOREIGN KEY (idMember) REFERENCES User (id) ); - -CREATE TABLE TacticInfo -( - id integer PRIMARY KEY AUTOINCREMENT, - name varchar, - creation_date timestamp DEFAULT CURRENT_TIMESTAMP -); diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 72b7d1c..b6e1431 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -61,5 +61,22 @@ class UserController extends VisitorController { return $ctrl->getTeam($id); } + public function addMember(): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + return $ctrl->displayAddMember($_POST); + } + return $ctrl->addMember($_POST); + } + + public function deleteMember(): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + return $ctrl->displayDeleteMember($_POST); + } + return $ctrl->deleteMember($_POST); + } } diff --git a/src/Data/Member.php b/src/Data/Member.php index 64beb51..3049fde 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -9,7 +9,7 @@ class Member { /** * @var AccountUser The member's user account */ - private AccountUser $user; + private int $userId; /** * @var int The member's team id @@ -25,8 +25,8 @@ class Member { * @param int $userId * @param MemberRole $role */ - public function __construct(Account $user, int $teamId, MemberRole $role) { - $this->user = $user; + public function __construct(int $userId, int $teamId, MemberRole $role) { + $this->userId = $userId; $this->teamId = $teamId; $this->role = $role; } diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 6c13571..c98d803 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -78,7 +78,7 @@ class TeamGateway { */ public function getMembersById(int $id): array { return $this->con->fetch( - "SELECT u.id,m.role,u.email,u.username FROM AccountUser u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", + "SELECT a.id,m.role,a.email,a.username FROM Account a,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = a.id", [ ":id" => [$id, PDO::PARAM_INT] ] @@ -87,7 +87,7 @@ class TeamGateway { public function getMemberIdByMail($mail) : array { return $this->con->fetch( - "SELECT id FROM AccountUser WHERE email = :mail", + "SELECT id FROM Account WHERE email = :mail", [ ":mail" => [$mail, PDO::PARAM_STR] ] diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 5a3ce5f..c936d30 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -46,6 +46,7 @@ class TeamModel { $result = $this->gateway->getTeamById($id)[0]; $resultMembers = $this->gateway->getMembersById($id); foreach ($resultMembers as $row) { + var_dump($row['role']); if ($row['role'] == 'C') { $role = MemberRole::coach(); } else { diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index f1207db..474397e 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -76,7 +76,7 @@ {% for m in team.listMembers() %}

{{ m.getUserId() }}

- {% if m.getRole() == 'C' %} + {% if m.getRole().isCoach() %}

: Coach

{% else %}

: Joueur

-- 2.36.3