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)
{
$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');

View file

@ -4,6 +4,8 @@ namespace App\Http\Requests\API;
/**
* @property string[] $songs
* @property string $name
* @property array $rules
*/
class PlaylistStoreRequest extends Request
{

View file

@ -27,6 +27,7 @@ class Playlist extends Model
'user_id' => 'int',
'rules' => 'array',
];
protected $appends = ['is_smart'];
public function songs(): BelongsToMany
{

View file

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