Skip to content

Commit

Permalink
Moving logic out of page controller to its repository
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Sep 16, 2017
1 parent 2a327fe commit 02601a1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 43 deletions.
44 changes: 1 addition & 43 deletions Modules/Page/Http/Controllers/Api/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Modules\Page\Repositories\PageRepository;
use Modules\Page\Transformers\FullPageTransformer;
use Modules\Page\Transformers\PageTransformer;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Facades\DataTables;

class PageController extends Controller
{
Expand All @@ -32,47 +30,7 @@ public function index()

public function indexServerSide(Request $request)
{
$pages = $this->page->allWithBuilder();
// $test = new EloquentDataTable(Page::query());
// dd($test->make());
// $test = DataTables::eloquent(Page::query())->make();
// dd($test);


if ($request->has('search')) {
$term = $request->get('search');
$pages->whereHas('translations', function($query) use($term) {
$query->where('title', 'LIKE', "%{$term}%");
$query->orWhere('slug', 'LIKE', "%{$term}%");
})
->orWhere('id', $term);
}

if ($request->has('order_by') && $request->get('order_by') !== 'undefined' && $request->get('order') !== 'null') {
$order = $request->get('order') === 'ascending' ? 'asc' : 'desc';

if (str_contains($request->get('order_by'), '.')) {
$fields = explode('.', $request->get('order_by'));

$pages->with('translations')->join('page__page_translations as t', function ($join) {
$join->on('page__pages.id', '=', 't.page_id');
})
->where('t.locale', locale())
->groupBy('page__pages.id')->orderBy("t.{$fields[1]}", $order);
} else {
$pages->orderBy($request->get('order_by'), $order);
}
}
//dd($pages->toSql());

// $pages->with('translations')->join('page__page_translations as t', function ($join) {
// $join->on('page__pages.id', '=', 't.page_id');
// })->where('t.locale', locale())
// ->groupBy('page__pages.id')->orderBy("t.title", 'desc');

//dd($pages->take(10)->get());
// return PageTransformer::collection($pages->take(10)->get());
return PageTransformer::collection($pages->paginate($request->get('per_page', 10)));
return PageTransformer::collection($this->page->serverPaginationFilteringFor($request));
}

public function store(CreatePageRequest $request)
Expand Down
25 changes: 25 additions & 0 deletions Modules/Page/Repositories/Cache/CachePageDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Modules\Page\Repositories\Cache;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Modules\Core\Repositories\Cache\BaseCacheDecorator;
use Modules\Page\Repositories\PageRepository;

Expand Down Expand Up @@ -65,4 +67,27 @@ function () use ($slug, $locale) {
}
);
}

/**
* Paginating, ordering and searching through pages for server side index table
* @param Request $request
* @return LengthAwarePaginator
*/
public function serverPaginationFilteringFor(Request $request): LengthAwarePaginator
{
$order = $request->get('order');
$orderBy = $request->get('order_by');
$perPage = $request->get('per_page');
$search = $request->get('search');

$key = "{$order}-{$orderBy}-{$perPage}-{$search}";

return $this->cache
->tags([$this->entityName, 'global'])
->remember("{$this->locale}.{$this->entityName}.serverPaginationFilteringFor.{$key}", $this->cacheTime,
function () use ($request) {
return $this->repository->serverPaginationFilteringFor($request);
}
);
}
}
44 changes: 44 additions & 0 deletions Modules/Page/Repositories/Eloquent/EloquentPageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Modules\Page\Repositories\Eloquent;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Page\Events\PageIsCreating;
use Modules\Page\Events\PageIsUpdating;
Expand Down Expand Up @@ -127,4 +129,46 @@ private function removeOtherHomepage($pageId = null)
$homepage->is_home = 0;
$homepage->save();
}

/**
* Paginating, ordering and searching through pages for server side index table
* @param Request $request
* @return LengthAwarePaginator
*/
public function serverPaginationFilteringFor(Request $request): LengthAwarePaginator
{
$pages = $this->allWithBuilder();

if ($request->has('search')) {
$term = $request->get('search');
$pages->whereHas('translations', function($query) use($term) {
$query->where('title', 'LIKE', "%{$term}%");
$query->orWhere('slug', 'LIKE', "%{$term}%");
})
->orWhere('id', $term);
}

if ($request->has('order_by') && $request->get('order_by') !== 'undefined' && $request->get('order') !== 'null') {
$order = $request->get('order') === 'ascending' ? 'asc' : 'desc';

if (str_contains($request->get('order_by'), '.')) {
$fields = explode('.', $request->get('order_by'));

$pages->with('translations')->join('page__page_translations as t', function ($join) {
$join->on('page__pages.id', '=', 't.page_id');
})
->where('t.locale', locale())
->groupBy('page__pages.id')->orderBy("t.{$fields[1]}", $order);
} else {
$pages->orderBy($request->get('order_by'), $order);
}
}
//dd($pages->toSql());

// $pages->with('translations')->join('page__page_translations as t', function ($join) {
// $join->on('page__pages.id', '=', 't.page_id');
// })->where('t.locale', locale())
// ->groupBy('page__pages.id')->orderBy("t.title", 'desc');
return $pages->paginate($request->get('per_page', 10));
}
}
9 changes: 9 additions & 0 deletions Modules/Page/Repositories/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Modules\Page\Repositories;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Modules\Core\Repositories\BaseRepository;

interface PageRepository extends BaseRepository
Expand All @@ -24,4 +26,11 @@ public function countAll();
* @return object
*/
public function findBySlugInLocale($slug, $locale);

/**
* Paginating, ordering and searching through pages for server side index table
* @param Request $request
* @return LengthAwarePaginator
*/
public function serverPaginationFilteringFor(Request $request) : LengthAwarePaginator;
}

0 comments on commit 02601a1

Please sign in to comment.