Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

3.0 KB
Raw Blame
<?php

namespace HiEvents\Repository\Eloquent;

use Carbon\CarbonInterface;
use HiEvents\DomainObjects\EventOccurrenceDomainObject;
use HiEvents\DomainObjects\Generated\EventOccurrenceDomainObjectAbstract;
use HiEvents\DomainObjects\Status\EventOccurrenceStatus;
use HiEvents\Http\DTO\QueryParamsDTO;
use HiEvents\Models\EventOccurrence;
use HiEvents\Repository\Interfaces\EventOccurrenceRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;

class EventOccurrenceRepository extends BaseRepository implements EventOccurrenceRepositoryInterface
{
    protected function getModel(): string
    {
        return EventOccurrence::class;
    }

    public function getDomainObject(): string
    {
        return EventOccurrenceDomainObject::class;
    }

    public function findByIdLocked(int $id): ?EventOccurrenceDomainObject
    {
        $model = EventOccurrence::query()
            ->where('id', $id)
            ->lockForUpdate()
            ->first();

        if ($model === null) {
            return null;
        }

        return $this->handleSingleResult($model);
    }

    public function findUpcomingIdsForEvent(int $eventId, ?CarbonInterface $endedAfter = null): array
    {
        return $this->runQuery(fn () => EventOccurrence::query()
            ->where(EventOccurrenceDomainObjectAbstract::EVENT_ID, $eventId)
            ->where(EventOccurrenceDomainObjectAbstract::STATUS, '!=', EventOccurrenceStatus::CANCELLED->name)
            ->whereRaw('COALESCE(end_date, start_date) > ?', [$endedAfter ?? now()])
            ->orderBy(EventOccurrenceDomainObjectAbstract::START_DATE)
            ->pluck(EventOccurrenceDomainObjectAbstract::ID)
            ->map(static fn (int|string $id) => (int) $id)
            ->all());
    }

    public function findByEventId(int $eventId, QueryParamsDTO $params): LengthAwarePaginator
    {
        $this->model = $this->model->newQuery()->orderBy(
            column: $this->validateSortColumn($params->sort_by, EventOccurrenceDomainObject::class),
            direction: $this->validateSortDirection($params->sort_direction, EventOccurrenceDomainObject::class),
        )->orderBy(EventOccurrenceDomainObjectAbstract::ID);

        if (! empty($params->filter_fields)) {
            $this->applyFilterFields($params, EventOccurrenceDomainObject::getAllowedFilterFields());

            $timePeriod = $params->filter_fields->firstWhere('field', 'time_period');
            if ($timePeriod) {
                $now = now()->toDateTimeString();
                if ($timePeriod->value === 'upcoming') {
                    $this->model = $this->model->whereRaw('COALESCE(end_date, start_date) >= ?', [$now]);
                } elseif ($timePeriod->value === 'past') {
                    $this->model = $this->model->whereRaw('COALESCE(end_date, start_date) < ?', [$now]);
                }
            }
        }

        return $this->paginateWhere(
            where: [
                EventOccurrenceDomainObjectAbstract::EVENT_ID => $eventId,
            ],
            limit: $params->per_page,
            page: $params->page,
        );
    }
}