cleanup searchCriteria generation for flights
This commit is contained in:
parent
6bb0a52d81
commit
e032fd2dbf
@ -4,42 +4,30 @@ namespace App\Http\Controllers\Api;
|
|||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
use App\Models\Airline;
|
|
||||||
use App\Models\Airport;
|
|
||||||
use App\Models\Flight;
|
|
||||||
use App\Models\Transformers\FlightTransformer;
|
|
||||||
|
|
||||||
use App\Http\Controllers\AppBaseController;
|
use App\Http\Controllers\AppBaseController;
|
||||||
|
use App\Models\Transformers\FlightTransformer;
|
||||||
|
use App\Repositories\FlightRepository;
|
||||||
|
|
||||||
|
|
||||||
class FlightController extends AppBaseController
|
class FlightController extends AppBaseController
|
||||||
{
|
{
|
||||||
|
protected $flightRepo;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
FlightRepository $flightRepo
|
||||||
|
) {
|
||||||
|
$this->flightRepo = $flightRepo;
|
||||||
|
}
|
||||||
|
|
||||||
public function get($id)
|
public function get($id)
|
||||||
{
|
{
|
||||||
$flight = Flight::find($id);
|
$flight = $this->flightRepo->find($id);
|
||||||
return fractal($flight, new FlightTransformer())->respond();
|
return fractal($flight, new FlightTransformer())->respond();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function search(Request $request)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
$where = [];
|
$flights = $this->flightRepo->searchCriteria($request)->paginate();
|
||||||
if($request->airline) {
|
|
||||||
$airline = Airline::where('code', $request->airline)->first()->id;
|
|
||||||
$where['airline_id'] = $airline;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($request->depICAO) {
|
|
||||||
$airport = Airport::where('icao', $request->depICAO)->first()->id;
|
|
||||||
$where['dpt_airport_id'] = $airport;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($request->arrICAO) {
|
|
||||||
$airport = Airport::where('icao', $request->depICAO)->first()->id;
|
|
||||||
$where['dpt_airport_id'] = $airport;
|
|
||||||
}
|
|
||||||
|
|
||||||
$flights = Flight::where($where)->get();
|
|
||||||
return fractal($flights, new FlightTransformer())->respond();
|
return fractal($flights, new FlightTransformer())->respond();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,18 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Frontend;
|
namespace App\Http\Controllers\Frontend;
|
||||||
|
|
||||||
|
use Log;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use App\Repositories\FlightRepository;
|
use App\Repositories\FlightRepository;
|
||||||
use App\Http\Controllers\AppBaseController;
|
use App\Http\Controllers\AppBaseController;
|
||||||
|
|
||||||
use App\Models\Flight;
|
|
||||||
use App\Models\UserFlight;
|
use App\Models\UserFlight;
|
||||||
use App\Repositories\Criteria\WhereCriteria;
|
use App\Repositories\Criteria\WhereCriteria;
|
||||||
use Mockery\Exception;
|
use Mockery\Exception;
|
||||||
use Prettus\Repository\Criteria\RequestCriteria;
|
use Prettus\Repository\Criteria\RequestCriteria;
|
||||||
|
use Prettus\Repository\Exceptions\RepositoryException;
|
||||||
|
|
||||||
class FlightController extends AppBaseController
|
class FlightController extends AppBaseController
|
||||||
{
|
{
|
||||||
@ -31,7 +33,12 @@ class FlightController extends AppBaseController
|
|||||||
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
|
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
|
try {
|
||||||
|
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
|
||||||
|
} catch (RepositoryException $e) {
|
||||||
|
Log::emergency($e);
|
||||||
|
}
|
||||||
|
|
||||||
$flights = $this->flightRepo->paginate();
|
$flights = $this->flightRepo->paginate();
|
||||||
|
|
||||||
$saved_flights = UserFlight::where('user_id', Auth::id())
|
$saved_flights = UserFlight::where('user_id', Auth::id())
|
||||||
@ -51,12 +58,7 @@ class FlightController extends AppBaseController
|
|||||||
*/
|
*/
|
||||||
public function search(Request $request)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
$where = ['active' => true];
|
$flights = $this->flightRepo->searchCriteria($request)->paginate();
|
||||||
|
|
||||||
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
|
||||||
$flights = $this->flightRepo->paginate();
|
|
||||||
|
|
||||||
// TODO: PAGINATION
|
|
||||||
|
|
||||||
$saved_flights = UserFlight::where('user_id', Auth::id())
|
$saved_flights = UserFlight::where('user_id', Auth::id())
|
||||||
->pluck('flight_id')->toArray();
|
->pluck('flight_id')->toArray();
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
namespace App\Repositories;
|
namespace App\Repositories;
|
||||||
|
|
||||||
use App\Models\Flight;
|
use App\Models\Flight;
|
||||||
|
use App\Repositories\Criteria\WhereCriteria;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
use Prettus\Repository\Contracts\CacheableInterface;
|
use Prettus\Repository\Contracts\CacheableInterface;
|
||||||
use Prettus\Repository\Traits\CacheableRepository;
|
use Prettus\Repository\Traits\CacheableRepository;
|
||||||
|
|
||||||
@ -22,4 +24,33 @@ class FlightRepository extends BaseRepository implements CacheableInterface
|
|||||||
{
|
{
|
||||||
return Flight::class;
|
return Flight::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the search criteria and return this with the stuff pushed
|
||||||
|
* @param Request $request
|
||||||
|
* @param bool $only_active
|
||||||
|
* @return $this
|
||||||
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
||||||
|
*/
|
||||||
|
public function searchCriteria(Request $request, bool $only_active=true)
|
||||||
|
{
|
||||||
|
$where = [
|
||||||
|
'active' => $only_active,
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($request->airline) {
|
||||||
|
$where['airline_id'] = $request->airline;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->depICAO) {
|
||||||
|
$where['dpt_airport_id'] = $request->depICAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->arrICAO) {
|
||||||
|
$where['dpt_airport_id'] = $request->arrICAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->pushCriteria(new WhereCriteria($request, $where));
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user