Update source into github
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Post;
|
||||
|
||||
use App\Repositories\BaseCachedRepository;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class PostCachedRepository extends BaseCachedRepository implements PostRepositoryInterface
|
||||
{
|
||||
public function __construct(public PostRepository $postRepository)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
public function getModel()
|
||||
{
|
||||
return $this->postRepository->getModel();
|
||||
}
|
||||
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
||||
{
|
||||
$cacheKey = $this->getCacheKey(md5(json_encode([
|
||||
'filterItem' => [
|
||||
'data' => $data,
|
||||
'relationships' => $relationships,
|
||||
'select' => $select,
|
||||
'withCount' => $withCount
|
||||
]
|
||||
])));
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
|
||||
return $this->postRepository->filter($data, $relationships, $withCount, $select);
|
||||
});
|
||||
}
|
||||
|
||||
public function findBy(string $key = '',string $value = '') {
|
||||
$cacheKey = $this->getCacheKey(md5(json_encode([
|
||||
'findBy' => [
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
]
|
||||
])));
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key,$value) {
|
||||
return $this->postRepository->findBy($key,$value);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Repositories\BaseRepository;
|
||||
|
||||
class PostRepository extends BaseRepository implements PostRepositoryInterface
|
||||
{
|
||||
public function getModel()
|
||||
{
|
||||
return Post::class;
|
||||
}
|
||||
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
||||
{
|
||||
$model = $this->model->with($relationships)->select($select);
|
||||
|
||||
if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') {
|
||||
$model = $model->where("status", match (strtolower($data['status'])) {
|
||||
'published' => 1,
|
||||
'draft' => 2,
|
||||
'pending' => 0,
|
||||
});
|
||||
}
|
||||
|
||||
if (count($withCount) > 0) {
|
||||
$model = $model->withCount($withCount);
|
||||
}
|
||||
if (isset($data['limit']) && $data['limit']) {
|
||||
|
||||
return $model->latest('id')->take($data['limit'])->get();
|
||||
}
|
||||
if (isset($data['page']) && $data['page']) {
|
||||
|
||||
return $model->paginate(15);
|
||||
}
|
||||
return $model->get();
|
||||
}
|
||||
|
||||
public function findBy(string $key = '',string $value = '') {
|
||||
if(empty($key) || empty($value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return $this->model->where($key,$value)->first();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace App\Repositories\Post;
|
||||
|
||||
use App\Repositories\BaseRepositoryInterface;
|
||||
|
||||
interface PostRepositoryInterface extends BaseRepositoryInterface {
|
||||
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
|
||||
public function findBy(string $key = '',string $value = '');
|
||||
}
|
||||
Reference in New Issue
Block a user