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 }}