Pagination on the pireps/flights on the front-end

This commit is contained in:
Nabeel Shahzad 2017-12-04 10:34:01 -06:00
parent 5593d01035
commit 6bb0a52d81
5 changed files with 77 additions and 9 deletions

View File

@ -7,8 +7,11 @@ 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 Mockery\Exception; use Mockery\Exception;
use Prettus\Repository\Criteria\RequestCriteria;
class FlightController extends AppBaseController class FlightController extends AppBaseController
{ {
@ -28,9 +31,9 @@ class FlightController extends AppBaseController
$where['dpt_airport_id'] = Auth::user()->curr_airport_id; $where['dpt_airport_id'] = Auth::user()->curr_airport_id;
} }
// TODO: PAGINATION $this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$flights = $this->flightRepo->paginate();
$flights = $this->flightRepo->findWhere($where);
$saved_flights = UserFlight::where('user_id', Auth::id()) $saved_flights = UserFlight::where('user_id', Auth::id())
->pluck('flight_id')->toArray(); ->pluck('flight_id')->toArray();
@ -40,10 +43,18 @@ class FlightController extends AppBaseController
]); ]);
} }
/**
* Make a search request using the Repository search
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Prettus\Repository\Exceptions\RepositoryException
*/
public function search(Request $request) public function search(Request $request)
{ {
$where = ['active' => true]; $where = ['active' => true];
$flights = $this->flightRepo->findWhere($where);
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$flights = $this->flightRepo->paginate();
// TODO: PAGINATION // TODO: PAGINATION
@ -60,11 +71,11 @@ class FlightController extends AppBaseController
{ {
$user_id = Auth::id(); $user_id = Auth::id();
$flight_id = $request->input('flight_id'); $flight_id = $request->input('flight_id');
$action = $request->input('action'); $action = strtolower($request->input('action'));
$cols = ['user_id' => $user_id, 'flight_id' => $flight_id]; $cols = ['user_id' => $user_id, 'flight_id' => $flight_id];
if(strtolower($action) == 'save') { if($action === 'save') {
$uf = UserFlight::create($cols); $uf = UserFlight::create($cols);
$uf->save(); $uf->save();
@ -74,7 +85,7 @@ class FlightController extends AppBaseController
]); ]);
} }
elseif (strtolower($action) == 'remove') { elseif ($action === 'remove') {
try { try {
$uf = UserFlight::where($cols)->first(); $uf = UserFlight::where($cols)->first();
$uf->delete(); $uf->delete();

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Frontend; namespace App\Http\Controllers\Frontend;
use App\Repositories\Criteria\WhereCriteria;
use App\Services\PIREPService; use App\Services\PIREPService;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -45,9 +46,10 @@ class PirepController extends Controller
public function index(Request $request) public function index(Request $request)
{ {
$user = Auth::user(); $user = Auth::user();
$pireps = Pirep::where('user_id', $user->id)
->orderBy('created_at', 'desc') $where = ['user_id' => $user->id];
->paginate(); $this->pirepRepo->pushCriteria(new WhereCriteria($request, $where));
$pireps = $this->pirepRepo->orderBy('created_at', 'desc')->paginate();
return $this->view('pireps.index', [ return $this->view('pireps.index', [
'user' => $user, 'user' => $user,

View File

@ -0,0 +1,45 @@
<?php
namespace App\Repositories\Criteria;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;
/**
* Class RequestCriteria
* @package Prettus\Repository\Criteria
*/
class WhereCriteria implements CriteriaInterface
{
/**
* @var \Illuminate\Http\Request
*/
protected $request, $where;
public function __construct($request, $where)
{
$this->request = $request;
$this->where = $where;
}
/**
* Apply criteria in query repository
*
* @param Builder|Model $model
* @param RepositoryInterface $repository
*
* @return mixed
* @throws \Exception
*/
public function apply($model, RepositoryInterface $repository)
{
if($this->where) {
$model = $model->where($this->where);
}
return $model;
}
}

View File

@ -15,6 +15,11 @@
</div> </div>
</div> </div>
</div> </div>
<div class="row">
<div class="col-12 text-center">
{{ $flights->links('layouts.default.pagination.default') }}
</div>
</div>
@endsection @endsection
@section('scripts') @section('scripts')
<script> <script>

View File

@ -13,5 +13,10 @@
@include('layouts.default.pireps.table') @include('layouts.default.pireps.table')
</div> </div>
</div> </div>
<div class="row">
<div class="col-12 text-center">
{{ $pireps->links('layouts.default.pagination.default') }}
</div>
</div>
@endsection @endsection