WIP almost done, added errors handling and more, got some errors with the gateway, i will fix it tomorrow
continuous-integration/drone/push Build is passing Details

pull/16/head
Maël DAIM 1 year ago
parent 5753264157
commit a283c4b126

@ -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();

@ -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,

@ -1,7 +1,17 @@
<?php
namespace App\Controller;
use App\Http\HttpRequest;
use App\Http\HttpResponse;
use App\Http\ViewHttpResponse;
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*/
{
@ -12,29 +22,46 @@ class TeamController /* verif si les camp sont assignés, sinon erreur 400*/
* @param TeamModel $model
* @param Environment $twig
*/
public function __construct(TeamModel $model, Environment $twig)
{
public function __construct(TeamModel $model, Environment $twig) {
$this->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);
$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)) {
/*todo appelle vue avec param*/
$badFields = [];
foreach ($errors as $e) {
if ($e instanceof FieldValidationFail) {
$badFields[] = $e->getFieldName();
}
}
else{
/*todo appelle bonne vue*/
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);
}
}

@ -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;

@ -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]
]
);
}

@ -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)){
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;
}
}

@ -1,10 +0,0 @@
<?php
namespace App\Model;
class Validation
{
public static function hasHTMLInjection(string $s): bool{
return filter_var($s,FILTER_VALIDATE_REGEXP,"[<>]");
}
}

@ -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")]
);
}
}

@ -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;
}
</style>
</head>
<body>

Loading…
Cancel
Save