add TacticModel
continuous-integration/drone/push Build is passing Details

pull/8/head
maxime.batista 1 year ago
parent 1f261fe4fe
commit 29685562bb

@ -15,9 +15,9 @@ $router = new AltoRouter();
$router->setBasePath(get_public_path() . "/api"); $router->setBasePath(get_public_path() . "/api");
$tacticEndpoint = new TacticEndpoint(new TacticInfoGateway($con)); $tacticEndpoint = new TacticEndpoint(new TacticInfoGateway($con));
$router->map("POST", "/tactic/[i:id]/edit/name", fn(int $id) => $tacticEndpoint->update_name($id)); $router->map("POST", "/tactic/[i:id]/edit/name", fn(int $id) => $tacticEndpoint->updateName($id));
$router->map("GET", "/tactic/[i:id]", fn(int $id) => $tacticEndpoint->get_tactic_info($id)); $router->map("GET", "/tactic/[i:id]", fn(int $id) => $tacticEndpoint->getTacticInfo($id));
$router->map("POST", "/tactic/new", fn() => $tacticEndpoint->new_tactic()); $router->map("POST", "/tactic/new", fn() => $tacticEndpoint->newTactic());
$match = $router->match(); $match = $router->match();

@ -13,6 +13,8 @@ use App\Controller\EditorController;
use App\Gateway\FormResultGateway; use App\Gateway\FormResultGateway;
use App\Gateway\TacticInfoGateway; use App\Gateway\TacticInfoGateway;
use App\Model\TacticModel;
$loader = new FilesystemLoader('../src/Views/'); $loader = new FilesystemLoader('../src/Views/');
$twig = new \Twig\Environment($loader); $twig = new \Twig\Environment($loader);
@ -25,7 +27,7 @@ $router = new AltoRouter();
$router->setBasePath($basePath); $router->setBasePath($basePath);
$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); $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("GET", "/", fn() => $sampleFormController->displayForm());
$router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST));

@ -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'); $request_body = file_get_contents('php://input');
$data = json_decode($request_body); $data = json_decode($request_body);
@ -26,7 +26,7 @@ class TacticEndpoint {
$this->tactics->update($tactic_id, $new_name); $this->tactics->update($tactic_id, $new_name);
} }
public function new_tactic() { public function newTactic() {
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');
$data = json_decode($request_body); $data = json_decode($request_body);
@ -36,7 +36,7 @@ class TacticEndpoint {
echo "{id: $id}"; echo "{id: $id}";
} }
public function get_tactic_info(int $id) { public function getTacticInfo(int $id) {
$tactic_info = $this->tactics->get($id); $tactic_info = $this->tactics->get($id);
echo json_encode($tactic_info); echo json_encode($tactic_info);
} }

@ -4,18 +4,18 @@ namespace App\Controller;
use App\Data\TacticInfo; use App\Data\TacticInfo;
use App\Gateway\TacticInfoGateway; use App\Gateway\TacticInfoGateway;
use App\Model\TacticModel;
class EditorController { 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) { public function __construct(TacticModel $model) {
$this->tactics = $tactics; $this->model = $model;
} }
private function openEditor(TacticInfo $tactic) { private function openEditor(TacticInfo $tactic) {
@ -23,12 +23,18 @@ class EditorController {
} }
public function makeNew() { public function makeNew() {
$info = $this->tactics->insert(self::TACTIC_DEFAULT_NAME); $tactic = $this->model->makeNew();
$this->openEditor($info); $this->openEditor($tactic);
} }
public function edit(int $id) { 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); $this->openEditor($tactic);
} }

@ -16,11 +16,17 @@ class TacticInfoGateway {
$this->con = $con; $this->con = $con;
} }
public function get(int $id): TacticInfo { public function get(int $id): ?TacticInfo {
$row = $this->con->fetch( $res = $this->con->fetch(
"SELECT * FROM TacticInfo WHERE id = :id", "SELECT * FROM TacticInfo WHERE id = :id",
[":id" => [$id, PDO::PARAM_INT]] [":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"])); return new TacticInfo($id, $row["name"], strtotime($row["creation_date"]));
} }

@ -0,0 +1,28 @@
<?php
namespace App\Model;
use App\Data\TacticInfo;
use App\Gateway\TacticInfoGateway;
class TacticModel {
const TACTIC_DEFAULT_NAME = "Nouvelle tactique";
private TacticInfoGateway $tactics;
/**
* @param TacticInfoGateway $tactics
*/
public function __construct(TacticInfoGateway $tactics) {
$this->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);
}
}
Loading…
Cancel
Save