2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Repository;
|
2017-06-18 06:25:36 +08:00
|
|
|
use App\Models\Flight;
|
2017-12-05 00:59:25 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Prettus\Repository\Contracts\CacheableInterface;
|
|
|
|
use Prettus\Repository\Traits\CacheableRepository;
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class FlightRepository
|
|
|
|
*/
|
|
|
|
class FlightRepository extends Repository implements CacheableInterface
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
use CacheableRepository;
|
|
|
|
|
2017-06-18 06:25:36 +08:00
|
|
|
protected $fieldSearchable = [
|
2017-12-01 10:28:45 +08:00
|
|
|
'arr_airport_id',
|
2017-06-18 06:25:36 +08:00
|
|
|
'dpt_airport_id',
|
2017-12-01 10:28:45 +08:00
|
|
|
'flight_number' => 'like',
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight_code' => 'like',
|
|
|
|
'flight_leg' => 'like',
|
|
|
|
'route' => 'like',
|
|
|
|
'notes' => 'like',
|
2017-06-18 06:25:36 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Flight::class;
|
|
|
|
}
|
2017-12-05 00:59:25 +08:00
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
/**
|
|
|
|
* Find a flight based on the given criterea
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $airline_id
|
|
|
|
* @param $flight_num
|
2018-03-02 06:20:13 +08:00
|
|
|
* @param null $route_code
|
|
|
|
* @param null $route_leg
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function findFlight($airline_id, $flight_num, $route_code = null, $route_leg = null)
|
2018-02-25 05:38:25 +08:00
|
|
|
{
|
|
|
|
$where = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'airline_id' => $airline_id,
|
2018-03-02 06:20:13 +08:00
|
|
|
'flight_number' => $flight_num,
|
2018-03-20 09:50:40 +08:00
|
|
|
'active' => true,
|
2018-02-25 05:38:25 +08:00
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($route_code)) {
|
2018-03-02 06:20:13 +08:00
|
|
|
$where['route_code'] = $route_code;
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($route_leg)) {
|
2018-03-02 06:20:13 +08:00
|
|
|
$where['route_leg'] = $route_leg;
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findWhere($where);
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
/**
|
|
|
|
* Create the search criteria and return this with the stuff pushed
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 05:11:27 +08:00
|
|
|
* @param Request $request
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param bool $only_active
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-05 00:59:25 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return $this
|
2017-12-05 00:59:25 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function searchCriteria(Request $request, bool $only_active = true)
|
2017-12-05 00:59:25 +08:00
|
|
|
{
|
2017-12-27 04:14:04 +08:00
|
|
|
$where = [];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($only_active === true) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$where['active'] = $only_active;
|
2018-07-13 11:29:50 +08:00
|
|
|
$where['visible'] = $only_active;
|
2017-12-27 04:14:04 +08:00
|
|
|
}
|
2017-12-05 00:59:25 +08:00
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
if ($request->filled('flight_id')) {
|
|
|
|
$where['id'] = $request->flight_id;
|
|
|
|
}
|
|
|
|
|
2017-12-05 02:05:31 +08:00
|
|
|
if ($request->filled('airline_id')) {
|
|
|
|
$where['airline_id'] = $request->airline_id;
|
2017-12-05 00:59:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($request->filled('flight_number')) {
|
2017-12-05 02:05:31 +08:00
|
|
|
$where['flight_number'] = $request->flight_number;
|
2017-12-05 00:59:25 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 02:05:31 +08:00
|
|
|
if ($request->filled('route_code')) {
|
|
|
|
$where['route_code'] = $request->route_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('dep_icao')) {
|
|
|
|
$where['dpt_airport_id'] = $request->dep_icao;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('arr_icao')) {
|
|
|
|
$where['arr_airport_id'] = $request->arr_icao;
|
2017-12-05 00:59:25 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
// Distance, greater than
|
|
|
|
if ($request->filled('dgt')) {
|
|
|
|
$where[] = ['distance', '>=', $request->dgt];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Distance, less than
|
|
|
|
if ($request->filled('dlt')) {
|
|
|
|
$where[] = ['distance', '<', $request->dlt];
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
$this->pushCriteria(new WhereCriteria($request, $where));
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
return $this;
|
|
|
|
}
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|