2024-01-01 11:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\QueueState;
|
2024-05-19 05:49:42 +00:00
|
|
|
use App\Models\Song;
|
2024-01-01 11:40:21 +00:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\SongRepository;
|
|
|
|
use App\Values\QueueState as QueueStateDTO;
|
|
|
|
|
|
|
|
class QueueService
|
|
|
|
{
|
2024-04-18 14:36:28 +00:00
|
|
|
public function __construct(private readonly SongRepository $songRepository)
|
2024-01-01 11:40:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQueueState(User $user): QueueStateDTO
|
|
|
|
{
|
|
|
|
$state = QueueState::query()->where('user_id', $user->id)->firstOrCreate([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
], [
|
|
|
|
'song_ids' => [],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$currentSong = $state->current_song_id ? $this->songRepository->findOne($state->current_song_id, $user) : null;
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
return QueueStateDTO::make(
|
2024-05-31 05:40:34 +00:00
|
|
|
$this->songRepository->getMany(ids: $state->song_ids, preserveOrder: true, scopedUser: $user),
|
2024-01-01 11:40:21 +00:00
|
|
|
$currentSong,
|
|
|
|
$state->playback_position ?? 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateQueueState(User $user, array $songIds): void
|
|
|
|
{
|
|
|
|
QueueState::query()->updateOrCreate([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
], [
|
|
|
|
'song_ids' => $songIds,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
public function updatePlaybackStatus(User $user, Song $song, int $position): void
|
2024-01-01 11:40:21 +00:00
|
|
|
{
|
|
|
|
QueueState::query()->updateOrCreate([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
], [
|
2024-05-19 05:49:42 +00:00
|
|
|
'current_song_id' => $song->id,
|
2024-01-01 11:40:21 +00:00
|
|
|
'playback_position' => $position,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|