Added to the API the drivers to obtain the most used tracks and cars in the category

This commit is contained in:
Alfonso Saavedra "Son Link" 2024-12-31 11:50:09 +01:00
parent 166cfee7c3
commit 053259f186
No known key found for this signature in database
GPG key ID: D3594BCF897F74D8
4 changed files with 125 additions and 3 deletions

View file

@ -34,6 +34,8 @@ $routes->group('register', static function ($routes) {
$routes->group('api', static function ($routes) {
$routes->get('bests_laps', 'Api::getBestsLaps');
$routes->get('most_active_users', 'Api::getMostActiveUsers');
$routes->get('most_used_tracks', 'Api::getMostUsedTracks');
$routes->get('most_used_cars', 'Api::getMostUsedCars');
});
$routes->group('test', static function ($routes) {

View file

@ -4,7 +4,9 @@ namespace App\Controllers;
use App\Controllers\BaseController;
use App\Models\BestLapsModel;
use App\Models\CarsModel;
use App\Models\CarCatsModel;
use App\Models\TracksModel;
use CodeIgniter\API\ResponseTrait;
class Api extends BaseController
@ -60,4 +62,52 @@ class Api extends BaseController
return $this->respond(['data' => $list, 'total' => $total]);
}
public function getMostUsedTracks()
{
$period = $this->request->getGet('period');
$carCatId = $this->request->getGet('car_cat');
if (!$period || !$carCatId) return $this->fail('The period and/or category were not indicated');
$page = $this->request->getGet('page');
$limit = $this->request->getGet('limit');
if (!$page | !is_numeric($page)) $page = 0;
if (!$limit | !is_numeric($limit)) $limit = 0;
$carCatsModel = new CarCatsModel;
$tracksModel = new TracksModel();
$carsCatIds = $carCatsModel->getCarsInCat($carCatId);
$list = [];
$total = 0;
[$list, $total] = $tracksModel->getMostUsedTracks($carsCatIds, $period, $page, $limit);
return $this->respond(['data' => $list, 'total' => $total]);
}
public function getMostUsedCars()
{
$period = $this->request->getGet('period');
$carCatId = $this->request->getGet('car_cat');
if (!$period || !$carCatId) return $this->fail('The period and/or category were not indicated');
$page = $this->request->getGet('page');
$limit = $this->request->getGet('limit');
if (!$page | !is_numeric($page)) $page = 0;
if (!$limit | !is_numeric($limit)) $limit = 0;
$carCatsModel = new CarCatsModel;
$carsModel = new CarsModel;
$carsCatIds = $carCatsModel->getCarsInCat($carCatId);
$list = [];
$total = 0;
[$list, $total] = $carsModel->getMostUsedCars($carsCatIds, $period, $page, $limit);
return $this->respond(['data' => $list, 'total' => $total]);
}
}

View file

@ -4,11 +4,44 @@ use App\Models\BaseModel;
class CarsModel extends BaseModel
{
private $data;
protected $table = 'cars';
protected $table = 'cars';
protected $allowedFields = ['id', 'name', 'img', 'category', 'width', 'length', 'mass', 'fueltank', 'engine', 'drivetrain'];
protected $allowedFields = ['id', 'name', 'img', 'category', 'width', 'length', 'mass', 'fueltank', 'engine', 'drivetrain'];
public function getMostUsedCars(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
{
$list = [];
$total = 0;
$offset = $page * $limit;
$backto = getDateDiff($period);
$builder = $this->db->table('races r');
$builder->join('cars c', 'c.id = r.car_id');
$builder->select('r.car_id, COUNT(r.car_id) as count, c.name');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy('r.car_id');
$builder->orderBy('count DESC');
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $total = $query->getNumRows();
if ($total == 0) return [[], 0];
$builder = $this->db->table('races r');
$builder->join('cars c', 'c.id = r.car_id');
$builder->select('r.car_id, COUNT(r.car_id) as count, c.name');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy('r.car_id');
$builder->orderBy('count DESC');
if ($limit > 0) $builder->limit($limit, $offset);
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $list = $query->getResult();
return [$list, $total];
}
/*
public function __construct($car=null)

View file

@ -3,12 +3,49 @@ namespace App\Models;
use App\Models\BaseModel;
use function PHPUnit\Framework\returnSelf;
class TracksModel extends BaseModel
{
protected $table = 'tracks';
protected $db;
protected $allowedFields = ['id', 'name', 'img', 'category', 'author', 'description'];
public function getMostUsedTracks(array $carsCatIds, string $period='today', int $page=0, int $limit=20)
{
$list = [];
$total = 0;
$offset = $page * $limit;
$backto = getDateDiff($period);
$builder = $this->db->table('races r');
$builder->join('tracks t', 't.id = r.track_id');
$builder->select('r.track_id, COUNT(*) AS count, t.name AS track_name');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy('r.track_id');
$builder->orderBy('count DESC');
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $total = $query->getNumRows();
if ($total == 0) return [[], 0];
$builder = $this->db->table('races r');
$builder->join('tracks t', 't.id = r.track_id');
$builder->select('r.track_id, COUNT(*) AS count, t.name AS track_name');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy('r.track_id');
$builder->orderBy('count DESC');
if ($limit > 0) $builder->limit($limit, $offset);
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $list = $query->getResult();
return [$list, $total];
}
/*
public function __construct($track=null)
{