mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Allow smart playlist creation
This commit is contained in:
parent
e7ad687f8a
commit
d58b791c37
4 changed files with 18 additions and 5 deletions
|
@ -40,8 +40,17 @@ class PlaylistController extends Controller
|
||||||
*/
|
*/
|
||||||
public function store(PlaylistStoreRequest $request)
|
public function store(PlaylistStoreRequest $request)
|
||||||
{
|
{
|
||||||
$playlist = $request->user()->playlists()->create($request->only('name'));
|
/** @var Playlist $playlist */
|
||||||
$playlist->songs()->sync((array) $request->songs);
|
$playlist = $request->user()->playlists()->create([
|
||||||
|
'name' => $request->name,
|
||||||
|
'rules' => $request->rules,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$songs = (array) $request->songs;
|
||||||
|
|
||||||
|
if ($songs) {
|
||||||
|
$playlist->songs()->sync($songs);
|
||||||
|
}
|
||||||
|
|
||||||
$playlist->songs = $playlist->songs->pluck('id');
|
$playlist->songs = $playlist->songs->pluck('id');
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ namespace App\Http\Requests\API;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property string[] $songs
|
* @property string[] $songs
|
||||||
|
* @property string $name
|
||||||
|
* @property array $rules
|
||||||
*/
|
*/
|
||||||
class PlaylistStoreRequest extends Request
|
class PlaylistStoreRequest extends Request
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,6 +27,7 @@ class Playlist extends Model
|
||||||
'user_id' => 'int',
|
'user_id' => 'int',
|
||||||
'rules' => 'array',
|
'rules' => 'array',
|
||||||
];
|
];
|
||||||
|
protected $appends = ['is_smart'];
|
||||||
|
|
||||||
public function songs(): BelongsToMany
|
public function songs(): BelongsToMany
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,9 +24,10 @@ class PlaylistTest extends TestCase
|
||||||
$songs = Song::orderBy('id')->take(3)->get();
|
$songs = Song::orderBy('id')->take(3)->get();
|
||||||
|
|
||||||
$this->postAsUser('api/playlist', [
|
$this->postAsUser('api/playlist', [
|
||||||
'name' => 'Foo Bar',
|
'name' => 'Foo Bar',
|
||||||
'songs' => $songs->pluck('id')->toArray(),
|
'songs' => $songs->pluck('id')->toArray(),
|
||||||
], $user);
|
'rules' => [],
|
||||||
|
], $user);
|
||||||
|
|
||||||
$this->seeInDatabase('playlists', [
|
$this->seeInDatabase('playlists', [
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
|
|
Loading…
Reference in a new issue