87 lines
1.9 KiB
PHP
87 lines
1.9 KiB
PHP
<?php
|
|
|
|
use App\Core\Database as Database;
|
|
use App\Teamtable\Team as Team;
|
|
use App\Teamtable\TeamMapper as TeamMapper;
|
|
|
|
/**
|
|
* Does stuffs with the teamtable
|
|
*/
|
|
class Teamtable
|
|
{
|
|
/**
|
|
* Database connection
|
|
*/
|
|
public PDO $dbh;
|
|
|
|
/**
|
|
* We use a data mapper pattern
|
|
*/
|
|
public TeamMapper $teamMapper;
|
|
|
|
public function __construct(Database $database)
|
|
{
|
|
$this->dbh = $database->conn;
|
|
$this->teamMapper = new TeamMapper($this->dbh);
|
|
}
|
|
|
|
/**
|
|
* Fetch entire team table
|
|
*/
|
|
public function getAll(): array
|
|
{
|
|
return $this->teamMapper->getAll();
|
|
}
|
|
|
|
/**
|
|
* Find team with supplied id
|
|
*/
|
|
public function get(int $id): ?Team
|
|
{
|
|
return $this->teamMapper->get($id);
|
|
}
|
|
|
|
public function getByCardnumber(string $cardnumber): ?Team
|
|
{
|
|
return $this->teamMapper->getByCardnumber($cardnumber);
|
|
}
|
|
|
|
/**
|
|
* Inserts team into database
|
|
*/
|
|
public function create(Team $team): Team
|
|
{
|
|
return $this->teamMapper->create($team);
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
$this->teamMapper->delete($id);
|
|
}
|
|
|
|
public function update(Team $team): Team
|
|
{
|
|
return $this->teamMapper->update($team);
|
|
}
|
|
|
|
/**
|
|
* Returns TRUE if team exists, FALSE if not
|
|
* TODO: maybe use integers instead of booleans for codes
|
|
*/
|
|
public function recieveBaton(string $cardnumber): bool
|
|
{
|
|
$team = $this->getByCardnumber($cardnumber);
|
|
if ($team)
|
|
{
|
|
// team exists, insert into time table
|
|
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
|
|
$sth->execute([$team->id]);
|
|
return TRUE;
|
|
}
|
|
// team does not exist, lets create it
|
|
$team = new Team;
|
|
$team->cardnumber = $cardnumber;
|
|
$this->create($team);
|
|
return FALSE;
|
|
}
|
|
} |