From a283c4b12627feb38ca5cb8c57ea84babed10e43 Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Tue, 14 Nov 2023 17:27:03 +0100 Subject: [PATCH] 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; } +