Allow smart playlist creation

This commit is contained in:
Phan An 2018-11-25 22:21:46 +01:00
parent e7ad687f8a
commit d58b791c37
4 changed files with 18 additions and 5 deletions

View file

@ -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');

View file

@ -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
{ {

View file

@ -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
{ {

View file

@ -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,