Files

196 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2026-03-15 17:08:23 +07:00
<?php
namespace Modules\RestaurantDelivery\Repositories;
use App\Abstracts\EntityRepository;
use Exception;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Str;
use Modules\RestaurantDelivery\Models\DeliveryZone;
use Symfony\Component\HttpFoundation\Response;
class DeliveryZoneRepository extends EntityRepository
{
public string $table = DeliveryZone::TABLE_NAME;
protected array $fillableColumns = [
'restaurant_id',
'name',
'slug',
'description',
'color',
'coordinates',
'min_lat',
'max_lat',
'min_lng',
'max_lng',
'priority',
'is_active',
'is_default',
'max_delivery_distance',
'operating_hours',
];
protected function getQuery(): Builder
{
return parent::getQuery();
}
protected function getFilterData(array $filterData = []): array
{
$defaultArgs = [
'perPage' => 10,
'search' => '',
'orderBy' => 'id',
'order' => 'desc',
'with_deleted' => false,
];
return array_merge($defaultArgs, $filterData);
}
private function getDeliveryZoneQuery(): Builder
{
return $this->getQuery()
->select(
"{$this->table}.id",
"{$this->table}.uuid",
"{$this->table}.restaurant_id",
"{$this->table}.name",
"{$this->table}.slug",
"{$this->table}.description",
"{$this->table}.color",
"{$this->table}.coordinates",
"{$this->table}.min_lat",
"{$this->table}.max_lat",
"{$this->table}.min_lng",
"{$this->table}.max_lng",
"{$this->table}.priority",
"{$this->table}.is_active",
"{$this->table}.is_default",
"{$this->table}.max_delivery_distance",
"{$this->table}.operating_hours",
"{$this->table}.created_at",
"{$this->table}.deleted_at"
);
}
protected function filterSearchQuery(Builder|EloquentBuilder $query, string $searchedText): Builder
{
$searchable = "%$searchedText%";
return $query->where("{$this->table}.name", 'LIKE', $searchable)
->orWhere("{$this->table}.status", 'LIKE', $searchable);
}
public function getAll(array $filterData = []): Paginator
{
$filter = $this->getFilterData($filterData);
$query = $this->getDeliveryZoneQuery();
if (! $filter['with_deleted']) {
$query->whereNull("{$this->table}.deleted_at");
}
if (! empty($filter['search'])) {
$query = $this->filterSearchQuery($query, $filter['search']);
}
return $query
->orderBy($filter['orderBy'], $filter['order'])
->paginate($filter['perPage']);
}
public function getCount(array $filterData = []): int
{
$filter = $this->getFilterData($filterData);
$query = $this->getQuery();
if (! $filter['with_deleted']) {
$query->whereNull("{$this->table}.deleted_at");
}
return $query->count();
}
/**
* @throws Exception
*/
public function getByColumn(string $columnName, $columnValue, array $selects = ['*']): ?object
{
$item = $this->getDeliveryZoneQuery()
->where($columnName, $columnValue)
->first($selects);
if (empty($item)) {
throw new Exception(
$this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE),
Response::HTTP_NOT_FOUND
);
}
return $item;
}
/**
* @throws Exception
*/
public function create(array $data): object
{
$data = $this->prepareForDB($data);
$id = $this->getQuery()->insertGetId($data);
return DeliveryZone::find($id);
}
/**
* @throws Exception
*/
public function update(int $id, array $data): object
{
$item = DeliveryZone::findOrFail($id);
$data = $this->prepareForDB($data, $item);
parent::update($id, $data);
return $this->getById($id);
}
/**
* @throws Exception
*/
public function prepareForDB(array $data, ?object $item = null): array
{
$data = parent::prepareForDB($data, $item);
// Encode coordinates and operating hours
if (isset($data['coordinates'])) {
$data['coordinates'] = json_encode($data['coordinates']);
}
if (isset($data['operating_hours'])) {
$data['operating_hours'] = json_encode($data['operating_hours']);
}
if (empty($item)) {
// Generate UUID for new records
$data['uuid'] = (string) Str::uuid();
$data['created_at'] = now();
$data['restaurant_id'] = $this->getCurrentRestaurantId();
} else {
$data['updated_at'] = now();
}
return $data;
}
protected function getExceptionMessages(): array
{
return [
static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => 'DeliveryZone does not exist.',
static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => 'DeliveryZone could not be deleted.',
];
}
}