team/addMembers (#21)
continuous-integration/drone/push Build is failing Details

Co-authored-by: vivien.dufour <vivien.dufour@etu.uca.fr>
Reviewed-on: #21
pull/22/head
Vivien DUFOUR 1 year ago
parent b957004939
commit a020811518

@ -11,6 +11,7 @@ use App\Controller\FrontController;
use App\Session\PhpSessionHandle; use App\Session\PhpSessionHandle;
$basePath = get_public_path(); $basePath = get_public_path();
$frontController = new FrontController($basePath); $frontController = new FrontController($basePath);
$frontController->run(PhpSessionHandle::init()); $frontController->run(PhpSessionHandle::init());

@ -22,6 +22,9 @@ CREATE TABLE Tactic
FOREIGN KEY (owner) REFERENCES Account FOREIGN KEY (owner) REFERENCES Account
); );
CREATE TABLE FormEntries(name varchar, description varchar);
CREATE TABLE Team CREATE TABLE Team
( (
id integer PRIMARY KEY AUTOINCREMENT, id integer PRIMARY KEY AUTOINCREMENT,
@ -31,14 +34,9 @@ CREATE TABLE Team
secondColor varchar secondColor varchar
); );
CREATE TABLE User
(
id integer PRIMARY KEY AUTOINCREMENT
);
CREATE TABLE Member CREATE TABLE Member(
( idTeam integer,
idTeam integer,
idMember integer, idMember integer,
role char(1) CHECK (role IN ('C', 'P')), role char(1) CHECK (role IN ('C', 'P')),
FOREIGN KEY (idTeam) REFERENCES Team (id), FOREIGN KEY (idTeam) REFERENCES Team (id),

@ -23,11 +23,17 @@ class TeamController {
return ViewHttpResponse::twig("insert_team.html.twig", []); return ViewHttpResponse::twig("insert_team.html.twig", []);
} }
/**
* @param array<string, mixed> $request public function displayAddMember() : HttpResponse {
* @return 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 { public function submitTeam(array $request): HttpResponse {
$errors = []; $errors = [];
$request = HttpRequest::from($request, $errors, [ $request = HttpRequest::from($request, $errors, [
@ -80,4 +86,26 @@ class TeamController {
$result = $this->model->displayTeam($id); $result = $this->model->displayTeam($id);
return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); 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'])));
}
} }

@ -62,4 +62,23 @@ class UserController extends VisitorController {
$ctrl = new Sub\TeamController($model); $ctrl = new Sub\TeamController($model);
return $ctrl->getTeam($id); 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);
}
} }

@ -2,8 +2,6 @@
namespace App\Data; namespace App\Data;
use http\Exception\InvalidArgumentException;
/** /**
* Base class of a user account. * Base class of a user account.
* Contains the private information that we don't want * Contains the private information that we don't want

@ -7,10 +7,15 @@ namespace App\Data;
*/ */
class Member { class Member {
/** /**
* @var int The member's user id * @var AccountUser The member's user account
*/ */
private int $userId; private int $userId;
/**
* @var int The member's team id
*/
private int $teamId;
/** /**
* @var MemberRole the member's role * @var MemberRole the member's role
*/ */
@ -20,8 +25,9 @@ class Member {
* @param int $userId * @param int $userId
* @param MemberRole $role * @param MemberRole $role
*/ */
public function __construct(int $userId, MemberRole $role) { public function __construct(int $userId, int $teamId, MemberRole $role) {
$this->userId = $userId; $this->userId = $userId;
$this->teamId = $teamId;
$this->role = $role; $this->role = $role;
} }
@ -39,4 +45,12 @@ class Member {
public function getRole(): MemberRole { public function getRole(): MemberRole {
return $this->role; return $this->role;
} }
/**
* @return int
*/
public function getTeamId(): int
{
return $this->teamId;
}
} }

@ -72,4 +72,8 @@ class Team {
return $this->members; return $this->members;
} }
public function addMember(Member $m) {
$this->members[] = $m;
}
} }

@ -0,0 +1,47 @@
<?php
namespace App\Gateway;
use App\Connexion;
use PDO;
class AuthGateway {
private Connexion $con;
/**
* @param Connexion $con
*/
public function __construct(Connexion $con) {
$this->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<string,string>|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;
}
}

@ -24,10 +24,19 @@ class TeamGateway {
); );
} }
/**
* @param string $name public function insertMember(int $idTeam, int $idMember, string $role) {
* @return array<string,mixed>[] $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 { public function listByName(string $name): array {
return $this->con->fetch( return $this->con->fetch(
"SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'",
@ -69,11 +78,30 @@ class TeamGateway {
*/ */
public function getMembersById(int $id): array { public function getMembersById(int $id): array {
return $this->con->fetch( 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", "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], ":id" => [$id, PDO::PARAM_INT]
] ]
); );
} }
public function getMemberIdByMail($mail) : array {
return $this->con->fetch(
"SELECT id FROM Account 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],
]
);
}
} }

@ -24,10 +24,14 @@ class TeamModel {
return intval($result[0]['id']); return intval($result[0]['id']);
} }
/**
* @param string $name public function addMember(string $mail, int $teamId, string $role) : int {
* @return Team[] $result = $this->gateway->getMemberIdByMail($mail)[0];
*/ $memberId = intval($result['id']);
$this->gateway->insertMember($teamId, $memberId, $role);
return $teamId;
}
public function listByName(string $name): array { public function listByName(string $name): array {
$teams = []; $teams = [];
$results = $this->gateway->listByName($name); $results = $this->gateway->listByName($name);
@ -42,13 +46,22 @@ class TeamModel {
$result = $this->gateway->getTeamById($id)[0]; $result = $this->gateway->getTeamById($id)[0];
$resultMembers = $this->gateway->getMembersById($id); $resultMembers = $this->gateway->getMembersById($id);
foreach ($resultMembers as $row) { foreach ($resultMembers as $row) {
var_dump($row['role']);
if ($row['role'] == 'C') { if ($row['role'] == 'C') {
$role = MemberRole::coach(); $role = MemberRole::coach();
} else { } else {
$role = MemberRole::player(); $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); 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;
}
} }

@ -0,0 +1,103 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ajouter un membre</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f1f1f1;
}
.container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"],
input[type="radio"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
fieldset {
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
input[type="submit"] {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
.role{
margin-top: 10px;
}
.radio{
display: flex;
justify-content: space-between;
}
</style>
</head>
<body>
<div class="container">
<h2>Ajouter un membre à votre équipe</h2>
<form action="/user/addMember" method="POST">
<div class="form-group">
<label for="team">Team où ajouter le membre :</label>
<input type="text" id="team" name="team" required>
<label for="mail">Email du membre :</label>
<input type="text" id="mail" name="mail" required>
<fieldset class="role">
<legend >Rôle du membre dans l'équipe :</legend>
<div class="radio">
<label for="P">Joueur</label>
<input type="radio" id="P" name="role" value="P" checked />
</div>
<div class="radio">
<label for="C">Coach</label>
<input type="radio" id="C" name="role" value="C" />
</div>
</fieldset>
</div>
<div class="form-group">
<input type="submit" value="Confirmer">
</div>
</form>
</div>
</body>
</html>

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ajouter un membre</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f1f1f1;
}
.container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Supprimez un membre de votre équipe</h2>
<form action="/user/deleteMember" method="POST">
<div class="form-group">
<label for="team">Team où supprimer le membre :</label>
<input type="text" id="team" name="team" required>
<label for="mail">Email du membre :</label>
<input type="text" id="mail" name="mail" required>
</div>
<div class="form-group">
<input type="submit" value="Confirmer">
</div>
</form>
</div>
</body>
</html>

@ -54,6 +54,7 @@
height: 80px; height: 80px;
width: 80px; width: 80px;
} }
</style> </style>
</head> </head>
<body> <body>
@ -72,8 +73,14 @@
<div class="color"><p>Couleur principale : </p><div class="square" id="mainColor"></div> </div> <div class="color"><p>Couleur principale : </p><div class="square" id="mainColor"></div> </div>
<div class="color"><p>Couleur secondaire : </p><div class="square" id="secondColor"></div></div> <div class="color"><p>Couleur secondaire : </p><div class="square" id="secondColor"></div></div>
</div> </div>
{% for m in team.members %}
<p> m.id </p> {% for m in team.listMembers() %}
<p> {{ m.getUserId() }} </p>
{% if m.getRole().isCoach() %}
<p> : Coach</p>
{% else %}
<p> : Joueur</p>
{% endif %}
{% endfor %} {% endfor %}
</div> </div>

@ -10,7 +10,7 @@
<p>Aucune équipe n'a été trouvée</p> <p>Aucune équipe n'a été trouvée</p>
<div class="container"> <div class="container">
<h2>Chercher une équipe</h2> <h2>Chercher une équipe</h2>
<form action="/listTea" method="post"> <form action="/user/listTeams" method="post">
<div class="form-group"> <div class="form-group">
<label for="name">Nom de l'équipe :</label> <label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required> <input type="text" id="name" name="name" required>
@ -22,7 +22,7 @@
</div> </div>
{% else %} {% else %}
{% for t in teams %} {% for t in teams %}
<div class="team" onclick="window.location.href = '/getTeam/{{ t.id }}'"> <div class="team" onclick="window.location.href = '/user/getTeam/{{ t.id }}'">
<p>Nom de l'équipe : {{ t.name }}</p> <p>Nom de l'équipe : {{ t.name }}</p>
<img src="{{ t.picture }}" alt="logo de l'équipe"> <img src="{{ t.picture }}" alt="logo de l'équipe">
</div> </div>

@ -62,7 +62,7 @@
<div class="container"> <div class="container">
<h2>Chercher une équipe</h2> <h2>Chercher une équipe</h2>
<form action="/listTeams" method="post"> <form action="/user/listTeams" method="post">
<div class="form-group"> <div class="form-group">
<label for="name">Nom de l'équipe :</label> <label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required> <input type="text" id="name" name="name" required>

Loading…
Cancel
Save