91 lines
2.4 KiB
PHP
91 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Investment\Repositories;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Modules\Investment\Entities\InvestmentLimitProfile;
|
|
use Modules\Investment\Repositories\Contracts\InvestmentLimitProfileRepositoryInterface;
|
|
|
|
class InvestmentLimitProfileRepository implements InvestmentLimitProfileRepositoryInterface
|
|
{
|
|
protected function baseQuery()
|
|
{
|
|
return InvestmentLimitProfile::query()->orderBy('name');
|
|
}
|
|
|
|
protected function applySearch($query, string $search)
|
|
{
|
|
if ($search !== '') {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'ILIKE', "%{$search}%")
|
|
->orWhere('description', 'ILIKE', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function getAllPaginated(int $perPage = 10, string $search = '')
|
|
{
|
|
$query = $this->baseQuery();
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getAllWithRelationsPaginated(
|
|
int $perPage = 10,
|
|
string $search = '',
|
|
string $sortBy = 'name',
|
|
string $sortOrder = 'asc'
|
|
) {
|
|
$allowedSortColumns = ['id', 'name', 'created_at'];
|
|
$sortBy = in_array($sortBy, $allowedSortColumns, true) ? $sortBy : 'name';
|
|
$sortOrder = strtolower($sortOrder) === 'desc' ? 'desc' : 'asc';
|
|
|
|
$query = InvestmentLimitProfile::query()
|
|
->withCount('profileLimits')
|
|
->orderBy($sortBy, $sortOrder);
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getAllWithRelations(string $search = ''): Collection
|
|
{
|
|
$query = $this->baseQuery()->with('profileLimits');
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function create(array $data): InvestmentLimitProfile
|
|
{
|
|
return InvestmentLimitProfile::create($data);
|
|
}
|
|
|
|
public function findById(string $id): ?InvestmentLimitProfile
|
|
{
|
|
return InvestmentLimitProfile::query()->find($id);
|
|
}
|
|
|
|
public function delete(string $id): bool
|
|
{
|
|
$profile = $this->findById($id);
|
|
|
|
if ($profile) {
|
|
return (bool) $profile->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function all(string $search = ''): Collection
|
|
{
|
|
$query = $this->baseQuery();
|
|
$this->applySearch($query, $search);
|
|
|
|
return $query->get();
|
|
}
|
|
}
|