2021-10-08 16:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
class PlaylistService
|
|
|
|
{
|
2021-10-25 19:17:25 +00:00
|
|
|
public function createPlaylist(string $name, User $user, array $songs, ?array $ruleGroups = null): Playlist
|
2021-10-08 16:23:45 +00:00
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = $user->playlists()->create([
|
|
|
|
'name' => $name,
|
|
|
|
'rules' => $ruleGroups,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!$playlist->is_smart && $songs) {
|
|
|
|
$playlist->songs()->sync($songs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $playlist;
|
|
|
|
}
|
2022-07-04 10:39:02 +00:00
|
|
|
|
|
|
|
public function addSongsToPlaylist(Playlist $playlist, array $songIds): void
|
|
|
|
{
|
|
|
|
$playlist->songs()->syncWithoutDetaching($songIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeSongsFromPlaylist(Playlist $playlist, array $songIds): void
|
|
|
|
{
|
|
|
|
$playlist->songs()->detach($songIds);
|
|
|
|
}
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|