mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 20:52:08 +00:00
47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Rooms;
|
|
|
|
use App\Data\Rooms\RoomPageData;
|
|
use App\Models\Player;
|
|
use App\Models\Room;
|
|
use App\Models\User;
|
|
|
|
class RoomPageService
|
|
{
|
|
public function __construct(
|
|
private readonly RoomStatisticsService $statistics,
|
|
) {}
|
|
|
|
public function prepare(Room $room, Player $currentPlayer, ?User $user): RoomPageData
|
|
{
|
|
$isFavorite = $user !== null
|
|
&& $user->favoriteRooms()->whereKey($room->id)->exists();
|
|
|
|
$room->load([
|
|
'players' => fn ($query) => $query
|
|
->active()
|
|
->select(['id', 'room_id', 'user_id', 'name', 'is_moderator', 'created_at'])
|
|
->orderByDesc('is_moderator')
|
|
->orderBy('created_at'),
|
|
'players.votes' => fn ($query) => $query
|
|
->where('room_id', $room->id)
|
|
->where('round', $room->current_round),
|
|
]);
|
|
|
|
$currentVote = $room->players
|
|
->firstWhere('id', $currentPlayer->id)
|
|
?->votes
|
|
->first()
|
|
?->value;
|
|
|
|
return new RoomPageData(
|
|
room: $room,
|
|
currentPlayer: $currentPlayer,
|
|
isFavorite: $isFavorite,
|
|
currentVote: $currentVote,
|
|
statistics: $this->statistics->currentRound($room),
|
|
);
|
|
}
|
|
}
|