Skip to content

Commit

Permalink
Allow smart playlist creation
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Nov 25, 2018
1 parent e7ad687 commit d58b791
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
13 changes: 11 additions & 2 deletions app/Http/Controllers/API/PlaylistController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ public function index()
*/
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');

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Requests/API/PlaylistStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* @property string[] $songs
* @property string $name
* @property array $rules
*/
class PlaylistStoreRequest extends Request
{
Expand Down
1 change: 1 addition & 0 deletions app/Models/Playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Playlist extends Model
'user_id' => 'int',
'rules' => 'array',
];
protected $appends = ['is_smart'];

public function songs(): BelongsToMany
{
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/PlaylistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public function testCreatingPlaylist()
$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,
Expand Down

0 comments on commit d58b791

Please sign in to comment.