mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 20:12:10 +00:00
fix: refactored the codebase
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?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];
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user