From d58b791c37e120e3d9f5320a2507ee756a9ac249 Mon Sep 17 00:00:00 2001 From: Phan An Date: Sun, 25 Nov 2018 22:21:46 +0100 Subject: [PATCH] Allow smart playlist creation --- app/Http/Controllers/API/PlaylistController.php | 13 +++++++++++-- app/Http/Requests/API/PlaylistStoreRequest.php | 2 ++ app/Models/Playlist.php | 1 + tests/Feature/PlaylistTest.php | 7 ++++--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/API/PlaylistController.php b/app/Http/Controllers/API/PlaylistController.php index a577edaf..f673baf6 100644 --- a/app/Http/Controllers/API/PlaylistController.php +++ b/app/Http/Controllers/API/PlaylistController.php @@ -40,8 +40,17 @@ class PlaylistController extends Controller */ public function store(PlaylistStoreRequest $request) { - $playlist = $request->user()->playlists()->create($request->only('name')); - $playlist->songs()->sync((array) $request->songs); + /** @var Playlist $playlist */ + $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'); diff --git a/app/Http/Requests/API/PlaylistStoreRequest.php b/app/Http/Requests/API/PlaylistStoreRequest.php index 2d0dd651..84be9719 100644 --- a/app/Http/Requests/API/PlaylistStoreRequest.php +++ b/app/Http/Requests/API/PlaylistStoreRequest.php @@ -4,6 +4,8 @@ namespace App\Http\Requests\API; /** * @property string[] $songs + * @property string $name + * @property array $rules */ class PlaylistStoreRequest extends Request { diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index 178be5bc..3b35fa07 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -27,6 +27,7 @@ class Playlist extends Model 'user_id' => 'int', 'rules' => 'array', ]; + protected $appends = ['is_smart']; public function songs(): BelongsToMany { diff --git a/tests/Feature/PlaylistTest.php b/tests/Feature/PlaylistTest.php index fd01bda2..d2919876 100644 --- a/tests/Feature/PlaylistTest.php +++ b/tests/Feature/PlaylistTest.php @@ -24,9 +24,10 @@ class PlaylistTest extends TestCase $songs = Song::orderBy('id')->take(3)->get(); $this->postAsUser('api/playlist', [ - 'name' => 'Foo Bar', - 'songs' => $songs->pluck('id')->toArray(), - ], $user); + 'name' => 'Foo Bar', + 'songs' => $songs->pluck('id')->toArray(), + 'rules' => [], + ], $user); $this->seeInDatabase('playlists', [ 'user_id' => $user->id,