88 lines
3.0 KiB
PHP
88 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Group;
|
|
|
|
use App\Models\Group;
|
|
use App\Repositories\Addon\AddonRepositoryInterface;
|
|
use App\Repositories\BaseRepository;
|
|
use App\Repositories\Domain\DomainRepositoryInterface;
|
|
use App\Repositories\Hosting\HostingRepositoryInterface;
|
|
use App\Repositories\VPS\VPSRepositoryInterface;
|
|
|
|
class GroupRepository extends BaseRepository implements GroupRepositoryInterface
|
|
{
|
|
public function __construct(
|
|
public VPSRepositoryInterface $vPSRepository,
|
|
public HostingRepositoryInterface $hostingRepository,
|
|
public AddonRepositoryInterface $addonRepository,
|
|
public DomainRepositoryInterface $domainRepository
|
|
)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function getModel()
|
|
{
|
|
return Group::class;
|
|
}
|
|
|
|
public function getProduct(int $group_id = 0, int $type_id = 0)
|
|
{
|
|
return match ($type_id) {
|
|
3 => $this->hostingRepository->filter(['group_id' => $group_id]),
|
|
4 => $this->vPSRepository->filter(['group_id' => $group_id]),
|
|
default => null
|
|
};
|
|
}
|
|
|
|
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
|
{
|
|
$model = $this->model->with($relationships)->select($select);
|
|
|
|
if (isset($data['status'])) {
|
|
$model = $model->where("status", $data['status']);
|
|
}
|
|
if (isset($data['relations'])) {
|
|
foreach ($data['relations'] as $key => $value) {
|
|
$model = $model->whereHas($key, function ($query) use ($value) {
|
|
if (isset($value['condition'])) {
|
|
switch (strtoupper($value['condition'])) {
|
|
case "IN":
|
|
$query->whereIn($value['key'], $value['value']);
|
|
break;
|
|
case "NotIn":
|
|
$query->whereNotIn($value['key'], $value['value']);
|
|
break;
|
|
default:
|
|
$query->where($value['key'], $value['condition'], $value['value']);
|
|
break;
|
|
}
|
|
} else {
|
|
$query->where($value['key'], $value['value']);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
if (count($withCount) > 0) {
|
|
$model = $model->withCount($withCount);
|
|
}
|
|
if (isset($data['page'])) {
|
|
return $model->paginate(15);
|
|
}
|
|
|
|
return $model->get();
|
|
}
|
|
|
|
public function findBySlugWithProduct(string $slug)
|
|
{
|
|
return $this->model->where('slug', $slug)
|
|
->where('status', 1) // Only active groups
|
|
->with(['hostings' => function ($query) {
|
|
$query->where('status', 1); // Only active hostings
|
|
}, 'vps' => function ($query) {
|
|
$query->where('status', 1); // Only active VPS
|
|
}])
|
|
->first();
|
|
}
|
|
}
|