From f9eb6fbb6d0a10bce2b8c5b4ea257d3b4ff71475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20DAIM?= Date: Wed, 8 Nov 2023 17:30:06 +0100 Subject: [PATCH 01/16] new branch starting the team part in the MVC, plus a little change about the type Color --- Documentation/team.puml | 3 +++ public/index.php | 3 +++ sql/setup-tables.sql | 8 +++++++- src/Controller/FrontController.php | 8 ++++++++ src/Controller/TeamController.php | 33 ++++++++++++++++++++++++++++++ src/Data/Color.php | 21 +++++++++++++++---- src/Gateway/TeamGateway.php | 23 +++++++++++++++++++++ src/Model/TeamModel.php | 24 ++++++++++++++++++++++ 8 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 Documentation/team.puml create mode 100644 src/Controller/FrontController.php create mode 100644 src/Controller/TeamController.php create mode 100644 src/Gateway/TeamGateway.php create mode 100644 src/Model/TeamModel.php diff --git a/Documentation/team.puml b/Documentation/team.puml new file mode 100644 index 0000000..f775665 --- /dev/null +++ b/Documentation/team.puml @@ -0,0 +1,3 @@ +@startuml +/*todo*/ +@enduml \ No newline at end of file diff --git a/public/index.php b/public/index.php index 4c5290b..88d90fa 100644 --- a/public/index.php +++ b/public/index.php @@ -40,6 +40,9 @@ $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); +$teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(),$twig); +$router->map("GET","/team/new", fn()=>$teamController->submitTeam($_POST)); + $match = $router->match(); if ($match == null) { diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 0c6fbe7..70723dc 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -1,8 +1,14 @@ - -- drop tables here DROP TABLE IF EXISTS FormEntries; CREATE TABLE FormEntries(name varchar, description varchar); +CREATE TABLE Team( + id numeric PRIMARY KEY AUTOINCREMENT, + name varchar, + picture varchar, + mainColor varchar, + secondColor varchar +); diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php new file mode 100644 index 0000000..c1a39bc --- /dev/null +++ b/src/Controller/FrontController.php @@ -0,0 +1,8 @@ +model = $model; + $this->twig = $twig; + } + + public function submitTeam(array $request){ + if(isset($request['name']) && isset($request["picture"]) && isset($request["mainColor"]) && isset($request["secondColor"])){ + $result= $this->$model->; + } + else{ + http_response_code(400); + echo "Champ(s) manquant(s)"; + } + } + +} + + diff --git a/src/Data/Color.php b/src/Data/Color.php index f841731..bc9043b 100755 --- a/src/Data/Color.php +++ b/src/Data/Color.php @@ -14,10 +14,7 @@ class Color { * @param int $value 6 bytes unsigned int that represents an RGB color * @throws \InvalidArgumentException if the value is negative or greater than 0xFFFFFF */ - public function __constructor(int $value) { - if ($value < 0 || $value > 0xFFFFFF) { - throw new InvalidArgumentException("int color value is invalid, must be positive and lower than 0xFFFFFF"); - } + private function __constructor(int $value) { $this->value = $value; } @@ -27,4 +24,20 @@ class Color { public function getValue(): int { return $this->value; } + + public static function from(int $value): Color { + $color = self::tryFrom($value); + if ($color == null) { + throw new InvalidArgumentException("int color value is invalid, must be positive and lower than 0xFFFFFF"); + } + return $color; + } + + public static function tryFrom(int $value): ?Color { + if ($value < 0 || $value > 0xFFFFFF) { + return null; + } + return new Color($value); + } + } \ No newline at end of file diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php new file mode 100644 index 0000000..2bd5faa --- /dev/null +++ b/src/Gateway/TeamGateway.php @@ -0,0 +1,23 @@ +con = $con; + } + + function insert(string $name, string $picture, string $mainColor, string $secondColor) { + $this->con->exec( /* todo */ + "INSERT INTO Team VALUES (:name, :picture)", + [ + ":name" => [$username, PDO::PARAM_STR], + "description" => [$description, PDO::PARAM_STR] + ] + ); + } +} \ No newline at end of file diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php new file mode 100644 index 0000000..b1d4e67 --- /dev/null +++ b/src/Model/TeamModel.php @@ -0,0 +1,24 @@ +gateway = $gateway; + } + + + public function createTeam(string $name, Color $mainColor, Color $secondColor ){ + + } +} \ No newline at end of file -- 2.36.3 From c8df55669983ba7ffbc45490bea2c43f0e385f89 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Fri, 10 Nov 2023 15:23:21 +0100 Subject: [PATCH 02/16] made progression on the create team almost done --- src/Controller/FrontController.php | 8 ------ src/Controller/TeamController.php | 6 +++-- src/Gateway/TeamGateway.php | 20 +++++++++++---- src/Model/TeamModel.php | 39 +++++++++++++++++++++++++++--- src/Model/Validation.php | 10 ++++++++ 5 files changed, 65 insertions(+), 18 deletions(-) delete mode 100644 src/Controller/FrontController.php create mode 100644 src/Model/Validation.php diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php deleted file mode 100644 index c1a39bc..0000000 --- a/src/Controller/FrontController.php +++ /dev/null @@ -1,8 +0,0 @@ -$model->; + $errors = $this->model->createTeam($request['name'],$request['picture'],$request['mainColor'],$request["secondColor"]); + /*todo gestion des erreurs grace au tableau*/ } else{ http_response_code(400); - echo "Champ(s) manquant(s)"; + /*todo rappeler vue avec parametre pour signaler les pb */ } } diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 2bd5faa..56992d8 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -1,7 +1,10 @@ con = $con; } - function insert(string $name, string $picture, string $mainColor, string $secondColor) { - $this->con->exec( /* todo */ - "INSERT INTO Team VALUES (:name, :picture)", + public function insert(string $name, string $picture, Color $mainColor, Color $secondColor) { + $this->con->exec( + "INSERT INTO Team VALUES (:name, :picture, :mainColor, :secondColor)", [ - ":name" => [$username, PDO::PARAM_STR], - "description" => [$description, PDO::PARAM_STR] + ":name" => [$name, PDO::PARAM_STR], + ":picture" => [$picture, PDO::PARAM_STR], + ":mainColor" => [$mainColor, PDO::PARAM_STR], + ":secondColor" => [$secondColor, PDO::PARAM_STR] ] ); } + + public function listByName($name): array { + /*todo*/ + } + } \ No newline at end of file diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index b1d4e67..3158423 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -1,12 +1,22 @@ gateway = $gateway; } + public function createTeam(string $name,string $picture,int $mainColorValue, int $secondColorValue, array $errors) { - public function createTeam(string $name, Color $mainColor, Color $secondColor ){ + $mainColor = Color::tryFrom($mainColorValue); + $secondColor = Color::tryFrom($secondColorValue); + if( $mainColor == null || $secondColor == null ){ + $errors[] = self::ERROR_INVALID_COLOR; + } + + if(Validation::hasHTMLInjection($name)){ + $errors[] = self::ERROR_INVALID_NAME; + } + + if(filter_var($picture,FILTER_VALIDATE_URL)){ + $errors[] = self::ERROR_INVALID_PICTURE; + } + + if(empty($errors)){ + $this->gateway->insert($name,$picture,$mainColor,$secondColor); + } + + } + public function list(string $search,array $errors):array { + if(Validation::hasHTMLInjection($search)){ + $errors = self::ERROR_INVALID_SEARCH; + } } } \ No newline at end of file diff --git a/src/Model/Validation.php b/src/Model/Validation.php new file mode 100644 index 0000000..ac732d0 --- /dev/null +++ b/src/Model/Validation.php @@ -0,0 +1,10 @@ +]"); + } +} \ No newline at end of file -- 2.36.3 From d08defaf65a6491f751018b8debea120d0f90df1 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Sun, 12 Nov 2023 23:50:58 +0100 Subject: [PATCH 03/16] Added a view for the creation of a team, started the list by name action --- src/Controller/TeamController.php | 18 ++++--- src/Gateway/TeamGateway.php | 7 ++- src/Model/TeamModel.php | 13 ++++- src/Views/insertTeam.html.twig | 81 +++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 src/Views/insertTeam.html.twig diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 930fe21..65e3f25 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -20,16 +20,22 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ public function submitTeam(array $request){ $errors = []; - if(isset($request['name']) && isset($request["picture"]) && isset($request["mainColor"]) && isset($request["secondColor"])){ - $errors = $this->model->createTeam($request['name'],$request['picture'],$request['mainColor'],$request["secondColor"]); - /*todo gestion des erreurs grace au tableau*/ + $this->model->createTeam($request['name'],$request['picture'],$request['mainColor'],$request["secondColor"],$errors); + if(!empty($errors)){ + /*todo appelle vue avec param*/ + } + } + + public function listTeamByName(array $request){ + $errors = []; + $this->model->listByName($request['name'],$errors); + if(!empty($errors)){ + /*todo appelle vue avec param*/ } else{ - http_response_code(400); - /*todo rappeler vue avec parametre pour signaler les pb */ + /*todo appelle bonne vue*/ } } - } diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 56992d8..17462ea 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -27,7 +27,12 @@ class TeamGateway /* retourne exception par rapport à la validité du paramètr } public function listByName($name): array { - /*todo*/ + return $this->con->fetch( + "SELECT name,picture,mainColor,secondColor FROM Team WHERE name LIKE '% :thing %' ", + [ + ":thing" => [$name, PDO::PARAM_STR] + ] + ); } } \ No newline at end of file diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 3158423..6815ad6 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -16,6 +16,7 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po 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; @@ -49,9 +50,17 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po } - public function list(string $search,array $errors):array { - if(Validation::hasHTMLInjection($search)){ + public function listByName(string $name,array $errors):?array { + if(Validation::hasHTMLInjection($name)){ $errors = self::ERROR_INVALID_SEARCH; } + $results = $this->gateway->listByName($name); + if(empty($results)){ + $errors = self::ERROR_NO_DATA_FOUND; + } + if(!empty($errors)){ + return $results; + } + return null; } } \ No newline at end of file diff --git a/src/Views/insertTeam.html.twig b/src/Views/insertTeam.html.twig new file mode 100644 index 0000000..62f6553 --- /dev/null +++ b/src/Views/insertTeam.html.twig @@ -0,0 +1,81 @@ + + + + + Insertion view + + + + + + + +
+

Créer une équipe

+
+
+ + + + + + + + +
+
+ +
+
+
+ + + \ No newline at end of file -- 2.36.3 From 6ff667e3b8bcecb381f8d5b6f430cda65fa14149 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Mon, 13 Nov 2023 22:12:51 +0100 Subject: [PATCH 04/16] WIP made a diagram class For team within MVC, players aren't incorporated yet --- Documentation/team.puml | 67 ++++++++++++++++++++++++++++++- public/index.php | 2 +- src/Controller/TeamController.php | 2 +- src/Gateway/TeamGateway.php | 2 +- src/Model/TeamModel.php | 2 +- src/Views/insertTeam.html.twig | 3 -- 6 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Documentation/team.puml b/Documentation/team.puml index f775665..b8374cd 100644 --- a/Documentation/team.puml +++ b/Documentation/team.puml @@ -1,3 +1,68 @@ @startuml -/*todo*/ +class Team { + - name: String + - picture: Url + - members: array + + + getName(): String + + getPicture(): Url + + getMainColor(): Color + + getSecondColor(): Color + + listMembers(): array +} + +Team --> "- mainColor" Color +Team --> "- secondaryColor" Color + +class Color { + - value: int + + + getValue(): int +} + +class TeamGateway{ + -- + + __construct(con : Connexion) + + insert(name : string ,picture : string, mainColor : Color, secondColor : Color) + + listByName(name : string): array +} + +TeamGateway *--"- con" Connexion +TeamGateway ..> Color + +class TeamModel{ + + ERROR_INVALID_COLOR : int {readOnly} + + ERROR_INVALID_NAME : int {readOnly} + + ERROR_INVALID_PICTURE : int {readOnly} + + ERROR_INVALID_SEARCH : int {readOnly} + + ERROR_NO_DATA_FOUND : int {readOnly} + --- + + __construct(gateway : TeamGateway) + + createTeam(name : string,picture : string, mainColorValue : int, secondColorValue : int, errors : array) + + listByName(name : string ,errors : array) : ?array +} + +TeamModel *--"- gateway" TeamGateway +TeamModel ..> Team +TeamModel ..> Color + +class TeamController{ + - twig : Environement + -- + + __construct( model : TeamModel, twig : Environement) + + submitTeam(request : array) + + listTeamByName(request : array) +} + +TeamController *--"- model" TeamModel + +class Connexion{ + - pdo : PDO + -- + + __constructor(pdo : PDO) + + exec(query : string, args : array) + + fetch(query string, args array): array + +} + @enduml \ No newline at end of file diff --git a/public/index.php b/public/index.php index 88d90fa..816517d 100644 --- a/public/index.php +++ b/public/index.php @@ -40,7 +40,7 @@ $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); -$teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(),$twig); +$teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(new \App\Gateway\TeamGateway($con)),$twig); $router->map("GET","/team/new", fn()=>$teamController->submitTeam($_POST)); $match = $router->match(); diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 65e3f25..04d88e4 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -28,7 +28,7 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ public function listTeamByName(array $request){ $errors = []; - $this->model->listByName($request['name'],$errors); + $results = $this->model->listByName($request['name'],$errors); if(!empty($errors)){ /*todo appelle vue avec param*/ } diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 17462ea..f492411 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -26,7 +26,7 @@ class TeamGateway /* retourne exception par rapport à la validité du paramètr ); } - public function listByName($name): array { + public function listByName(string $name): array { return $this->con->fetch( "SELECT name,picture,mainColor,secondColor FROM Team WHERE name LIKE '% :thing %' ", [ diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 6815ad6..d356672 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -10,7 +10,7 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po { /** - * Attributes used to + * Attributes used to handle errors */ public const ERROR_INVALID_COLOR = 0; public const ERROR_INVALID_NAME = 1; diff --git a/src/Views/insertTeam.html.twig b/src/Views/insertTeam.html.twig index 62f6553..0f68a9f 100644 --- a/src/Views/insertTeam.html.twig +++ b/src/Views/insertTeam.html.twig @@ -55,9 +55,6 @@ - - -

Créer une équipe

-- 2.36.3 From a283c4b12627feb38ca5cb8c57ea84babed10e43 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Tue, 14 Nov 2023 17:27:03 +0100 Subject: [PATCH 05/16] WIP almost done, added errors handling and more, got some errors with the gateway, i will fix it tomorrow --- public/index.php | 3 +- sql/setup-tables.sql | 14 ++++++-- src/Controller/TeamController.php | 55 +++++++++++++++++++++++-------- src/Data/Team.php | 2 +- src/Gateway/TeamGateway.php | 16 ++++----- src/Model/TeamModel.php | 33 ++++++------------- src/Model/Validation.php | 10 ------ src/Validation/Validators.php | 19 +++++++++++ src/Views/insertTeam.html.twig | 7 ++++ 9 files changed, 100 insertions(+), 59 deletions(-) delete mode 100644 src/Model/Validation.php diff --git a/public/index.php b/public/index.php index 6bc77c4..ae49e19 100644 --- a/public/index.php +++ b/public/index.php @@ -40,7 +40,8 @@ $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); $teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(new \App\Gateway\TeamGateway($con)),$twig); -$router->map("GET","/team/new", fn()=>$teamController->submitTeam($_POST)); +$router->map("GET","/team/new", fn()=>$teamController->displaySubmitTeam()); +$router->map("POST","/team/new", fn()=>$teamController->SubmitTeam($_POST)); $match = $router->match(); diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index be0301a..989f241 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -8,10 +8,20 @@ CREATE TABLE Team( id numeric PRIMARY KEY AUTOINCREMENT, name varchar, picture varchar, - mainColor varchar, - secondColor varchar + mainColor numeric, + secondColor numeric ); +CREATE TABLE Participate( + idTeam numeric FOREIGN KEY REFERENCES Team(id), + idMember numeric FOREIGN KEY REFERENCES Member(id), + role char CHECK (role IN ('C','P')) +); + +CREATE TABLE Member( + id numeric PRIMARY KEY AUTOINCREMENT, + email varchar, +); CREATE TABLE TacticInfo( id integer PRIMARY KEY AUTOINCREMENT, diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 04d88e4..de98d09 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -1,7 +1,17 @@ model = $model; $this->twig = $twig; } - public function submitTeam(array $request){ - $errors = []; - $this->model->createTeam($request['name'],$request['picture'],$request['mainColor'],$request["secondColor"],$errors); - if(!empty($errors)){ - /*todo appelle vue avec param*/ + public function displaySubmitTeam() { + try { + $this->twig->display("insertTeam.html.twig", []); + } catch (LoaderError | RuntimeError | SyntaxError $e) { + echo " twig error : $e"; } } - public function listTeamByName(array $request){ + public function submitTeam(array $request): HttpResponse { + $errors = []; - $results = $this->model->listByName($request['name'],$errors); - if(!empty($errors)){ - /*todo appelle vue avec param*/ - } - else{ - /*todo appelle bonne vue*/ + $request = HttpRequest::from($request, $errors, [ + "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], + "mainColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)], + "secondColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)], + "picture" => [Validators::isURL()] + ]); + if (!empty($errors)) { + $badFields = []; + foreach ($errors as $e) { + if ($e instanceof FieldValidationFail) { + $badFields[] = $e->getFieldName(); + } + } + return ViewHttpResponse::twig('insertTeam.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',[]); + } + + public function listTeamByName(array $request): HttpResponse { + $errors = []; + + $results = $this->model->listByName($request['name'], $errors); + } } diff --git a/src/Data/Team.php b/src/Data/Team.php index 48643d9..bcdabac 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -22,7 +22,7 @@ class Team { * @param Color $secondColor * @param array $members */ - public function __construct(string $name, Url $picture, Color $mainColor, Color $secondColor, array $members) { + public function __construct(string $name, Url $picture, Color $mainColor, Color $secondColor, array $members =[]) { $this->name = $name; $this->picture = $picture; $this->mainColor = $mainColor; diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index f492411..7c5883c 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -6,7 +6,7 @@ use App\Connexion; use App\Data\Color; use PDO; -class TeamGateway /* retourne exception par rapport à la validité du paramètre par ex. un int qui ne peut pas etre <0 doit etre verif etsoulever une exception */ +class TeamGateway { private Connexion $con; @@ -14,23 +14,23 @@ class TeamGateway /* retourne exception par rapport à la validité du paramètr $this->con = $con; } - public function insert(string $name, string $picture, Color $mainColor, Color $secondColor) { + public function insert(string $name, string $picture, int $mainColor, int $secondColor) { $this->con->exec( - "INSERT INTO Team VALUES (:name, :picture, :mainColor, :secondColor)", + "INSERT INTO Team VALUES (:teamName , :picture, :mainColor, :secondColor)", [ - ":name" => [$name, PDO::PARAM_STR], + ":teamName" => [$name, PDO::PARAM_STR], ":picture" => [$picture, PDO::PARAM_STR], - ":mainColor" => [$mainColor, PDO::PARAM_STR], - ":secondColor" => [$secondColor, PDO::PARAM_STR] + ":mainColor" => [$mainColor, PDO::PARAM_INT], + ":secondColor" => [$secondColor, PDO::PARAM_INT] ] ); } public function listByName(string $name): array { return $this->con->fetch( - "SELECT name,picture,mainColor,secondColor FROM Team WHERE name LIKE '% :thing %' ", + "SELECT name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%:match%' ", [ - ":thing" => [$name, PDO::PARAM_STR] + ":match" => [$name, PDO::PARAM_STR] ] ); } diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index d356672..4bcd27e 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -2,6 +2,7 @@ namespace App\Model; use App\Data\Color; use App\Gateway\TeamGateway; +use App\Data\Team; /** * @@ -28,39 +29,25 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po $this->gateway = $gateway; } - public function createTeam(string $name,string $picture,int $mainColorValue, int $secondColorValue, array $errors) { - - $mainColor = Color::tryFrom($mainColorValue); - $secondColor = Color::tryFrom($secondColorValue); - if( $mainColor == null || $secondColor == null ){ - $errors[] = self::ERROR_INVALID_COLOR; - } - - if(Validation::hasHTMLInjection($name)){ - $errors[] = self::ERROR_INVALID_NAME; - } - - if(filter_var($picture,FILTER_VALIDATE_URL)){ - $errors[] = self::ERROR_INVALID_PICTURE; - } - - if(empty($errors)){ - $this->gateway->insert($name,$picture,$mainColor,$secondColor); - } + public function createTeam(string $name,string $picture,int $mainColor, int $secondColor) { + $this->gateway->insert($name,$picture,$mainColor,$secondColor); } - public function listByName(string $name,array $errors):?array { + public function listByName(string $name,array &$errors):array { + $teams=[]; if(Validation::hasHTMLInjection($name)){ $errors = self::ERROR_INVALID_SEARCH; } $results = $this->gateway->listByName($name); + if(empty($results)){ $errors = self::ERROR_NO_DATA_FOUND; } - if(!empty($errors)){ - return $results; + + foreach ($results as $row){ + $teams[] = new Team($row['name'],$row['picture'],$row['mainColor'],$row['secondColor']); } - return null; + return $results; } } \ No newline at end of file diff --git a/src/Model/Validation.php b/src/Model/Validation.php deleted file mode 100644 index ac732d0..0000000 --- a/src/Model/Validation.php +++ /dev/null @@ -1,10 +0,0 @@ -]"); - } -} \ No newline at end of file diff --git a/src/Validation/Validators.php b/src/Validation/Validators.php index ea9da46..2f69ca4 100644 --- a/src/Validation/Validators.php +++ b/src/Validation/Validators.php @@ -51,4 +51,23 @@ class Validators { } ); } + + public static function isInteger(): Validator { + return self::regex("/^[0-9]+$/"); + } + + public static function isIntInRange(int $min,int $max): Validator { + return new SimpleFunctionValidator( + fn(string $val) => intval($val) >= $min && intval($val) <= $max, + fn(string $name) => [new FieldValidationFail($name, "The value is not in the range $min to $max ")] + ); + } + + public static function isURL(): Validator { + return new SimpleFunctionValidator( + fn($val) => filter_var($val, FILTER_VALIDATE_URL) , + fn(string $name) => [new FieldValidationFail($name, "The value is not an URL")] + ); + } + } \ No newline at end of file diff --git a/src/Views/insertTeam.html.twig b/src/Views/insertTeam.html.twig index 0f68a9f..ba6ffac 100644 --- a/src/Views/insertTeam.html.twig +++ b/src/Views/insertTeam.html.twig @@ -31,6 +31,12 @@ margin-bottom: 5px; } + {% for item in bad_fields %} + #{{ item }}{ + border-color: red; + } + {% endfor %} + input[type="text"], input[type="password"] { width: 100%; padding: 10px; @@ -51,6 +57,7 @@ background-color: #0056b3; } + -- 2.36.3 From 0859467e3fdda12a5cef0e62aba7e1a089b04277 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Tue, 14 Nov 2023 22:58:21 +0100 Subject: [PATCH 06/16] WIP just a few little adjusments and progress --- public/index.php | 3 + sql/setup-tables.sql | 4 +- src/Controller/TeamController.php | 28 +++++-- src/Model/TeamModel.php | 11 +-- ...rtTeam.html.twig => insert_team.html.twig} | 0 src/Views/list_team_by_name.html.twig | 77 +++++++++++++++++++ 6 files changed, 106 insertions(+), 17 deletions(-) rename src/Views/{insertTeam.html.twig => insert_team.html.twig} (100%) create mode 100644 src/Views/list_team_by_name.html.twig diff --git a/public/index.php b/public/index.php index ae49e19..bdb9000 100644 --- a/public/index.php +++ b/public/index.php @@ -42,6 +42,9 @@ $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);*/ $match = $router->match(); diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 989f241..437649c 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -5,7 +5,7 @@ DROP TABLE IF EXISTS TacticInfo; CREATE TABLE FormEntries(name varchar, description varchar); CREATE TABLE Team( - id numeric PRIMARY KEY AUTOINCREMENT, + id integer PRIMARY KEY AUTOINCREMENT, name varchar, picture varchar, mainColor numeric, @@ -19,7 +19,7 @@ CREATE TABLE Participate( ); CREATE TABLE Member( - id numeric PRIMARY KEY AUTOINCREMENT, + id integer PRIMARY KEY AUTOINCREMENT, email varchar, ); diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index de98d09..8702b3e 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -29,7 +29,7 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ public function displaySubmitTeam() { try { - $this->twig->display("insertTeam.html.twig", []); + $this->twig->display("insert_team.html.twig", []); } catch (LoaderError | RuntimeError | SyntaxError $e) { echo " twig error : $e"; } @@ -51,17 +51,33 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ $badFields[] = $e->getFieldName(); } } - return ViewHttpResponse::twig('insertTeam.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',[]); + 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 listTeamByName(array $request): HttpResponse { $errors = []; - - $results = $this->model->listByName($request['name'], $errors); - + $request = HttpRequest::from($request, $errors, [ + "name" => [Validators::lenBetween(1,32),Validators::nameWithSpaces()] + ]); + if(!empty($errors) && $errors[0] instanceof FieldValidationFail){ + $badField = $errors[0]->getFieldName(); + 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*/ + } + return ViewHttpResponse::twig('display_teams.html.twig',['teams' => $results]); } } diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 4bcd27e..02eef70 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -34,20 +34,13 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po } - public function listByName(string $name,array &$errors):array { + public function listByName(string $name):array { $teams=[]; - if(Validation::hasHTMLInjection($name)){ - $errors = self::ERROR_INVALID_SEARCH; - } $results = $this->gateway->listByName($name); - if(empty($results)){ - $errors = self::ERROR_NO_DATA_FOUND; - } - foreach ($results as $row){ $teams[] = new Team($row['name'],$row['picture'],$row['mainColor'],$row['secondColor']); } - return $results; + return $teams; } } \ No newline at end of file diff --git a/src/Views/insertTeam.html.twig b/src/Views/insert_team.html.twig similarity index 100% rename from src/Views/insertTeam.html.twig rename to src/Views/insert_team.html.twig diff --git a/src/Views/list_team_by_name.html.twig b/src/Views/list_team_by_name.html.twig new file mode 100644 index 0000000..7e0cbb3 --- /dev/null +++ b/src/Views/list_team_by_name.html.twig @@ -0,0 +1,77 @@ + + + + + Insertion view + + + + +
+

Créer une équipe

+ +
+ + +
+
+ +
+ +
+ + + \ No newline at end of file -- 2.36.3 From 30611b9b21b0b9dad948fdfb5f33dabb12fdadfc Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Wed, 15 Nov 2023 15:21:17 +0100 Subject: [PATCH 07/16] WIP done with insertion, got a problem on list team and starting displayTeam --- public/index.php | 4 ++- src/Controller/TeamController.php | 35 ++++++++++++--------------- src/Data/Team.php | 11 ++++++++- src/Gateway/TeamGateway.php | 28 ++++++++++++++++----- src/Model/TeamModel.php | 20 ++++++--------- src/Views/display_team.html.twig | 35 +++++++++++++++++++++++++++ src/Views/display_teams.html.twig | 35 +++++++++++++++++++++++++++ src/Views/list_team_by_name.html.twig | 2 +- 8 files changed, 129 insertions(+), 41 deletions(-) create mode 100644 src/Views/display_team.html.twig create mode 100644 src/Views/display_teams.html.twig 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

-- 2.36.3 From 1acede41693ad7769df3247d7ba85ce00dfc38b9 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Thu, 16 Nov 2023 13:17:46 +0100 Subject: [PATCH 08/16] WIP did a few little changes --- src/Controller/TeamController.php | 3 ++- src/Data/Member.php | 13 ++++++++++- src/Gateway/TeamGateway.php | 2 +- src/Model/TeamModel.php | 37 +++++++++++++++++++++---------- src/Views/display_team.html.twig | 28 +++++------------------ src/Views/display_teams.html.twig | 2 -- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 8228d6a..ffbcb8e 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -74,7 +74,8 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ } public function displayTeam(int $id): HttpResponse{ - $results = $this->model->displayTeam($id); + $result = $this->model->displayTeam($id); + return ViewHttpResponse::twig('display_team.html.twig',['team' => $result]); } } diff --git a/src/Data/Member.php b/src/Data/Member.php index 91b09c4..9bf0b52 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -11,6 +11,7 @@ class Member { */ private int $userId; + private string $email; /** * @var MemberRole the member's role */ @@ -18,13 +19,16 @@ class Member { /** * @param int $userId + * @param string $email * @param MemberRole $role */ - public function __construct(int $userId, MemberRole $role) { + public function __construct(int $userId, string $email, MemberRole $role) { $this->userId = $userId; + $this->email = $email; $this->role = $role; } + /** * @return int */ @@ -39,4 +43,11 @@ class Member { return $this->role; } + /** + * @return string + */ + public function getEmail(): string { + return $this->email; + } + } \ No newline at end of file diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 7d9c014..1540c73 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -44,7 +44,7 @@ class TeamGateway { 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", + "SELECT p.role,m.email,m.id 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] ] diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 30c9fde..8c5c8f5 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -1,8 +1,11 @@ gateway = $gateway; } - public function createTeam(string $name,string $picture,int $mainColor, int $secondColor) { - $this->gateway->insert($name,$picture,$mainColor,$secondColor); + 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=[]; + public function listByName(string $name): array { + $teams = []; $results = $this->gateway->listByName($name); - foreach ($results as $row){ - $teams[] = new Team($row['id'],$row['name'],$row['picture'],$row['mainColor'],$row['secondColor']); + foreach ($results as $row) { + $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); + public function displayTeam(int $id): Team { + $members = []; + $result = $this->gateway->getTeamById($id); $resultMembers = $this->gateway->getMembersById($id); - + foreach ($resultMembers as $row) { + if ($row['role'] == 'C') { + $role = 1; + } else { + $role = 2; + } + $members[] = new Member($row['id'], $row['email'], $role); + } + $team = new Team($result['id'], $result['name'], $result['picture'], $result['mainColor'], $result['secondColor'], $members); + return $team; } + } \ No newline at end of file diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index a1f88fc..d06322f 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -6,30 +6,12 @@ -

Hello world

+

{{ team.name }}

-{% 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 %} + +{% for m in team.members %} +

m.email

+{% endfor %} \ No newline at end of file diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig index a1f88fc..d0c7c22 100644 --- a/src/Views/display_teams.html.twig +++ b/src/Views/display_teams.html.twig @@ -6,8 +6,6 @@ -

Hello world

- {% if teams is empty %}

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

-- 2.36.3 From 9bc03c099b33a3b4849f6318f6620425e9d48eaf Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Sun, 19 Nov 2023 22:42:30 +0100 Subject: [PATCH 09/16] WIP did a few little changes, add colorpicker for the creation of the team, still have to make changes in regard to it --- sql/setup-tables.sql | 16 +++++++++------- src/Controller/TeamController.php | 15 +++++++++------ src/Data/Member.php | 13 +------------ src/Gateway/TeamGateway.php | 7 +++++++ src/Model/TeamModel.php | 12 +++++++----- src/Views/display_teams.html.twig | 4 ++-- src/Views/insert_team.html.twig | 4 ++-- 7 files changed, 37 insertions(+), 34 deletions(-) diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 437649c..c2e8ec4 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -1,6 +1,9 @@ -- drop tables here DROP TABLE IF EXISTS FormEntries; DROP TABLE IF EXISTS TacticInfo; +DROP TABLE IF EXISTS Team; +DROP TABLE IF EXISTS Member; +DROP TABLE IF EXISTS Participate; CREATE TABLE FormEntries(name varchar, description varchar); @@ -12,15 +15,14 @@ CREATE TABLE Team( secondColor numeric ); -CREATE TABLE Participate( - idTeam numeric FOREIGN KEY REFERENCES Team(id), - idMember numeric FOREIGN KEY REFERENCES Member(id), - role char CHECK (role IN ('C','P')) +CREATE TABLE Member( + id integer PRIMARY KEY AUTOINCREMENT ); -CREATE TABLE Member( - id integer PRIMARY KEY AUTOINCREMENT, - email varchar, +CREATE TABLE Participate( + idTeam integer FOREIGN KEY REFERENCES Team(id), + idMember integer FOREIGN KEY REFERENCES Member(id), + role char CHECK (role IN ('C','P')) ); CREATE TABLE TacticInfo( diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index ffbcb8e..e2f0b89 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -9,9 +9,6 @@ use App\Model\TeamModel; use App\Validation\FieldValidationFail; use App\Validation\Validators; use \Twig\Environment; -use Twig\Error\LoaderError; -use Twig\Error\RuntimeError; -use Twig\Error\SyntaxError; class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ { @@ -32,7 +29,9 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ } public function submitTeam(array $request): HttpResponse { - + $request['mainColor'] = intval($request['mainColor']); + $request['secondColor'] = intval($request['secondColor']); + var_dump($request['secondColor']); $errors = []; $request = HttpRequest::from($request, $errors, [ "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], @@ -49,8 +48,8 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ } 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*/ + $id = $this->model->createTeam($request['name'], $request['picture'], $request['mainColor'], $request['secondColor']); + return $this->displayTeam($id); } public function displayListTeamByName(): HttpResponse { @@ -62,14 +61,18 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ $request = HttpRequest::from($request, $errors, [ "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()] ]); + if (!empty($errors) && $errors[0] instanceof FieldValidationFail) { $badField = $errors[0]->getFieldName(); return ViewHttpResponse::twig('list_team_by_name.html.twig', ['bad_field' => $badField]); } + $results = $this->model->listByName($request['name']); + if (empty($results)) { return ViewHttpResponse::twig('display_teams.html.twig', []); } + return ViewHttpResponse::twig('display_teams.html.twig', ['teams' => $results]); } diff --git a/src/Data/Member.php b/src/Data/Member.php index 9bf0b52..bfdb7a8 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -11,7 +11,6 @@ class Member { */ private int $userId; - private string $email; /** * @var MemberRole the member's role */ @@ -19,12 +18,10 @@ class Member { /** * @param int $userId - * @param string $email * @param MemberRole $role */ - public function __construct(int $userId, string $email, MemberRole $role) { + public function __construct(int $userId, MemberRole $role) { $this->userId = $userId; - $this->email = $email; $this->role = $role; } @@ -42,12 +39,4 @@ class Member { public function getRole(): MemberRole { return $this->role; } - - /** - * @return string - */ - public function getEmail(): string { - return $this->email; - } - } \ No newline at end of file diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 1540c73..0f8fa74 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -24,6 +24,13 @@ class TeamGateway { ); } + public function getIdOfLastInsertion(): array{ + return $this->con->fetch( + "SELECT last_insert_rowid() as id", + [] + ); + } + public function listByName(string $name): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 8c5c8f5..17b02dd 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -6,6 +6,7 @@ use App\Gateway\TeamGateway; use App\Data\Team; use App\Data\Member; use App\Data\MemberRole; +use http\Url; /** * @@ -21,15 +22,17 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po $this->gateway = $gateway; } - public function createTeam(string $name, string $picture, int $mainColor, int $secondColor) { + public function createTeam(string $name, string $picture, int $mainColor, int $secondColor): int{ $this->gateway->insert($name, $picture, $mainColor, $secondColor); + return (int) $this->gateway->getIdOfLastInsertion()['id']; } public function listByName(string $name): array { $teams = []; $results = $this->gateway->listByName($name); foreach ($results as $row) { - $teams[] = new Team($row['id'], $row['name'], $row['picture'], $row['mainColor'], $row['secondColor']); + $url = new Url($row['picture']); + $teams[] = new Team($row['id'], $row['name'], $url, $row['mainColor'], $row['secondColor']); } return $teams; } @@ -44,10 +47,9 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po } else { $role = 2; } - $members[] = new Member($row['id'], $row['email'], $role); + $members[] = new Member($row['id'], $role); } - $team = new Team($result['id'], $result['name'], $result['picture'], $result['mainColor'], $result['secondColor'], $members); - return $team; + return new Team($result['id'], $result['name'], $result['picture'], $result['mainColor'], $result['secondColor'], $members); } } \ No newline at end of file diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig index d0c7c22..e4941e4 100644 --- a/src/Views/display_teams.html.twig +++ b/src/Views/display_teams.html.twig @@ -22,8 +22,8 @@
{% else %} {% for t in teams %} -
-

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

+
+

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

picture : {{ t.picture }}

{% endfor %} diff --git a/src/Views/insert_team.html.twig b/src/Views/insert_team.html.twig index ba6ffac..0cc85dd 100644 --- a/src/Views/insert_team.html.twig +++ b/src/Views/insert_team.html.twig @@ -71,9 +71,9 @@ - + - +
-- 2.36.3 From 46862bb7cd91b3e3e060cfb1f5f06d0749dd721b Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Mon, 20 Nov 2023 09:53:53 +0100 Subject: [PATCH 10/16] WIP fixed some bugs, still got two regarding /team/id and /team/list --- sql/setup-tables.sql | 8 +++++--- src/Controller/TeamController.php | 14 +++++++------- src/Data/MemberRole.php | 13 +++++++++++-- src/Gateway/TeamGateway.php | 16 +++++++++------- src/Model/TeamModel.php | 7 ++++--- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index c2e8ec4..1366a87 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -20,9 +20,11 @@ CREATE TABLE Member( ); CREATE TABLE Participate( - idTeam integer FOREIGN KEY REFERENCES Team(id), - idMember integer FOREIGN KEY REFERENCES Member(id), - role char CHECK (role IN ('C','P')) + idTeam integer, + idMember integer, + role char(1) CHECK (role IN ('C','P')), + FOREIGN KEY (idTeam) REFERENCES Team(id), + FOREIGN KEY (idMember) REFERENCES Member(id) ); CREATE TABLE TacticInfo( diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index e2f0b89..30a6ab6 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -29,10 +29,11 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ } public function submitTeam(array $request): HttpResponse { - $request['mainColor'] = intval($request['mainColor']); - $request['secondColor'] = intval($request['secondColor']); - var_dump($request['secondColor']); + $errors = []; + $request['mainColor'] = hexdec($request['mainColor']); + $request['secondColor'] = hexdec($request['secondColor']); + $request = HttpRequest::from($request, $errors, [ "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], "mainColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)], @@ -48,8 +49,7 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ } return ViewHttpResponse::twig('insert_team.html.twig', ['bad_fields' => $badFields]); } - $id = $this->model->createTeam($request['name'], $request['picture'], $request['mainColor'], $request['secondColor']); - return $this->displayTeam($id); + return $this->displayTeam($this->model->createTeam($request['name'], $request['picture'], $request['mainColor'], $request['secondColor'])); } public function displayListTeamByName(): HttpResponse { @@ -76,9 +76,9 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ return ViewHttpResponse::twig('display_teams.html.twig', ['teams' => $results]); } - public function displayTeam(int $id): HttpResponse{ + public function displayTeam(int $id): HttpResponse { $result = $this->model->displayTeam($id); - return ViewHttpResponse::twig('display_team.html.twig',['team' => $result]); + return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); } } diff --git a/src/Data/MemberRole.php b/src/Data/MemberRole.php index 05d746d..bd8add3 100755 --- a/src/Data/MemberRole.php +++ b/src/Data/MemberRole.php @@ -11,8 +11,9 @@ use http\Exception\InvalidArgumentException; * encapsulates an integer value and use it as an enumeration discriminant */ final class MemberRole { - private const ROLE_PLAYER = 0; - private const ROLE_COACH = 1; + + public const ROLE_PLAYER = 0; + public const ROLE_COACH = 1; private const MIN = self::ROLE_PLAYER; private const MAX = self::ROLE_COACH; @@ -25,6 +26,14 @@ final class MemberRole { $this->value = $val; } + public static function player(){ + return new MemberRole(MemberRole::ROLE_PLAYER); + } + + public static function coach(){ + return new MemberRole(MemberRole::ROLE_COACH); + } + private function isValid(int $val): bool { return ($val <= self::MAX and $val >= self::MIN); } diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 0f8fa74..4bb1518 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -24,13 +24,6 @@ class TeamGateway { ); } - public function getIdOfLastInsertion(): array{ - return $this->con->fetch( - "SELECT last_insert_rowid() as id", - [] - ); - } - public function listByName(string $name): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", @@ -49,6 +42,15 @@ class TeamGateway { ); } + public function getIdTeamByName(string $name): array{ + return $this->con->fetch( + "SELECT id FROM Team WHERE name = :name", + [ + ":name" => [$name, PDO::PARAM_STR] + ] + ); + } + public function getMembersById($id):array{ return $this->con->fetch( "SELECT p.role,m.email,m.id FROM Member m,Team t,Participate p WHERE t.id = :id AND p.idTeam = t.id AND p.idMember = m.id", diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 17b02dd..1dd57a0 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -24,7 +24,8 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po public function createTeam(string $name, string $picture, int $mainColor, int $secondColor): int{ $this->gateway->insert($name, $picture, $mainColor, $secondColor); - return (int) $this->gateway->getIdOfLastInsertion()['id']; + $result = $this->gateway->getIdTeamByName($name); + return intval($result[0]['id']); } public function listByName(string $name): array { @@ -43,9 +44,9 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po $resultMembers = $this->gateway->getMembersById($id); foreach ($resultMembers as $row) { if ($row['role'] == 'C') { - $role = 1; + $role = MemberRole::coach(); } else { - $role = 2; + $role = MemberRole::player(); } $members[] = new Member($row['id'], $role); } -- 2.36.3 From d824f17ea75f277d3a70b90c21eaf839c8dc861d Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Tue, 21 Nov 2023 11:36:25 +0100 Subject: [PATCH 11/16] every bugs has been fixed --- .gitignore | 2 +- sql/setup-tables.sql | 12 ++++++------ src/Controller/TeamController.php | 6 ++---- src/Data/Color.php | 22 +++++++++++----------- src/Data/Team.php | 12 +++++------- src/Gateway/TeamGateway.php | 10 +++++----- src/Model/TeamModel.php | 17 ++++++----------- src/Validation/Validators.php | 1 - src/Views/display_team.html.twig | 19 +++++++++++++++++-- src/Views/display_teams.html.twig | 2 +- 10 files changed, 54 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 9124809..b116c4f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ .vite vendor - +.nfs* composer.lock *.phar /dist diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 1366a87..4b9f10b 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -2,8 +2,8 @@ DROP TABLE IF EXISTS FormEntries; DROP TABLE IF EXISTS TacticInfo; DROP TABLE IF EXISTS Team; +DROP TABLE IF EXISTS User; DROP TABLE IF EXISTS Member; -DROP TABLE IF EXISTS Participate; CREATE TABLE FormEntries(name varchar, description varchar); @@ -11,20 +11,20 @@ CREATE TABLE Team( id integer PRIMARY KEY AUTOINCREMENT, name varchar, picture varchar, - mainColor numeric, - secondColor numeric + mainColor varchar, + secondColor varchar ); -CREATE TABLE Member( +CREATE TABLE User( id integer PRIMARY KEY AUTOINCREMENT ); -CREATE TABLE Participate( +CREATE TABLE Member( idTeam integer, idMember integer, role char(1) CHECK (role IN ('C','P')), FOREIGN KEY (idTeam) REFERENCES Team(id), - FOREIGN KEY (idMember) REFERENCES Member(id) + FOREIGN KEY (idMember) REFERENCES User(id) ); CREATE TABLE TacticInfo( diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 30a6ab6..fa418c5 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -31,13 +31,11 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ public function submitTeam(array $request): HttpResponse { $errors = []; - $request['mainColor'] = hexdec($request['mainColor']); - $request['secondColor'] = hexdec($request['secondColor']); $request = HttpRequest::from($request, $errors, [ "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], - "mainColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)], - "secondColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)], + "mainColor" => [Validators::regex('/#(?:[0-9a-fA-F]{6})/')], + "secondColor" => [Validators::regex('/#(?:[0-9a-fA-F]{6})/')], "picture" => [Validators::isURL()] ]); if (!empty($errors)) { diff --git a/src/Data/Color.php b/src/Data/Color.php index bc9043b..2e934c0 100755 --- a/src/Data/Color.php +++ b/src/Data/Color.php @@ -2,39 +2,39 @@ namespace App\Data; -use http\Exception\InvalidArgumentException; +use \InvalidArgumentException; class Color { /** - * @var int 6 bytes unsigned int that represents an RGB color + * @var string 6 bytes unsigned int that represents an RGB color */ - private int $value; + private string $hex; /** - * @param int $value 6 bytes unsigned int that represents an RGB color + * @param string $value 6 bytes unsigned int that represents an RGB color * @throws \InvalidArgumentException if the value is negative or greater than 0xFFFFFF */ - private function __constructor(int $value) { + private function __construct(string $value) { $this->value = $value; } /** - * @return int + * @return string */ - public function getValue(): int { + public function getValue(): string { return $this->value; } - public static function from(int $value): Color { + public static function from(string $value): Color { $color = self::tryFrom($value); if ($color == null) { - throw new InvalidArgumentException("int color value is invalid, must be positive and lower than 0xFFFFFF"); + throw new InvalidArgumentException("The string is not an hexadecimal code"); } return $color; } - public static function tryFrom(int $value): ?Color { - if ($value < 0 || $value > 0xFFFFFF) { + public static function tryFrom(string $value): ?Color { + if (!preg_match('/#(?:[0-9a-fA-F]{6})/',$value)) { return null; } return new Color($value); diff --git a/src/Data/Team.php b/src/Data/Team.php index 321a5d9..4c80dc6 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -2,12 +2,10 @@ namespace App\Data; -use http\Url; - class Team { private int $id; private string $name; - private Url $picture; + private string $picture; private Color $mainColor; private Color $secondColor; @@ -18,12 +16,12 @@ class Team { /** * @param string $name - * @param Url $picture + * @param string $picture * @param Color $mainColor * @param Color $secondColor * @param array $members */ - public function __construct(int $id,string $name, Url $picture, Color $mainColor, Color $secondColor, array $members =[]) { + public function __construct(int $id,string $name, string $picture, Color $mainColor, Color $secondColor, array $members =[]) { $this->id = $id; $this->name = $name; $this->picture = $picture; @@ -47,9 +45,9 @@ class Team { } /** - * @return Url + * @return string */ - public function getPicture(): Url { + public function getPicture(): string { return $this->picture; } diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 4bb1518..1cecbe1 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -12,14 +12,14 @@ class TeamGateway { $this->con = $con; } - public function insert(string $name, string $picture, int $mainColor, int $secondColor) { + public function insert(string $name, string $picture, string $mainColor, string $secondColor) { $this->con->exec( "INSERT INTO Team(name, picture, mainColor, secondColor) VALUES (:teamName , :picture, :mainColor, :secondColor)", [ ":teamName" => [$name, PDO::PARAM_STR], ":picture" => [$picture, PDO::PARAM_STR], - ":mainColor" => [$mainColor, PDO::PARAM_INT], - ":secondColor" => [$secondColor, PDO::PARAM_INT] + ":mainColor" => [$mainColor, PDO::PARAM_STR], + ":secondColor" => [$secondColor, PDO::PARAM_STR] ] ); } @@ -35,7 +35,7 @@ class TeamGateway { public function getTeamById(int $id): array{ return $this->con->fetch( - "SELECT name,picture,mainColor,secondColor FROM Team WHERE id = :id", + "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE id = :id", [ ":id" => [$id, PDO::PARAM_INT] ] @@ -53,7 +53,7 @@ class TeamGateway { public function getMembersById($id):array{ return $this->con->fetch( - "SELECT p.role,m.email,m.id FROM Member m,Team t,Participate p WHERE t.id = :id AND p.idTeam = t.id AND p.idMember = m.id", + "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] ] diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index 1dd57a0..c5521eb 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -6,12 +6,9 @@ use App\Gateway\TeamGateway; use App\Data\Team; use App\Data\Member; use App\Data\MemberRole; -use http\Url; +use App\Data\Color; -/** - * - */ -class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) pour le controller qui l'utilise, catch celle de la gw, */ +class TeamModel { private TeamGateway $gateway; @@ -22,7 +19,7 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po $this->gateway = $gateway; } - public function createTeam(string $name, string $picture, int $mainColor, int $secondColor): int{ + public function createTeam(string $name, string $picture, string $mainColor, string $secondColor): int { $this->gateway->insert($name, $picture, $mainColor, $secondColor); $result = $this->gateway->getIdTeamByName($name); return intval($result[0]['id']); @@ -32,15 +29,14 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po $teams = []; $results = $this->gateway->listByName($name); foreach ($results as $row) { - $url = new Url($row['picture']); - $teams[] = new Team($row['id'], $row['name'], $url, $row['mainColor'], $row['secondColor']); + $teams[] = new Team($row['id'], $row['name'], $row['picture'], Color::from($row['mainColor']), Color::from($row['secondColor'])); } return $teams; } public function displayTeam(int $id): Team { $members = []; - $result = $this->gateway->getTeamById($id); + $result = $this->gateway->getTeamById($id)[0]; $resultMembers = $this->gateway->getMembersById($id); foreach ($resultMembers as $row) { if ($row['role'] == 'C') { @@ -50,7 +46,6 @@ class TeamModel /* throw des exceptions(ex validation des champs, filtre etc) po } $members[] = new Member($row['id'], $role); } - return new Team($result['id'], $result['name'], $result['picture'], $result['mainColor'], $result['secondColor'], $members); + return new Team(intval($result['id']), $result['name'], $result['picture'], Color::from($result['mainColor']), Color::from($result['secondColor']), $members); } - } \ No newline at end of file diff --git a/src/Validation/Validators.php b/src/Validation/Validators.php index 2f69ca4..a36bbef 100644 --- a/src/Validation/Validators.php +++ b/src/Validation/Validators.php @@ -69,5 +69,4 @@ class Validators { fn(string $name) => [new FieldValidationFail($name, "The value is not an URL")] ); } - } \ No newline at end of file diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index d06322f..e3b0fe0 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -3,14 +3,29 @@ Twig view +

{{ team.name }}

- +Logo d'équipe +
+
{% for m in team.members %} -

m.email

+

m.id

{% endfor %} diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig index e4941e4..c0ac185 100644 --- a/src/Views/display_teams.html.twig +++ b/src/Views/display_teams.html.twig @@ -24,7 +24,7 @@ {% for t in teams %}

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

-

picture : {{ t.picture }}

+ logo de l'équipe
{% endfor %} {% endif %} -- 2.36.3 From 1b9a873cdce83db0b6f91e246ae72b7b7035d09a Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Tue, 21 Nov 2023 23:24:20 +0100 Subject: [PATCH 12/16] Updated documentation + started to add some documentation --- Documentation/team.puml | 24 ++++----- src/Controller/TeamController.php | 4 +- src/Views/display_team.html.twig | 84 ++++++++++++++++++++----------- 3 files changed, 69 insertions(+), 43 deletions(-) diff --git a/Documentation/team.puml b/Documentation/team.puml index b8374cd..a291b1d 100644 --- a/Documentation/team.puml +++ b/Documentation/team.puml @@ -12,12 +12,14 @@ class Team { } Team --> "- mainColor" Color -Team --> "- secondaryColor" Color +Team --> "- secondColor" Color class Color { - - value: int - - + getValue(): int + - value: string + - __construct(value : string) + + getValue(): string + + from(value: string): Color + + tryFrom(value : string) : ?Color } class TeamGateway{ @@ -31,15 +33,11 @@ TeamGateway *--"- con" Connexion TeamGateway ..> Color class TeamModel{ - + ERROR_INVALID_COLOR : int {readOnly} - + ERROR_INVALID_NAME : int {readOnly} - + ERROR_INVALID_PICTURE : int {readOnly} - + ERROR_INVALID_SEARCH : int {readOnly} - + ERROR_NO_DATA_FOUND : int {readOnly} --- + __construct(gateway : TeamGateway) + createTeam(name : string,picture : string, mainColorValue : int, secondColorValue : int, errors : array) + listByName(name : string ,errors : array) : ?array + + displayTeam(id : int): Team } TeamModel *--"- gateway" TeamGateway @@ -50,8 +48,11 @@ class TeamController{ - twig : Environement -- + __construct( model : TeamModel, twig : Environement) - + submitTeam(request : array) - + listTeamByName(request : array) + + displaySubmitTeam() : HttpResponse + + submitTeam(request : array) : HttpResponse + + displayListTeamByName(): HttpResponse + + listTeamByName(request : array) : HttpResponse + + displayTeam(id : int): HttpResponse } TeamController *--"- model" TeamModel @@ -62,7 +63,6 @@ class Connexion{ + __constructor(pdo : PDO) + exec(query : string, args : array) + fetch(query string, args array): array - } @enduml \ No newline at end of file diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index fa418c5..288e733 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -10,7 +10,7 @@ use App\Validation\FieldValidationFail; use App\Validation\Validators; use \Twig\Environment; -class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ +class TeamController { private TeamModel $model; private Environment $twig; @@ -24,7 +24,7 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/ $this->twig = $twig; } - public function displaySubmitTeam() { + public function displaySubmitTeam() : HttpResponse { return ViewHttpResponse::twig("insert_team.html.twig", []); } diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index e3b0fe0..f7bb02b 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -1,32 +1,58 @@ - - - Twig view - - - - -

{{ team.name }}

-Logo d'équipe -
-
- -{% for m in team.members %} -

m.id

-{% endfor %} - - + + + Twig view + + + +
+

IQBall

+
+ +
+ +
+

{{ team.name }}

+ Logo d'équipe +
+
+ + {% for m in team.members %} +

m.id

+ {% endfor %} +
+ +
+ \ No newline at end of file -- 2.36.3 From 262fa6180e9ea0b6ba992fdf86db58f23007a10d Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Wed, 22 Nov 2023 08:40:15 +0100 Subject: [PATCH 13/16] applied suggestions --- public/index.php | 2 +- src/Controller/TeamController.php | 10 +++------- src/Data/Color.php | 1 + src/Data/MemberRole.php | 4 ++-- src/Views/display_team.html.twig | 7 ++++++- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/public/index.php b/public/index.php index cc63837..abc1a50 100644 --- a/public/index.php +++ b/public/index.php @@ -46,7 +46,7 @@ $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)); -$router->map("GET","/team/[i:id]", fn(int $id)=>$teamController->displayTeam($id)); +$router->map("GET","/team/[i:id]", fn(int $id)=>$teamController->getTeam($id)); $match = $router->match(); diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index 288e733..d4c7bfe 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -13,15 +13,12 @@ use \Twig\Environment; 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 { @@ -29,7 +26,6 @@ class TeamController } public function submitTeam(array $request): HttpResponse { - $errors = []; $request = HttpRequest::from($request, $errors, [ @@ -47,7 +43,7 @@ class TeamController } return ViewHttpResponse::twig('insert_team.html.twig', ['bad_fields' => $badFields]); } - return $this->displayTeam($this->model->createTeam($request['name'], $request['picture'], $request['mainColor'], $request['secondColor'])); + return $this->getTeam($this->model->createTeam($request['name'], $request['picture'], $request['mainColor'], $request['secondColor'])); } public function displayListTeamByName(): HttpResponse { @@ -74,7 +70,7 @@ class TeamController return ViewHttpResponse::twig('display_teams.html.twig', ['teams' => $results]); } - public function displayTeam(int $id): HttpResponse { + public function getTeam(int $id): HttpResponse { $result = $this->model->displayTeam($id); return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); } diff --git a/src/Data/Color.php b/src/Data/Color.php index 2e934c0..d32d81f 100755 --- a/src/Data/Color.php +++ b/src/Data/Color.php @@ -28,6 +28,7 @@ class Color { public static function from(string $value): Color { $color = self::tryFrom($value); if ($color == null) { + var_dump($value); throw new InvalidArgumentException("The string is not an hexadecimal code"); } return $color; diff --git a/src/Data/MemberRole.php b/src/Data/MemberRole.php index bd8add3..3bc9d56 100755 --- a/src/Data/MemberRole.php +++ b/src/Data/MemberRole.php @@ -12,8 +12,8 @@ use http\Exception\InvalidArgumentException; */ final class MemberRole { - public const ROLE_PLAYER = 0; - public const ROLE_COACH = 1; + private const ROLE_PLAYER = 0; + private const ROLE_COACH = 1; private const MIN = self::ROLE_PLAYER; private const MAX = self::ROLE_COACH; diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index f7bb02b..9783fe2 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -8,10 +8,11 @@ background-color: #f1f1f1; display: flex; flex-direction: column; + align-items: center; } section{ - width: 80%; + width: 60%; } .square{ @@ -32,6 +33,10 @@ .team{ border-color: darkgrey; border-radius: 20px; + align-items: center; + } + .team.h1{ + } -- 2.36.3 From 65151cc1eacf00bea776f3c8666536c9c7846770 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Wed, 22 Nov 2023 09:02:30 +0100 Subject: [PATCH 14/16] add some css --- src/Views/display_team.html.twig | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index 9783fe2..732fd19 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -29,14 +29,27 @@ .container{ background-color: #fff; + display: flex; + flex-direction: column; + align-items: center; } .team{ border-color: darkgrey; border-radius: 20px; - align-items: center; + } - .team.h1{ + .colors{ + justify-content: space-between; + } + .color{ + flex-direction: row; + justify-content: space-between; + } + + .logo{ + height: 80px; + width: 80px; } @@ -47,11 +60,16 @@
-
-

{{ team.name }}

- Logo d'équipe -
-
+
+
+

{{ team.name }}

+ +
+
+

Couleur principale :

+

Couleur secondaire :

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

m.id

-- 2.36.3 From 76e64a5ae5511f7f1198358f6fb67564225558de Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Wed, 22 Nov 2023 12:23:45 +0100 Subject: [PATCH 15/16] formated the code and verification plus some more css --- public/index.php | 12 +++++----- src/Controller/TeamController.php | 20 +++++++++------- src/Data/Color.php | 13 +++++----- src/Data/MemberRole.php | 5 ++-- src/Data/Team.php | 2 +- src/Gateway/TeamGateway.php | 40 +++++++++++++++++++++---------- src/Model/TeamModel.php | 5 ++-- src/Validation/Validators.php | 7 ++---- src/Views/display_team.html.twig | 13 +++++----- 9 files changed, 66 insertions(+), 51 deletions(-) diff --git a/public/index.php b/public/index.php index 3e4bf87..6633860 100644 --- a/public/index.php +++ b/public/index.php @@ -49,14 +49,14 @@ $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); $router->map("GET", "/tactic/[i:id]", fn(int $id) => $visualizerController->openVisualizer($id)); -$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)); +$teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(new \App\Gateway\TeamGateway($con))); +$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)); +$router->map("GET", "/team/list", fn() => $teamController->displayListTeamByName()); +$router->map("POST", "/team/list", fn() => $teamController->ListTeamByName($_POST)); -$router->map("GET","/team/[i:id]", fn(int $id)=>$teamController->getTeam($id)); +$router->map("GET", "/team/[i:id]", fn(int $id) => $teamController->getTeam($id)); $match = $router->match(); diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index d4c7bfe..08e22cc 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -8,10 +8,8 @@ use App\Http\ViewHttpResponse; use App\Model\TeamModel; use App\Validation\FieldValidationFail; use App\Validation\Validators; -use \Twig\Environment; -class TeamController -{ +class TeamController { private TeamModel $model; /** @@ -21,10 +19,14 @@ class TeamController $this->model = $model; } - public function displaySubmitTeam() : HttpResponse { + public function displaySubmitTeam(): HttpResponse { return ViewHttpResponse::twig("insert_team.html.twig", []); } + /** + * @param array $request + * @return HttpResponse + */ public function submitTeam(array $request): HttpResponse { $errors = []; @@ -32,7 +34,7 @@ class TeamController "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], "mainColor" => [Validators::regex('/#(?:[0-9a-fA-F]{6})/')], "secondColor" => [Validators::regex('/#(?:[0-9a-fA-F]{6})/')], - "picture" => [Validators::isURL()] + "picture" => [Validators::isURL()], ]); if (!empty($errors)) { $badFields = []; @@ -50,10 +52,14 @@ class TeamController return ViewHttpResponse::twig("list_team_by_name.html.twig", []); } + /** + * @param array $request + * @return HttpResponse + */ 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) { @@ -75,5 +81,3 @@ class TeamController return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); } } - - diff --git a/src/Data/Color.php b/src/Data/Color.php index 875b615..b12e27a 100755 --- a/src/Data/Color.php +++ b/src/Data/Color.php @@ -2,31 +2,31 @@ namespace App\Data; -use \InvalidArgumentException; +use InvalidArgumentException; class Color { /** - * @var string 6 bytes unsigned int that represents an RGB color + * @var string that represents an hexadecimal color code */ private string $hex; /** * @param string $value 6 bytes unsigned int that represents an RGB color - * @throws \InvalidArgumentException if the value is negative or greater than 0xFFFFFF + * @throws InvalidArgumentException if the value is negative or greater than 0xFFFFFF */ private function __construct(string $value) { if ($value < 0 || $value > 0xFFFFFF) { throw new InvalidArgumentException("int color value is invalid, must be positive and lower than 0xFFFFFF"); } - $this->value = $value; + $this->hex = $value; } /** * @return string */ public function getValue(): string { - return $this->value; + return $this->hex; } public static function from(string $value): Color { @@ -39,10 +39,9 @@ class Color { } public static function tryFrom(string $value): ?Color { - if (!preg_match('/#(?:[0-9a-fA-F]{6})/',$value)) { + if (!preg_match('/#(?:[0-9a-fA-F]{6})/', $value)) { return null; } return new Color($value); } } - diff --git a/src/Data/MemberRole.php b/src/Data/MemberRole.php index 9838b92..559d516 100755 --- a/src/Data/MemberRole.php +++ b/src/Data/MemberRole.php @@ -10,7 +10,6 @@ use http\Exception\InvalidArgumentException; * encapsulates an integer value and use it as an enumeration discriminant */ final class MemberRole { - private const ROLE_PLAYER = 0; private const ROLE_COACH = 1; private const MIN = self::ROLE_PLAYER; @@ -25,11 +24,11 @@ final class MemberRole { $this->value = $val; } - public static function player(){ + public static function player(): MemberRole { return new MemberRole(MemberRole::ROLE_PLAYER); } - public static function coach(){ + public static function coach(): MemberRole { return new MemberRole(MemberRole::ROLE_COACH); } diff --git a/src/Data/Team.php b/src/Data/Team.php index a31829f..e50ad40 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -21,7 +21,7 @@ class Team { * @param Color $secondColor * @param Member[] $members */ - public function __construct(int $id,string $name, string $picture, Color $mainColor, Color $secondColor, array $members =[]) { + public function __construct(int $id, string $name, string $picture, Color $mainColor, Color $secondColor, array $members = []) { $this->id = $id; $this->name = $name; $this->picture = $picture; diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 1cecbe1..c3d22dc 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -12,52 +12,68 @@ class TeamGateway { $this->con = $con; } - public function insert(string $name, string $picture, string $mainColor, string $secondColor) { + public function insert(string $name, string $picture, string $mainColor, string $secondColor): void { $this->con->exec( "INSERT INTO Team(name, picture, mainColor, secondColor) VALUES (:teamName , :picture, :mainColor, :secondColor)", [ ":teamName" => [$name, PDO::PARAM_STR], ":picture" => [$picture, PDO::PARAM_STR], ":mainColor" => [$mainColor, PDO::PARAM_STR], - ":secondColor" => [$secondColor, PDO::PARAM_STR] + ":secondColor" => [$secondColor, PDO::PARAM_STR], ] ); } + /** + * @param string $name + * @return array[] + */ public function listByName(string $name): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", [ - ":name" => [$name, PDO::PARAM_STR] + ":name" => [$name, PDO::PARAM_STR], ] ); } - public function getTeamById(int $id): array{ + /** + * @param int $id + * @return array[] + */ + public function getTeamById(int $id): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE id = :id", [ - ":id" => [$id, PDO::PARAM_INT] + ":id" => [$id, PDO::PARAM_INT], ] ); } - public function getIdTeamByName(string $name): array{ + /** + * @param string $name + * @return array[] + */ + public function getIdTeamByName(string $name): array { return $this->con->fetch( "SELECT id FROM Team WHERE name = :name", [ - ":name" => [$name, PDO::PARAM_STR] + ":name" => [$name, PDO::PARAM_STR], ] ); } - public function getMembersById($id):array{ + /** + * @param int $id + * @return array[] + */ + 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 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], ] ); } -} \ No newline at end of file +} diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index c5521eb..b6e4ab1 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -8,8 +8,7 @@ use App\Data\Member; use App\Data\MemberRole; use App\Data\Color; -class TeamModel -{ +class TeamModel { private TeamGateway $gateway; /** @@ -48,4 +47,4 @@ class TeamModel } return new Team(intval($result['id']), $result['name'], $result['picture'], Color::from($result['mainColor']), Color::from($result['secondColor']), $members); } -} \ No newline at end of file +} diff --git a/src/Validation/Validators.php b/src/Validation/Validators.php index b1d1eaf..cb28007 100644 --- a/src/Validation/Validators.php +++ b/src/Validation/Validators.php @@ -56,7 +56,7 @@ class Validators { return self::regex("/^[0-9]+$/"); } - public static function isIntInRange(int $min,int $max): Validator { + public static function isIntInRange(int $min, int $max): Validator { return new SimpleFunctionValidator( fn(string $val) => intval($val) >= $min && intval($val) <= $max, fn(string $name) => [new FieldValidationFail($name, "The value is not in the range $min to $max ")] @@ -65,11 +65,8 @@ class Validators { public static function isURL(): Validator { return new SimpleFunctionValidator( - fn($val) => filter_var($val, FILTER_VALIDATE_URL) , + fn($val) => filter_var($val, FILTER_VALIDATE_URL), fn(string $name) => [new FieldValidationFail($name, "The value is not an URL")] ); } } - - - diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index 732fd19..ada8566 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -22,9 +22,15 @@ #mainColor{ background-color: {{ team.mainColor.getValue() }}; + {% if team.mainColor.getValue() == "#ffffff" %} + border-color: #666666; + {% endif %} } #secondColor{ background-color: {{ team.secondColor.getValue() }}; + {% if team.secondColor.getValue() == "#ffffff" %} + border-color: #666666; + {% endif %} } .container{ @@ -39,9 +45,6 @@ } - .colors{ - justify-content: space-between; - } .color{ flex-direction: row; justify-content: space-between; @@ -65,12 +68,10 @@

{{ team.name }}

-
+

Couleur principale :

Couleur secondaire :

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

m.id

{% endfor %} -- 2.36.3 From d8000bcf3b32fbd97512f6740ea02eae9dc9cf80 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Wed, 22 Nov 2023 12:33:27 +0100 Subject: [PATCH 16/16] fixing CI --- src/Model/TeamModel.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index b6e4ab1..65f22a7 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -24,6 +24,10 @@ class TeamModel { return intval($result[0]['id']); } + /** + * @param string $name + * @return Team[] + */ public function listByName(string $name): array { $teams = []; $results = $this->gateway->listByName($name); -- 2.36.3