You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Application-Web/src/Gateway/TacticInfoGateway.php

78 lines
2.0 KiB

<?php
namespace App\Gateway;
use App\Connexion;
use App\Data\TacticInfo;
use PDO;
class TacticInfoGateway {
private Connexion $con;
/**
* @param Connexion $con
*/
public function __construct(Connexion $con) {
$this->con = $con;
}
public function get(int $id): ?TacticInfo {
$res = $this->con->fetch(
"SELECT * FROM Tactic WHERE id = :id",
[":id" => [$id, PDO::PARAM_INT]]
);
if (!isset($res[0])) {
return null;
}
$row = $res[0];
return new TacticInfo($id, $row["name"], strtotime($row["creation_date"]), $row["owner"]);
}
/**
* Return the nb last tactics created
*
* @param integer $nb
* @return array<array<string,mixed>>
*/
public function getLast(int $nb) : ?array {
$res = $this->con->fetch(
"SELECT * FROM Tactic ORDER BY creation_date DESC LIMIT :nb ",
[":nb" => [$nb, PDO::PARAM_INT]]
);
if (count($res) == 0) {
return null;
}
return $res;
}
public function insert(string $name, int $owner): TacticInfo {
$this->con->exec(
"INSERT INTO Tactic(name, owner) VALUES(:name, :owner)",
[
":name" => [$name, PDO::PARAM_STR],
":owner" => [$owner, PDO::PARAM_INT],
]
);
$row = $this->con->fetch(
"SELECT id, creation_date, owner FROM Tactic WHERE :id = id",
[':id' => [$this->con->lastInsertId(), PDO::PARAM_INT]]
)[0];
return new TacticInfo(intval($row["id"]), $name, strtotime($row["creation_date"]), $row["owner"]);
}
public function updateName(int $id, string $name): void {
$this->con->exec(
"UPDATE Tactic SET name = :name WHERE id = :id",
[
":name" => [$name, PDO::PARAM_STR],
":id" => [$id, PDO::PARAM_INT],
]
);
}
}