diff --git a/app/model/Cardreader.php b/app/model/Cardreader.php new file mode 100644 index 0000000..31d7fb1 --- /dev/null +++ b/app/model/Cardreader.php @@ -0,0 +1,38 @@ +dbh = $database->conn; + $this->teamMapper = new TeamMapper($this->dbh); + } + + /** + * Returns TRUE if team exists, FALSE if not + */ + public function recieve(string $cardnumber): bool + { + $team = $this->teamMapper->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->teamMapper->create($team); + return FALSE; + } +} \ No newline at end of file diff --git a/app/model/Teamtable.php b/app/model/Teamtable.php index 71314a2..8f52617 100644 --- a/app/model/Teamtable.php +++ b/app/model/Teamtable.php @@ -9,9 +9,6 @@ use App\Teamtable\TeamMapper as TeamMapper; */ class Teamtable { - /** - * Database connection - */ public PDO $dbh; /** @@ -41,11 +38,6 @@ class Teamtable return $this->teamMapper->get($id); } - public function getByCardnumber(string $cardnumber): ?Team - { - return $this->teamMapper->getByCardnumber($cardnumber); - } - /** * Inserts team into database */ @@ -63,25 +55,4 @@ class Teamtable { 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; - } } \ No newline at end of file diff --git a/public/simulator.php b/public/simulator.php index 4005fe1..a99afda 100644 --- a/public/simulator.php +++ b/public/simulator.php @@ -2,7 +2,7 @@ // TODO: refactor -$model = $app->model('Teamtable'); +$cardreader = $app->model('Cardreader'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { @@ -11,7 +11,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') $cardnumber = $_POST['cardnumber']; if (!(strlen($cardnumber) > 32)) { - if ($model->recieveBaton($cardnumber)) + if ($cardreader->recieve($cardnumber)) { $app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); } else {