Initial commit
This commit is contained in:
34
api/app/Actions/Events/UpdateEventAction.php
Normal file
34
api/app/Actions/Events/UpdateEventAction.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Events;
|
||||
|
||||
use App\Models\Event;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UpdateEventAction
|
||||
{
|
||||
public function execute(Event $event, array $data): Event
|
||||
{
|
||||
return \Illuminate\Support\Facades\DB::transaction(function () use ($event, $data) {
|
||||
if (isset($data['slug']) && $data['slug'] !== $event->slug) {
|
||||
$data['slug'] = $this->generateUniqueSlug($data['slug'], $event->id);
|
||||
}
|
||||
$event->update($data);
|
||||
|
||||
return $event->fresh();
|
||||
});
|
||||
}
|
||||
|
||||
protected function generateUniqueSlug(string $base, int $excludeId): string
|
||||
{
|
||||
$slug = Str::slug($base);
|
||||
$original = $slug;
|
||||
$count = 0;
|
||||
while (Event::where('slug', $slug)->where('id', '!=', $excludeId)->exists()) {
|
||||
$count++;
|
||||
$slug = $original.'-'.$count;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user