WIP done with insertion, got a problem on list team and starting displayTeam
continuous-integration/drone/push Build is passing Details

pull/16/head
Maël DAIM 1 year ago
parent 0859467e3f
commit 30611b9b21

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

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

@ -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
*/

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

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

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
</head>
<body>
<h1>Hello world</h1>
{% if teams is empty %}
<p>Aucune équipe n'a été trouvée</p>
<div class="container">
<h2>Chercher une équipe</h2>
<form action="/team/list" method="post">
<div class="form-group">
<label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<input type="submit" value="Confirmer">
</div>
</form>
</div>
{% else %}
{% for t in teams %}
<div onclick="window.location.href = '/team/{{ t.id }}'">
<p>Nom de l'équipe: {{ t.name }}</p>
<p>picture : {{ t.picture }}</p>
</div>
{% endfor %}
{% endif %}
</body>
</html>

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
</head>
<body>
<h1>Hello world</h1>
{% if teams is empty %}
<p>Aucune équipe n'a été trouvée</p>
<div class="container">
<h2>Chercher une équipe</h2>
<form action="/team/list" method="post">
<div class="form-group">
<label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<input type="submit" value="Confirmer">
</div>
</form>
</div>
{% else %}
{% for t in teams %}
<div onclick="window.location.href = '/team/{{ t.id }}'">
<p>Nom de l'équipe: {{ t.name }}</p>
<p>picture : {{ t.picture }}</p>
</div>
{% endfor %}
{% endif %}
</body>
</html>

@ -61,7 +61,7 @@
<body>
<div class="container">
<h2>Cer une équipe</h2>
<h2>Chercher une équipe</h2>
<form action="/team/list" method="post">
<div class="form-group">
<label for="name">Nom de l'équipe :</label>

Loading…
Cancel
Save