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