90 lines
2.3 KiB
PHP
90 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\User\Repositories;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Modules\User\Entities\Address;
|
|
use Modules\User\Repositories\Contracts\AddressRepositoryInterface;
|
|
|
|
class AddressRepository implements AddressRepositoryInterface
|
|
{
|
|
protected function baseQuery()
|
|
{
|
|
return Address::query()
|
|
->where('user_id', auth()->id())
|
|
->orderBy('address_type');
|
|
}
|
|
|
|
public function getAllPaginated(int $perPage = 10, string $search = '')
|
|
{
|
|
$query = $this->baseQuery();
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getAllWithRelationsPaginated(
|
|
int $perPage = 10,
|
|
string $search = '',
|
|
string $sortBy = 'address_type',
|
|
string $sortOrder = 'asc'
|
|
) {
|
|
$allowedSortColumns = [
|
|
'id',
|
|
'address_type',
|
|
'address_line_1',
|
|
'city',
|
|
'state',
|
|
'postcode',
|
|
'country',
|
|
'created_at',
|
|
];
|
|
$sortBy = in_array($sortBy, $allowedSortColumns, true) ? $sortBy : 'address_type';
|
|
$sortOrder = strtolower($sortOrder) === 'desc' ? 'desc' : 'asc';
|
|
|
|
$query = Address::query()
|
|
->where('user_id', auth()->id())
|
|
->with(['user:id,name,email'])
|
|
->orderBy($sortBy, $sortOrder);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
public function getAllWithRelations(string $search = ''): Collection
|
|
{
|
|
$query = Address::query()
|
|
->where('user_id', auth()->id())
|
|
->with(['user:id,name,email'])
|
|
->orderBy('address_type');
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function create(array $data): Address
|
|
{
|
|
return Address::create($data);
|
|
}
|
|
|
|
public function findById(string $id): ?Address
|
|
{
|
|
return Address::query()
|
|
->where('user_id', auth()->id())
|
|
->with(['user:id,name,email'])
|
|
->find($id);
|
|
}
|
|
|
|
public function delete(string $id): bool
|
|
{
|
|
$address = $this->findById($id);
|
|
if ($address) {
|
|
return $address->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function all(string $search = ''): Collection
|
|
{
|
|
return $this->baseQuery()->get();
|
|
}
|
|
}
|