Skip to content

Commit

Permalink
Pagination on the pireps/flights on the front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Dec 4, 2017
1 parent 5593d01 commit 6bb0a52
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
23 changes: 17 additions & 6 deletions app/Http/Controllers/Frontend/FlightController.php
Expand Up @@ -7,8 +7,11 @@
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;

class FlightController extends AppBaseController
{
Expand All @@ -28,9 +31,9 @@ public function index(Request $request)
$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())
->pluck('flight_id')->toArray();

Expand All @@ -40,10 +43,18 @@ public function index(Request $request)
]);
}

/**
* 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)
{
$where = ['active' => true];
$flights = $this->flightRepo->findWhere($where);

$this->flightRepo->pushCriteria(new RequestCriteria($request));
$flights = $this->flightRepo->paginate();

// TODO: PAGINATION

Expand All @@ -60,11 +71,11 @@ public function save(Request $request)
{
$user_id = Auth::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];

if(strtolower($action) == 'save') {
if($action === 'save') {
$uf = UserFlight::create($cols);
$uf->save();

Expand All @@ -74,7 +85,7 @@ public function save(Request $request)
]);
}

elseif (strtolower($action) == 'remove') {
elseif ($action === 'remove') {
try {
$uf = UserFlight::where($cols)->first();
$uf->delete();
Expand Down
8 changes: 5 additions & 3 deletions app/Http/Controllers/Frontend/PirepController.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Frontend;

use App\Repositories\Criteria\WhereCriteria;
use App\Services\PIREPService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -45,9 +46,10 @@ public function __construct(
public function index(Request $request)
{
$user = Auth::user();
$pireps = Pirep::where('user_id', $user->id)
->orderBy('created_at', 'desc')
->paginate();

$where = ['user_id' => $user->id];
$this->pirepRepo->pushCriteria(new WhereCriteria($request, $where));
$pireps = $this->pirepRepo->orderBy('created_at', 'desc')->paginate();

return $this->view('pireps.index', [
'user' => $user,
Expand Down
45 changes: 45 additions & 0 deletions app/Repositories/Criteria/WhereCriteria.php
@@ -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;
}
}
5 changes: 5 additions & 0 deletions resources/views/layouts/default/flights/index.blade.php
Expand Up @@ -15,6 +15,11 @@
</div>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
{{ $flights->links('layouts.default.pagination.default') }}
</div>
</div>
@endsection
@section('scripts')
<script>
Expand Down
5 changes: 5 additions & 0 deletions resources/views/layouts/default/pireps/index.blade.php
Expand Up @@ -13,5 +13,10 @@
@include('layouts.default.pireps.table')
</div>
</div>
<div class="row">
<div class="col-12 text-center">
{{ $pireps->links('layouts.default.pagination.default') }}
</div>
</div>
@endsection

0 comments on commit 6bb0a52

Please sign in to comment.