From 29685562bb6269b56d66250a12e730b8b3b74214 Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Fri, 10 Nov 2023 15:23:31 +0100 Subject: [PATCH] add TacticModel --- public/api/index.php | 6 +++--- public/index.php | 4 +++- src/Api/TacticEndpoint.php | 6 +++--- src/Controller/EditorController.php | 22 ++++++++++++++-------- src/Gateway/TacticInfoGateway.php | 12 +++++++++--- src/Model/TacticModel.php | 28 ++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 src/Model/TacticModel.php diff --git a/public/api/index.php b/public/api/index.php index e9fc0c3..52ecf2f 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -15,9 +15,9 @@ $router = new AltoRouter(); $router->setBasePath(get_public_path() . "/api"); $tacticEndpoint = new TacticEndpoint(new TacticInfoGateway($con)); -$router->map("POST", "/tactic/[i:id]/edit/name", fn(int $id) => $tacticEndpoint->update_name($id)); -$router->map("GET", "/tactic/[i:id]", fn(int $id) => $tacticEndpoint->get_tactic_info($id)); -$router->map("POST", "/tactic/new", fn() => $tacticEndpoint->new_tactic()); +$router->map("POST", "/tactic/[i:id]/edit/name", fn(int $id) => $tacticEndpoint->updateName($id)); +$router->map("GET", "/tactic/[i:id]", fn(int $id) => $tacticEndpoint->getTacticInfo($id)); +$router->map("POST", "/tactic/new", fn() => $tacticEndpoint->newTactic()); $match = $router->match(); diff --git a/public/index.php b/public/index.php index 48b0637..b4b3f9a 100644 --- a/public/index.php +++ b/public/index.php @@ -13,6 +13,8 @@ use App\Controller\EditorController; use App\Gateway\FormResultGateway; use App\Gateway\TacticInfoGateway; +use App\Model\TacticModel; + $loader = new FilesystemLoader('../src/Views/'); $twig = new \Twig\Environment($loader); @@ -25,7 +27,7 @@ $router = new AltoRouter(); $router->setBasePath($basePath); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); -$editorController = new EditorController(new TacticInfoGateway($con)); +$editorController = new EditorController(new TacticModel(new TacticInfoGateway($con))); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); diff --git a/src/Api/TacticEndpoint.php b/src/Api/TacticEndpoint.php index 67af25b..1c35072 100644 --- a/src/Api/TacticEndpoint.php +++ b/src/Api/TacticEndpoint.php @@ -17,7 +17,7 @@ class TacticEndpoint { } - public function update_name(int $tactic_id) { + public function updateName(int $tactic_id) { $request_body = file_get_contents('php://input'); $data = json_decode($request_body); @@ -26,7 +26,7 @@ class TacticEndpoint { $this->tactics->update($tactic_id, $new_name); } - public function new_tactic() { + public function newTactic() { $request_body = file_get_contents('php://input'); $data = json_decode($request_body); @@ -36,7 +36,7 @@ class TacticEndpoint { echo "{id: $id}"; } - public function get_tactic_info(int $id) { + public function getTacticInfo(int $id) { $tactic_info = $this->tactics->get($id); echo json_encode($tactic_info); } diff --git a/src/Controller/EditorController.php b/src/Controller/EditorController.php index 1c0c2ed..e815ed1 100644 --- a/src/Controller/EditorController.php +++ b/src/Controller/EditorController.php @@ -4,18 +4,18 @@ namespace App\Controller; use App\Data\TacticInfo; use App\Gateway\TacticInfoGateway; +use App\Model\TacticModel; class EditorController { - const TACTIC_DEFAULT_NAME = "Nouvelle tactique"; - private TacticInfoGateway $tactics; + private TacticModel $model; /** - * @param TacticInfoGateway $tactics + * @param TacticModel $model */ - public function __construct(TacticInfoGateway $tactics) { - $this->tactics = $tactics; + public function __construct(TacticModel $model) { + $this->model = $model; } private function openEditor(TacticInfo $tactic) { @@ -23,12 +23,18 @@ class EditorController { } public function makeNew() { - $info = $this->tactics->insert(self::TACTIC_DEFAULT_NAME); - $this->openEditor($info); + $tactic = $this->model->makeNew(); + $this->openEditor($tactic); } public function edit(int $id) { - $tactic = $this->tactics->get($id); + $tactic = $this->model->get($id); + + if ($tactic == null) { + echo "la tactique " . $id . " n'existe pas"; + http_response_code(404); + return; + } $this->openEditor($tactic); } diff --git a/src/Gateway/TacticInfoGateway.php b/src/Gateway/TacticInfoGateway.php index 1c9a190..690d5bd 100644 --- a/src/Gateway/TacticInfoGateway.php +++ b/src/Gateway/TacticInfoGateway.php @@ -16,11 +16,17 @@ class TacticInfoGateway { $this->con = $con; } - public function get(int $id): TacticInfo { - $row = $this->con->fetch( + public function get(int $id): ?TacticInfo { + $res = $this->con->fetch( "SELECT * FROM TacticInfo WHERE id = :id", [":id" => [$id, PDO::PARAM_INT]] - )[0]; + ); + + if (!isset($res[0])) { + return null; + } + + $row = $res[0]; return new TacticInfo($id, $row["name"], strtotime($row["creation_date"])); } diff --git a/src/Model/TacticModel.php b/src/Model/TacticModel.php new file mode 100644 index 0000000..1a394f6 --- /dev/null +++ b/src/Model/TacticModel.php @@ -0,0 +1,28 @@ +tactics = $tactics; + } + + public function makeNew(): TacticInfo { + return $this->tactics->insert(self::TACTIC_DEFAULT_NAME); + } + + public function get(int $id): ?TacticInfo { + return $this->tactics->get($id); + } + +} \ No newline at end of file