phpvms/app/Repositories/FlightRepository.php

122 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Repositories;
use App\Interfaces\Repository;
use App\Models\Flight;
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;
/**
* Class FlightRepository
*/
class FlightRepository extends Repository implements CacheableInterface
{
use CacheableRepository;
protected $fieldSearchable = [
'arr_airport_id',
2019-05-11 05:24:34 +08:00
'distance',
'dpt_airport_id',
'flight_number' => 'like',
2019-05-11 05:24:34 +08:00
'route_code' => 'like',
'route_leg' => 'like',
'route' => 'like',
'notes' => 'like',
];
2019-05-11 05:24:34 +08:00
public function model(): string
{
return Flight::class;
}
/**
* Find a flight based on the given criterea
2018-08-27 00:40:04 +08:00
*
* @param $airline_id
* @param $flight_num
* @param null $route_code
* @param null $route_leg
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function findFlight($airline_id, $flight_num, $route_code = null, $route_leg = null)
{
$where = [
'airline_id' => $airline_id,
'flight_number' => $flight_num,
'active' => true,
];
if (filled($route_code)) {
$where['route_code'] = $route_code;
}
if (filled($route_leg)) {
$where['route_leg'] = $route_leg;
}
return $this->findWhere($where);
}
/**
* 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
* @param bool $only_active
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
2018-08-27 00:40:04 +08:00
*
* @return $this
*/
2019-05-11 05:24:34 +08:00
public function searchCriteria(Request $request, bool $only_active = true): self
{
$where = [];
if ($only_active === true) {
2018-08-27 00:40:04 +08:00
$where['active'] = $only_active;
$where['visible'] = $only_active;
}
if ($request->filled('flight_id')) {
$where['id'] = $request->flight_id;
}
if ($request->filled('airline_id')) {
$where['airline_id'] = $request->airline_id;
}
if ($request->filled('flight_number')) {
$where['flight_number'] = $request->flight_number;
}
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;
}
// Distance, greater than
if ($request->filled('dgt')) {
$where[] = ['distance', '>=', $request->dgt];
}
// Distance, less than
if ($request->filled('dlt')) {
2019-05-12 00:37:06 +08:00
$where[] = ['distance', '<=', $request->dlt];
}
$this->pushCriteria(new WhereCriteria($request, $where));
return $this;
}
}