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");
$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();

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

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

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

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

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