cleanup searchCriteria generation for flights

This commit is contained in:
Nabeel Shahzad 2017-12-04 10:59:25 -06:00
parent 6bb0a52d81
commit e032fd2dbf
3 changed files with 52 additions and 31 deletions

View File

@ -4,42 +4,30 @@ namespace App\Http\Controllers\Api;
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\Models\Transformers\FlightTransformer;
use App\Repositories\FlightRepository;
class FlightController extends AppBaseController
{
protected $flightRepo;
public function __construct(
FlightRepository $flightRepo
) {
$this->flightRepo = $flightRepo;
}
public function get($id)
{
$flight = Flight::find($id);
$flight = $this->flightRepo->find($id);
return fractal($flight, new FlightTransformer())->respond();
}
public function search(Request $request)
{
$where = [];
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();
$flights = $this->flightRepo->searchCriteria($request)->paginate();
return fractal($flights, new FlightTransformer())->respond();
}
}

View File

@ -2,16 +2,18 @@
namespace App\Http\Controllers\Frontend;
use Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Repositories\FlightRepository;
use App\Http\Controllers\AppBaseController;
use App\Models\Flight;
use App\Models\UserFlight;
use App\Repositories\Criteria\WhereCriteria;
use Mockery\Exception;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
class FlightController extends AppBaseController
{
@ -31,7 +33,12 @@ class FlightController extends AppBaseController
$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();
$saved_flights = UserFlight::where('user_id', Auth::id())
@ -51,12 +58,7 @@ class FlightController extends AppBaseController
*/
public function search(Request $request)
{
$where = ['active' => true];
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$flights = $this->flightRepo->paginate();
// TODO: PAGINATION
$flights = $this->flightRepo->searchCriteria($request)->paginate();
$saved_flights = UserFlight::where('user_id', Auth::id())
->pluck('flight_id')->toArray();

View File

@ -3,6 +3,8 @@
namespace App\Repositories;
use App\Models\Flight;
use App\Repositories\Criteria\WhereCriteria;
use Illuminate\Http\Request;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;
@ -22,4 +24,33 @@ class FlightRepository extends BaseRepository implements CacheableInterface
{
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;
}
}