Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
02601a1b
Unverified
Commit
02601a1b
authored
Sep 16, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moving logic out of page controller to its repository
parent
2a327fea
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
43 deletions
+79
-43
PageController.php
Modules/Page/Http/Controllers/Api/PageController.php
+1
-43
CachePageDecorator.php
Modules/Page/Repositories/Cache/CachePageDecorator.php
+25
-0
EloquentPageRepository.php
...les/Page/Repositories/Eloquent/EloquentPageRepository.php
+44
-0
PageRepository.php
Modules/Page/Repositories/PageRepository.php
+9
-0
No files found.
Modules/Page/Http/Controllers/Api/PageController.php
View file @
02601a1b
...
...
@@ -10,8 +10,6 @@ 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
{
...
...
@@ -32,47 +30,7 @@ class PageController extends Controller
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
)
...
...
Modules/Page/Repositories/Cache/CachePageDecorator.php
View file @
02601a1b
...
...
@@ -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
;
...
...
@@ -65,4 +67,27 @@ class CachePageDecorator extends BaseCacheDecorator implements PageRepository
}
);
}
/**
* 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
);
}
);
}
}
Modules/Page/Repositories/Eloquent/EloquentPageRepository.php
View file @
02601a1b
...
...
@@ -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
;
...
...
@@ -127,4 +129,46 @@ class EloquentPageRepository extends EloquentBaseRepository implements PageRepos
$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
));
}
}
Modules/Page/Repositories/PageRepository.php
View file @
02601a1b
...
...
@@ -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
...
...
@@ -24,4 +26,11 @@ interface PageRepository extends BaseRepository
* @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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment