From d824f17ea75f277d3a70b90c21eaf839c8dc861d Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Tue, 21 Nov 2023 11:36:25 +0100 Subject: [PATCH] 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 %}