mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-app.git
synced 2026-06-17 16:40:31 +00:00
- 🏗️ Introduced CoursePolicy and CourseEventPolicy for authorization.
- ✨ Added `StoreCourseRequest` and `UpdateCourseRequest` for structured validation. - ✨ Introduced `StoreCourseEventRequest` and `UpdateCourseEventRequest` for consistent request validation. - 🖼️ Created `CourseResource` and `CourseEventResource` for API responses. - 🔄 Refactored `CourseController` and `CourseEventController` to use Policies and FormRequests. - ✨ Added dedicated `uploadLogo` and `uploadAvatar` API endpoints with shared media validation. - 🚀 Improved API by aligning Course and CourseEvent behavior with other entities.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\MeetupEvents;
|
||||
|
||||
use App\Enums\RecurrenceType;
|
||||
use App\Models\MeetupEvent;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Persists a recurrence rule as concrete individual MeetupEvent records,
|
||||
* mirroring the Livewire editor: each occurrence is stored as a standalone
|
||||
* event without recurrence metadata.
|
||||
*/
|
||||
class CreateMeetupEventSeries
|
||||
{
|
||||
public function __construct(private ExpandRecurrenceSeries $expandRecurrenceSeries) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data Validated StoreMeetupEventRequest payload.
|
||||
* @return Collection<int, MeetupEvent>
|
||||
*/
|
||||
public function handle(array $data): Collection
|
||||
{
|
||||
$dates = $this->expandRecurrenceSeries->handle(
|
||||
Carbon::parse($data['start']),
|
||||
Carbon::parse($data['recurrence_end_date']),
|
||||
RecurrenceType::from($data['recurrence_type']),
|
||||
$data['recurrence_day_of_week'] ?? null,
|
||||
$data['recurrence_day_position'] ?? null,
|
||||
);
|
||||
|
||||
return collect($dates)->map(fn (Carbon $start): MeetupEvent => MeetupEvent::create([
|
||||
'meetup_id' => $data['meetup_id'],
|
||||
'start' => $start,
|
||||
'location' => $data['location'] ?? null,
|
||||
'description' => $data['description'] ?? null,
|
||||
'link' => $data['link'] ?? null,
|
||||
'attendees' => [],
|
||||
'might_attendees' => [],
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\MeetupEvents;
|
||||
|
||||
use App\Enums\RecurrenceType;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonInterface;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Expands a recurrence rule into the concrete list of start datetimes.
|
||||
*
|
||||
* This is the single source of truth shared by the Livewire event editor
|
||||
* (preview + persist) and the REST API. It is timezone-agnostic: it operates
|
||||
* on the Carbon instances it receives and preserves their timezone, leaving
|
||||
* any UTC normalization to the caller.
|
||||
*/
|
||||
class ExpandRecurrenceSeries
|
||||
{
|
||||
/**
|
||||
* Hard upper bound on the number of generated occurrences.
|
||||
*/
|
||||
public const MAX_OCCURRENCES = 100;
|
||||
|
||||
/**
|
||||
* @return array<int, Carbon>
|
||||
*/
|
||||
public function handle(
|
||||
CarbonInterface $start,
|
||||
CarbonInterface $end,
|
||||
RecurrenceType $type,
|
||||
?string $dayOfWeek = null,
|
||||
?string $dayPosition = null,
|
||||
): array {
|
||||
$start = $start->copy();
|
||||
$end = $end->copy();
|
||||
|
||||
if ($dayOfWeek && $dayPosition) {
|
||||
return $this->customRecurrence($start, $end, $dayOfWeek, $dayPosition);
|
||||
}
|
||||
|
||||
if ($type === RecurrenceType::Weekly && $dayOfWeek) {
|
||||
$dayOfWeekNumber = self::dayOfWeekNumber($dayOfWeek);
|
||||
|
||||
if ($dayOfWeekNumber !== null) {
|
||||
$cursor = $start->copy();
|
||||
|
||||
while ($cursor->dayOfWeek !== $dayOfWeekNumber) {
|
||||
$cursor->addDay();
|
||||
}
|
||||
|
||||
return $this->collect($cursor, $end, fn (Carbon $date) => $date->addWeek());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->collect(
|
||||
$start,
|
||||
$end,
|
||||
fn (Carbon $date) => $type === RecurrenceType::Weekly ? $date->addWeek() : $date->addMonth(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure(Carbon): mixed $advance
|
||||
* @return array<int, Carbon>
|
||||
*/
|
||||
private function collect(CarbonInterface $cursor, CarbonInterface $end, Closure $advance): array
|
||||
{
|
||||
$dates = [];
|
||||
$current = $cursor->copy();
|
||||
|
||||
while ($current->lessThanOrEqualTo($end) && count($dates) < self::MAX_OCCURRENCES) {
|
||||
$dates[] = $current->copy();
|
||||
$advance($current);
|
||||
}
|
||||
|
||||
return $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Carbon>
|
||||
*/
|
||||
private function customRecurrence(CarbonInterface $start, CarbonInterface $end, string $dayOfWeek, string $dayPosition): array
|
||||
{
|
||||
$dates = [];
|
||||
$cursor = $start->copy()->startOfMonth();
|
||||
|
||||
while ($cursor->lessThanOrEqualTo($end) && count($dates) < self::MAX_OCCURRENCES) {
|
||||
$occurrence = $this->findOccurrence($cursor, $dayOfWeek, $dayPosition);
|
||||
|
||||
if ($occurrence && $occurrence->lessThanOrEqualTo($end)) {
|
||||
$occurrenceWithTime = $occurrence->copy()->setTimeFrom($start);
|
||||
|
||||
if ($occurrenceWithTime->greaterThanOrEqualTo($start)) {
|
||||
$dates[] = $occurrenceWithTime;
|
||||
}
|
||||
|
||||
$cursor = $cursor->copy()->addMonth();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $dates;
|
||||
}
|
||||
|
||||
private function findOccurrence(CarbonInterface $monthCursor, string $dayOfWeek, string $dayPosition): ?Carbon
|
||||
{
|
||||
$dayOfWeekNumber = self::dayOfWeekNumber($dayOfWeek);
|
||||
$dayPositionNumber = self::dayPositionNumber($dayPosition);
|
||||
|
||||
if ($dayOfWeekNumber === null || $dayPositionNumber === null) {
|
||||
return $monthCursor->copy();
|
||||
}
|
||||
|
||||
$date = $monthCursor->copy()->startOfMonth();
|
||||
|
||||
if ($dayPositionNumber === -1) {
|
||||
return $date->lastOfMonth($dayOfWeekNumber)
|
||||
->setTime($monthCursor->hour, $monthCursor->minute, $monthCursor->second);
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
|
||||
while ($date->month === $monthCursor->month) {
|
||||
if ($date->dayOfWeek === $dayOfWeekNumber) {
|
||||
$count++;
|
||||
|
||||
if ($count === $dayPositionNumber) {
|
||||
return $date->copy()
|
||||
->setTime($monthCursor->hour, $monthCursor->minute, $monthCursor->second);
|
||||
}
|
||||
}
|
||||
|
||||
$date->addDay();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function dayOfWeekNumber(string $day): ?int
|
||||
{
|
||||
return match (strtolower($day)) {
|
||||
'monday', 'montag' => Carbon::MONDAY,
|
||||
'tuesday', 'dienstag' => Carbon::TUESDAY,
|
||||
'wednesday', 'mittwoch' => Carbon::WEDNESDAY,
|
||||
'thursday', 'donnerstag' => Carbon::THURSDAY,
|
||||
'friday', 'freitag' => Carbon::FRIDAY,
|
||||
'saturday', 'samstag' => Carbon::SATURDAY,
|
||||
'sunday', 'sonntag' => Carbon::SUNDAY,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
private static function dayPositionNumber(string $position): ?int
|
||||
{
|
||||
return match (strtolower($position)) {
|
||||
'first', 'erster' => 1,
|
||||
'second', 'zweiter' => 2,
|
||||
'third', 'dritter' => 3,
|
||||
'fourth', 'vierter' => 4,
|
||||
'last', 'letzter' => -1,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user