diff --git a/public/index.php b/public/index.php index bdb9000..cc63837 100644 --- a/public/index.php +++ b/public/index.php @@ -42,9 +42,11 @@ $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->ope $teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(new \App\Gateway\TeamGateway($con)),$twig); $router->map("GET","/team/new", fn()=>$teamController->displaySubmitTeam()); $router->map("POST","/team/new", fn()=>$teamController->SubmitTeam($_POST)); + $router->map("GET","/team/list", fn()=>$teamController->displayListTeamByName()); $router->map("POST","/team/list", fn()=>$teamController->ListTeamByName($_POST)); -/*todo $router->map("GET","/team/[i:id]", fn()=>$teamController->displayTeam);*/ + +$router->map("GET","/team/[i:id]", fn(int $id)=>$teamController->displayTeam($id)); $match = $router->match(); diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 8702b3e..8228d6a 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -28,11 +28,7 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ } public function displaySubmitTeam() { - try { - $this->twig->display("insert_team.html.twig", []); - } catch (LoaderError | RuntimeError | SyntaxError $e) { - echo " twig error : $e"; - } + return ViewHttpResponse::twig("insert_team.html.twig", []); } public function submitTeam(array $request): HttpResponse { @@ -51,33 +47,34 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ $badFields[] = $e->getFieldName(); } } - return ViewHttpResponse::twig('insert_team.html.twig',['bad_fields'=> $badFields]); + return ViewHttpResponse::twig('insert_team.html.twig', ['bad_fields' => $badFields]); } $this->model->createTeam($request['name'], $request['picture'], intval($request['mainColor']), intval($request['secondColor'])); - return ViewHttpResponse::twig('sample_form.html.twig',[]); /*todo appeler une vue qui display la team au lieu de ça*/ + return ViewHttpResponse::twig('sample_form.html.twig', []); /*todo appeler une vue qui display la team au lieu de ça*/ } - public function displayListTeamByName(){ - try { - $this->twig->display("list_team_by_name.html.twig", []); - } catch (LoaderError | RuntimeError | SyntaxError $e) { - echo " twig error : $e"; - } + public function displayListTeamByName(): HttpResponse { + return ViewHttpResponse::twig("list_team_by_name.html.twig", []); } + public function listTeamByName(array $request): HttpResponse { $errors = []; $request = HttpRequest::from($request, $errors, [ - "name" => [Validators::lenBetween(1,32),Validators::nameWithSpaces()] + "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()] ]); - if(!empty($errors) && $errors[0] instanceof FieldValidationFail){ + if (!empty($errors) && $errors[0] instanceof FieldValidationFail) { $badField = $errors[0]->getFieldName(); - return ViewHttpResponse::twig('list_team_by_name.html.twig',['bad_field' => $badField]) ; + return ViewHttpResponse::twig('list_team_by_name.html.twig', ['bad_field' => $badField]); } $results = $this->model->listByName($request['name']); - if (empty($results)){ - /*todo appelle de la bonne vue qui va afficher un message qui dit que bah ca retourne rien, proposer de refaire une recherche*/ + if (empty($results)) { + return ViewHttpResponse::twig('display_teams.html.twig', []); } - return ViewHttpResponse::twig('display_teams.html.twig',['teams' => $results]); + return ViewHttpResponse::twig('display_teams.html.twig', ['teams' => $results]); + } + + public function displayTeam(int $id): HttpResponse{ + $results = $this->model->displayTeam($id); } } diff --git a/src/Data/Team.php b/src/Data/Team.php index bcdabac..321a5d9 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -5,6 +5,7 @@ namespace App\Data; use http\Url; class Team { + private int $id; private string $name; private Url $picture; private Color $mainColor; @@ -22,7 +23,8 @@ class Team { * @param Color $secondColor * @param array $members */ - public function __construct(string $name, Url $picture, Color $mainColor, Color $secondColor, array $members =[]) { + public function __construct(int $id,string $name, Url $picture, Color $mainColor, Color $secondColor, array $members =[]) { + $this->id = $id; $this->name = $name; $this->picture = $picture; $this->mainColor = $mainColor; @@ -30,6 +32,13 @@ class Team { $this->members = $members; } + /** + * @return int + */ + public function getId(): int { + return $this->id; + } + /** * @return string */ diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 7c5883c..7d9c014 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -3,11 +3,9 @@ namespace App\Gateway; use App\Connexion; -use App\Data\Color; use PDO; -class TeamGateway -{ +class TeamGateway { private Connexion $con; public function __construct(Connexion $con) { @@ -16,7 +14,7 @@ class TeamGateway public function insert(string $name, string $picture, int $mainColor, int $secondColor) { $this->con->exec( - "INSERT INTO Team VALUES (:teamName , :picture, :mainColor, :secondColor)", + "INSERT INTO Team(name, picture, mainColor, secondColor) VALUES (:teamName , :picture, :mainColor, :secondColor)", [ ":teamName" => [$name, PDO::PARAM_STR], ":picture" => [$picture, PDO::PARAM_STR], @@ -28,11 +26,29 @@ class TeamGateway public function listByName(string $name): array { return $this->con->fetch( - "SELECT name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%:match%' ", + "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", [ - ":match" => [$name, PDO::PARAM_STR] + ":name" => [$name, PDO::PARAM_STR] ] ); } + public function getTeamById(int $id): array{ + return $this->con->fetch( + "SELECT name,picture,mainColor,secondColor FROM Team WHERE id = :id", + [ + ":id" => [$id, PDO::PARAM_INT] + ] + ); + } + + public function getMembersById($id):array{ + return $this->con->fetch( + "SELECT p.role,m.email FROM Member m,Team t,Participate p WHERE t.id = :id AND p.idTeam = t.id AND p.idMember = m.id", + [ + ":id" => [$id, PDO::PARAM_INT] + ] + ); + } + } \ No newline at end of file diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 02eef70..30c9fde 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -9,16 +9,6 @@ use App\Data\Team; */ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) pour le controller qui l'utilise, catch celle de la gw, */ { - - /** - * Attributes used to handle errors - */ - public const ERROR_INVALID_COLOR = 0; - public const ERROR_INVALID_NAME = 1; - public const ERROR_INVALID_PICTURE = 2; - public const ERROR_INVALID_SEARCH = 3; - public const ERROR_NO_DATA_FOUND = 4; - private TeamGateway $gateway; /** @@ -31,16 +21,20 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po public function createTeam(string $name,string $picture,int $mainColor, int $secondColor) { $this->gateway->insert($name,$picture,$mainColor,$secondColor); - } public function listByName(string $name):array { $teams=[]; $results = $this->gateway->listByName($name); - foreach ($results as $row){ - $teams[] = new Team($row['name'],$row['picture'],$row['mainColor'],$row['secondColor']); + $teams[] = new Team($row['id'],$row['name'],$row['picture'],$row['mainColor'],$row['secondColor']); } return $teams; } + + public function displayTeam(int $id): array{ + $resultTeam = $this->gateway->getTeamById($id); + $resultMembers = $this->gateway->getMembersById($id); + + } } \ No newline at end of file diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig new file mode 100644 index 0000000..a1f88fc --- /dev/null +++ b/src/Views/display_team.html.twig @@ -0,0 +1,35 @@ + + + + + Twig view + + + +

Hello world

+ +{% if teams is empty %} +

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

+
+

Chercher une équipe

+
+
+ + +
+
+ +
+
+
+{% else %} + {% for t in teams %} +
+

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

+

picture : {{ t.picture }}

+
+ {% endfor %} +{% endif %} + + + \ No newline at end of file diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig new file mode 100644 index 0000000..a1f88fc --- /dev/null +++ b/src/Views/display_teams.html.twig @@ -0,0 +1,35 @@ + + + + + Twig view + + + +

Hello world

+ +{% if teams is empty %} +

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

+
+

Chercher une équipe

+
+
+ + +
+
+ +
+
+
+{% else %} + {% for t in teams %} +
+

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

+

picture : {{ t.picture }}

+
+ {% endfor %} +{% endif %} + + + \ No newline at end of file diff --git a/src/Views/list_team_by_name.html.twig b/src/Views/list_team_by_name.html.twig index 7e0cbb3..1d9ddf3 100644 --- a/src/Views/list_team_by_name.html.twig +++ b/src/Views/list_team_by_name.html.twig @@ -61,7 +61,7 @@
-

Créer une équipe

+

Chercher une équipe