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.
176 lines
4.8 KiB
176 lines
4.8 KiB
<?php
|
|
|
|
namespace IQBall\Core\Gateway;
|
|
|
|
use IQBall\Core\Connection;
|
|
use IQBall\Core\Data\CourtType;
|
|
use IQBall\Core\Data\TacticInfo;
|
|
use PDO;
|
|
|
|
class TacticInfoGateway {
|
|
private Connection $con;
|
|
|
|
/**
|
|
* @param Connection $con
|
|
*/
|
|
public function __construct(Connection $con) {
|
|
$this->con = $con;
|
|
}
|
|
|
|
/**
|
|
* get tactic information from given identifier
|
|
* @param int $id
|
|
* @return TacticInfo|null
|
|
*/
|
|
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];
|
|
|
|
$type = CourtType::fromName($row['court_type']);
|
|
return new TacticInfo($id, $row["name"], strtotime($row["creation_date"]), $row["owner"], $type, $row['content']);
|
|
}
|
|
|
|
|
|
/**
|
|
* Return the nb last tactics created
|
|
*
|
|
* @param integer $nb
|
|
* @return TacticInfo[] | null
|
|
*/
|
|
public function getLastOwnedBy(int $nb, int $ownerId): ?array {
|
|
$res = $this->con->fetch(
|
|
"SELECT *
|
|
FROM Tactic
|
|
WHERE owner = :ownerId
|
|
ORDER BY creation_date DESC
|
|
LIMIT :nb",
|
|
[
|
|
":ownerId" => [$ownerId, PDO::PARAM_INT],":nb" => [$nb, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
if (count($res) == 0) {
|
|
return [];
|
|
}
|
|
return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res);
|
|
}
|
|
|
|
/**
|
|
* Get all the tactics of the owner
|
|
*
|
|
* @return TacticInfo[] | null
|
|
*/
|
|
public function getAllOwnedBy(int $ownerId): ?array {
|
|
$res = $this->con->fetch(
|
|
"SELECT *
|
|
FROM Tactic
|
|
WHERE owner = :ownerId
|
|
ORDER BY name DESC",
|
|
[
|
|
":ownerId" => [$ownerId, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
if (count($res) == 0) {
|
|
return [];
|
|
}
|
|
return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res);
|
|
}
|
|
|
|
/**
|
|
* @param int $ownerId
|
|
* @return TacticInfo[]|null
|
|
*/
|
|
public function getAllTacticSharedTeam(int $ownerId): ?array {
|
|
$res = $this->con->fetch(
|
|
"SELECT t.* FROM Tactic t, TacticSharedTeam ts, Member m
|
|
WHERE ts.id_team = m.id_team AND ts.id_tactic = t.id
|
|
AND m.id_user = :ownerId",
|
|
[
|
|
":ownerId" => [$ownerId, PDO::PARAM_INT]
|
|
]
|
|
);
|
|
if (count($res) == 0) {
|
|
return [];
|
|
}
|
|
return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res);
|
|
}
|
|
|
|
/**
|
|
* @param int $ownerId
|
|
* @return TacticInfo[]|null
|
|
*/
|
|
public function getAllTacticSharedAccount(int $ownerId): ?array {
|
|
$res = $this->con->fetch(
|
|
"SELECT * FROM TacticSharedAccount
|
|
WHERE id_account = :ownerId",
|
|
[
|
|
":ownerId" => [$ownerId, PDO::PARAM_INT]
|
|
]
|
|
);
|
|
if (count($res) == 0) {
|
|
return [];
|
|
}
|
|
return array_map(fn(array $row) => $this->rowToTacticInfo($row), $res);
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param int $owner
|
|
* @param CourtType $type
|
|
* @return int inserted tactic id
|
|
*/
|
|
public function insert(string $name, int $owner, CourtType $type): int {
|
|
$this->con->exec(
|
|
"INSERT INTO Tactic(name, owner, court_type) VALUES(:name, :owner, :court_type)",
|
|
[
|
|
":name" => [$name, PDO::PARAM_STR],
|
|
":owner" => [$owner, PDO::PARAM_INT],
|
|
":court_type" => [$type->name(), PDO::PARAM_STR],
|
|
]
|
|
);
|
|
return intval($this->con->lastInsertId());
|
|
}
|
|
|
|
/**
|
|
* update name of given tactic identifier
|
|
* @param int $id
|
|
* @param string $name
|
|
* @return bool
|
|
*/
|
|
public function updateName(int $id, string $name): bool {
|
|
$stmnt = $this->con->prepare("UPDATE Tactic SET name = :name WHERE id = :id");
|
|
$stmnt->execute([
|
|
":name" => $name,
|
|
":id" => $id,
|
|
]);
|
|
return $stmnt->rowCount() == 1;
|
|
}
|
|
|
|
/***
|
|
* Updates a given tactics content
|
|
* @param int $id
|
|
* @param string $json
|
|
* @return bool
|
|
*/
|
|
public function updateContent(int $id, string $json): bool {
|
|
$stmnt = $this->con->prepare("UPDATE Tactic SET content = :content WHERE id = :id");
|
|
$stmnt->execute([
|
|
":content" => $json,
|
|
":id" => $id,
|
|
]);
|
|
return $stmnt->rowCount() == 1;
|
|
}
|
|
|
|
private function rowToTacticInfo(array $row) : TacticInfo {
|
|
$type = CourtType::fromName($row['court_type']);
|
|
return new TacticInfo($row['id'], $row["name"], strtotime($row["creation_date"]), $row["owner"], $type, $row['content']);
|
|
}
|
|
}
|