Adding extra server side only method

parent 95468324
......@@ -2,6 +2,7 @@
namespace Modules\Page\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Page\Entities\Page;
use Modules\Page\Http\Requests\CreatePageRequest;
......@@ -9,6 +10,8 @@ use Modules\Page\Http\Requests\UpdatePageRequest;
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
{
......@@ -27,6 +30,43 @@ class PageController extends Controller
return PageTransformer::collection($this->page->all());
}
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->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);
}
}
return PageTransformer::collection($pages->paginate($request->get('per_page', 10)));
}
public function store(CreatePageRequest $request)
{
$this->page->create($request->all());
......
......@@ -13,6 +13,11 @@ $router->group(['prefix' => '/page'], function (Router $router) {
'uses' => 'PageController@index',
'middleware' => 'token-can:page.pages.index',
]);
$router->get('pages-server-side', [
'as' => 'api.page.page.indexServerSide',
'uses' => 'PageController@indexServerSide',
'middleware' => 'token-can:page.pages.index',
]);
$router->delete('pages/{page}', [
'as' => 'api.page.page.destroy',
'uses' => 'PageController@destroy',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment