chore: add User dependency to SmartPlaylistService

This commit is contained in:
Phan An 2022-08-01 13:03:58 +02:00
parent b6daeb5d77
commit 444e91ab57
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
3 changed files with 10 additions and 5 deletions

View file

@ -5,14 +5,18 @@ namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Http\Requests\API\PlaylistSongUpdateRequest;
use App\Models\Playlist;
use App\Models\User;
use App\Services\PlaylistService;
use App\Services\SmartPlaylistService;
use Illuminate\Contracts\Auth\Authenticatable;
class PlaylistSongController extends Controller
{
/** @param User $user */
public function __construct(
private SmartPlaylistService $smartPlaylistService,
private PlaylistService $playlistService
private PlaylistService $playlistService,
private Authenticatable $user
) {
}
@ -22,7 +26,7 @@ class PlaylistSongController extends Controller
return response()->json(
$playlist->is_smart
? $this->smartPlaylistService->getSongs($playlist)->pluck('id')
? $this->smartPlaylistService->getSongs($playlist, $this->user)->pluck('id')
: $playlist->songs->pluck('id')
);
}

View file

@ -31,7 +31,7 @@ class PlaylistSongController extends Controller
return SongResource::collection(
$playlist->is_smart
? $this->smartPlaylistService->getSongs($playlist)
? $this->smartPlaylistService->getSongs($playlist, $this->user)
: $this->songRepository->getByStandardPlaylist($playlist, $this->user)
);
}

View file

@ -5,6 +5,7 @@ namespace App\Services;
use App\Exceptions\NonSmartPlaylistException;
use App\Models\Playlist;
use App\Models\Song;
use App\Models\User;
use App\Values\SmartPlaylistRule;
use App\Values\SmartPlaylistRuleGroup;
use Illuminate\Contracts\Auth\Guard;
@ -18,11 +19,11 @@ class SmartPlaylistService
}
/** @return Collection|array<array-key, Song> */
public function getSongs(Playlist $playlist): Collection
public function getSongs(Playlist $playlist, ?User $user = null): Collection
{
throw_unless($playlist->is_smart, NonSmartPlaylistException::create($playlist));
$query = Song::withMeta($this->auth->user());
$query = Song::withMeta($user ?? $this->auth->user());
$playlist->rule_groups->each(static function (SmartPlaylistRuleGroup $group, int $index) use ($query): void {
$clause = $index === 0 ? 'where' : 'orWhere';