Update source into github
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Module\Host;
|
||||
|
||||
use App\Repositories\BaseCachedRepository;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class HostCachedRepository extends BaseCachedRepository implements HostRepositoryInterface
|
||||
{
|
||||
public function __construct(
|
||||
public HostRepository $hostRepository
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
public function getModel()
|
||||
{
|
||||
return $this->hostRepository->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->hostRepository->filter($data, $relationships, $withCount, $select);
|
||||
});
|
||||
}
|
||||
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
|
||||
{
|
||||
$cacheKey = $this->getCacheKey(md5(json_encode([
|
||||
'findWithRelationships' => [
|
||||
'id' => $id,
|
||||
'relationships' => $relationships
|
||||
]
|
||||
])));
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
|
||||
return $this->hostRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
|
||||
});
|
||||
}
|
||||
public function findBy(string $key = '', string|int|null $value = '')
|
||||
{
|
||||
$cacheKey = $this->getCacheKey(md5(json_encode([
|
||||
'findBy' => [
|
||||
'key' => $key,
|
||||
'value' => $value
|
||||
]
|
||||
])));
|
||||
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value) {
|
||||
return $this->hostRepository->findBy($key, $value);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Module\Host;
|
||||
|
||||
use App\Models\ModuleHost;
|
||||
use App\Repositories\BaseRepository;
|
||||
|
||||
class HostRepository extends BaseRepository implements HostRepositoryInterface
|
||||
{
|
||||
public function getModel()
|
||||
{
|
||||
return ModuleHost::class;
|
||||
}
|
||||
|
||||
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
||||
{
|
||||
$model = $this->model->with($relationships)->select($select);
|
||||
|
||||
if (isset($data['module_datacenter_id']) && $data['module_datacenter_id']) {
|
||||
$model = $model->where("module_datacenter_id", $data['module_datacenter_id']);
|
||||
}
|
||||
|
||||
if (count($withCount) > 0) {
|
||||
$model = $model->withCount($withCount);
|
||||
}
|
||||
|
||||
if (isset($data['page'])) {
|
||||
return $model->paginate(15);
|
||||
}
|
||||
|
||||
return $model->get();
|
||||
}
|
||||
|
||||
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
|
||||
{
|
||||
$model = $this->model;
|
||||
if (count($relationships) > 0) {
|
||||
foreach ($relationships as $relationship) {
|
||||
if (method_exists($model, $relationship)) {
|
||||
$model = $model->with($relationship);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Xử lý withCount
|
||||
if (count($withCount) > 0) {
|
||||
foreach ($withCount as $key => $items) {
|
||||
if (is_array($items)) {
|
||||
foreach ($items as $alias => $item) {
|
||||
$model = $model->withCount([
|
||||
$key . ' as ' . $alias => function ($query) use ($item) {
|
||||
$query->where($item['key'], $item['condition'], $item['value']);
|
||||
}
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$model = $model->withCount($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Xử lý withSum
|
||||
if (count($withSum) > 0) {
|
||||
foreach ($withSum as $key => $items) {
|
||||
if (is_array($items)) {
|
||||
foreach ($items as $alias => $item) {
|
||||
$model = $model->withSum([
|
||||
$key . ' as ' . $alias => function ($query) use ($item) {
|
||||
$query->where($item['key'], $item['condition'], $item['value']);
|
||||
}
|
||||
], $item['sum']);
|
||||
}
|
||||
} else {
|
||||
$model = $model->withSum($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $model->find($id);
|
||||
}
|
||||
|
||||
public function findBy(string $key = '', string|int|null $value = '')
|
||||
{
|
||||
if (empty($key) || empty($value)) {
|
||||
return false;
|
||||
}
|
||||
return $this->model->where($key, $value)->first();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace App\Repositories\Module\Host;
|
||||
|
||||
use App\Repositories\BaseRepositoryInterface;
|
||||
|
||||
interface HostRepositoryInterface extends BaseRepositoryInterface{
|
||||
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
|
||||
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
|
||||
public function findBy(string $key = '',string|int|null $value = '' );
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user