mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 16:52:08 +00:00
71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Rooms;
|
|
|
|
use App\Models\Deck;
|
|
use App\Models\Player;
|
|
use App\Models\Room;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RoomCreationService
|
|
{
|
|
/**
|
|
* @return Collection<int, Deck>
|
|
*/
|
|
public function availableDecks(?User $user): Collection
|
|
{
|
|
return Deck::query()
|
|
->where(function ($query) use ($user) {
|
|
$query->where('is_system', true);
|
|
|
|
if ($user !== null) {
|
|
$query->orWhere('user_id', $user->id);
|
|
}
|
|
})
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'cards']);
|
|
}
|
|
|
|
/**
|
|
* @param array{deck_id: int, player_name: string, title?: string|null} $data
|
|
* @return array{Room, Player}
|
|
*/
|
|
public function create(array $data, ?User $user): array
|
|
{
|
|
$deck = Deck::query()
|
|
->whereKey($data['deck_id'])
|
|
->where(function ($query) use ($user) {
|
|
$query->where('is_system', true);
|
|
|
|
if ($user !== null) {
|
|
$query->orWhere('user_id', $user->id);
|
|
}
|
|
})
|
|
->firstOrFail();
|
|
|
|
return DB::transaction(function () use ($data, $deck, $user): array {
|
|
$room = Room::create([
|
|
'owner_id' => $user?->id,
|
|
'title' => $data['title'] ?? null,
|
|
'last_activity_at' => now(),
|
|
'deck_config' => [
|
|
'id' => $deck->id,
|
|
'name' => $deck->name,
|
|
'cards' => $deck->cards,
|
|
],
|
|
]);
|
|
|
|
$player = $room->players()->create([
|
|
'user_id' => $user?->id,
|
|
'name' => $data['player_name'],
|
|
'is_moderator' => true,
|
|
'last_seen_at' => now(),
|
|
]);
|
|
|
|
return [$room, $player];
|
|
});
|
|
}
|
|
}
|