Trow PageNotFoundException if car, track, user or race don't exists

This commit is contained in:
Alfonso Saavedra "Son Link" 2024-12-23 20:51:04 +01:00
parent ca004ee472
commit 01de175fe9
No known key found for this signature in database
GPG key ID: D3594BCF897F74D8
4 changed files with 20 additions and 12 deletions

View file

@ -16,6 +16,9 @@ class Cars extends BaseController
{ {
$this->cachePage(3600); $this->cachePage(3600);
$car = $this->carsModel->data($id); $car = $this->carsModel->data($id);
if (!$car) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
echo get_header('Car: ' . $car->name); echo get_header('Car: ' . $car->name);
echo view('car', ['car' => $car]); echo view('car', ['car' => $car]);
echo get_footer(); echo get_footer();

View file

@ -31,19 +31,18 @@ class Races extends BaseController
$tplData = []; $tplData = [];
if ($query && $query->getNumRows() == 1) if (!$query || $query->getNumRows() == 0) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
{
$tplData['race'] = $query->getRow(); $tplData['race'] = $query->getRow();
$builder = $this->db->table('laps'); $builder = $this->db->table('laps');
$builder->where('race_id', $tplData['race']->id); $builder->where('race_id', $tplData['race']->id);
$query = $builder->get(); $query = $builder->get();
$tplData['race']->n_laps = 0; $tplData['race']->n_laps = 0;
if ($query && $query->getNumRows() > 0) if ($query && $query->getNumRows() > 0)
{ {
$tplData['laps'] = json_encode($query->getResult()); $tplData['laps'] = json_encode($query->getResult());
$tplData['race']->n_laps = $query->getNumRows(); $tplData['race']->n_laps = $query->getNumRows();
}
} }
echo get_header('Races'); echo get_header('Races');

View file

@ -16,6 +16,9 @@ class Tracks extends BaseController
{ {
$this->cachePage(3600); $this->cachePage(3600);
$track = $this->tracksModel->data($id); $track = $this->tracksModel->data($id);
if (!$track) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
$bestLaps = getBestTimesTrack($id); $bestLaps = getBestTimesTrack($id);
echo get_header('Track: ' . $track->name); echo get_header('Track: ' . $track->name);
echo view('track', ['track' => $track, 'bestLaps' => $bestLaps]); echo view('track', ['track' => $track, 'bestLaps' => $bestLaps]);

View file

@ -26,6 +26,9 @@ class Users extends BaseController
{ {
//$this->cachePage(3600); //$this->cachePage(3600);
$user = $this->usersModel->getUser($username); $user = $this->usersModel->getUser($username);
if (!$user) throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
$userraces = $this->usersModel->getRaces(); $userraces = $this->usersModel->getRaces();
$raceswon = $this->usersModel->getWon(); $raceswon = $this->usersModel->getWon();
$racespodiums = $this->usersModel->getPodiums(); $racespodiums = $this->usersModel->getPodiums();