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.
187 lines
5.4 KiB
187 lines
5.4 KiB
<?php
|
|
|
|
namespace IQBall\Core\Gateway;
|
|
|
|
use IQBall\Core\Connection;
|
|
use IQBall\Core\Data\Color;
|
|
use IQBall\Core\Data\Team;
|
|
use IQBall\Core\Data\TeamInfo;
|
|
use PDO;
|
|
|
|
class TeamGateway {
|
|
private Connection $con;
|
|
|
|
public function __construct(Connection $con) {
|
|
$this->con = $con;
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param string $picture
|
|
* @param string $mainColor
|
|
* @param string $secondColor
|
|
* @return int the inserted team identifier
|
|
*/
|
|
public function insert(string $name, string $picture, string $mainColor, string $secondColor): int {
|
|
$this->con->exec(
|
|
"INSERT INTO team(name, picture, main_color, second_color) VALUES (:team_name , :picture, :main_color, :second_color)",
|
|
[
|
|
":team_name" => [$name, PDO::PARAM_STR],
|
|
":picture" => [$picture, PDO::PARAM_STR],
|
|
":main_color" => [$mainColor, PDO::PARAM_STR],
|
|
":second_color" => [$secondColor, PDO::PARAM_STR],
|
|
]
|
|
);
|
|
return intval($this->con->lastInsertId());
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param int $id
|
|
* @return TeamInfo[]
|
|
*/
|
|
public function listByName(string $name, int $id): array {
|
|
$result = $this->con->fetch(
|
|
"SELECT t.* FROM team t, Member m WHERE t.name LIKE '%' || :name || '%' AND t.id=m.id_team AND m.id_user=:id",
|
|
[
|
|
":name" => [$name, PDO::PARAM_STR],
|
|
"id" => [$id, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
|
|
return array_map(fn($row) => new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']), $result);
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @return TeamInfo|null
|
|
*/
|
|
public function getTeamById(int $id): ?TeamInfo {
|
|
$row = $this->con->fetch(
|
|
"SELECT * FROM team WHERE id = :id",
|
|
[
|
|
":id" => [$id, PDO::PARAM_INT],
|
|
]
|
|
)[0] ?? null;
|
|
if ($row == null) {
|
|
return null;
|
|
}
|
|
return new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']);
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @return int|null
|
|
*/
|
|
public function getTeamIdByName(string $name): ?int {
|
|
return $this->con->fetch(
|
|
"SELECT id FROM team WHERE name = :name",
|
|
[
|
|
":name" => [$name, PDO::PARAM_INT],
|
|
]
|
|
)[0]['id'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @param int $idTeam
|
|
*/
|
|
public function deleteTeam(int $idTeam): void {
|
|
$this->con->exec(
|
|
"DELETE FROM Member WHERE id_team=:team",
|
|
[
|
|
"team" => [$idTeam, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
$this->con->exec(
|
|
"DELETE FROM TEAM WHERE id=:team",
|
|
[
|
|
"team" => [$idTeam, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param int $idTeam
|
|
* @param string $newName
|
|
* @param string $newPicture
|
|
* @param string $newMainColor
|
|
* @param string $newSecondColor
|
|
* @return void
|
|
*/
|
|
public function editTeam(int $idTeam, string $newName, string $newPicture, string $newMainColor, string $newSecondColor) {
|
|
$this->con->exec(
|
|
"UPDATE team
|
|
SET name = :newName,
|
|
picture = :newPicture,
|
|
main_color = :newMainColor,
|
|
second_color = :newSecondColor
|
|
WHERE id = :team",
|
|
[
|
|
"team" => [$idTeam, PDO::PARAM_INT],
|
|
"newName" => [$newName, PDO::PARAM_STR],
|
|
"newPicture" => [$newPicture, PDO::PARAM_STR],
|
|
"newMainColor" => [$newMainColor, PDO::PARAM_STR],
|
|
"newSecondColor" => [$newSecondColor, PDO::PARAM_STR],
|
|
]
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* @param int $user
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function getAll(int $user): array {
|
|
return $this->con->fetch(
|
|
"SELECT t.* FROM team t,Member m WHERE m.id_team = t.id AND m.id_user= :idUser ",
|
|
[
|
|
"idUser" => [$user, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param int $start
|
|
* @param int $n
|
|
* @return TeamInfo[]
|
|
*/
|
|
public function listAll(int $start, int $n): array {
|
|
$rows = $this->con->fetch(
|
|
"SELECT * FROM Team LIMIT :start, :n",
|
|
[
|
|
":start" => [$start, PDO::PARAM_INT],
|
|
":n" => [$n, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
return array_map(fn($row) => new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']), $rows);
|
|
}
|
|
|
|
public function countTeam(): int {
|
|
$result = $this->con->fetch(
|
|
"SELECT count(*) as count FROM Team",
|
|
[]
|
|
);
|
|
if (empty($result) || !isset($result[0]['count'])) {
|
|
return 0;
|
|
}
|
|
return $result[0]['count'];
|
|
}
|
|
|
|
/**
|
|
* @param array<Team> $selectedTeams
|
|
* @return void
|
|
*/
|
|
public function deleteTeamSelected(array $selectedTeams): void {
|
|
foreach ($selectedTeams as $team) {
|
|
$this->con->exec(
|
|
"DELETE FROM TEAM WHERE id=:team",
|
|
[
|
|
"team" => [$team, PDO::PARAM_INT],
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
}
|