From 247c96763c65f83fd6c28e591a6e8fe3e62780a1 Mon Sep 17 00:00:00 2001 From: Zach DeCook Date: Mon, 21 Sep 2020 21:15:18 -0400 Subject: * Feature: Add to "favorites" playlist --- laravel/app/Http/Controllers/PlaylistController.php | 12 ++++++++++++ laravel/app/Http/Controllers/SongController.php | 7 ++++++- laravel/resources/views/song.blade.php | 7 +++++++ laravel/routes/web.php | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) (limited to 'laravel') diff --git a/laravel/app/Http/Controllers/PlaylistController.php b/laravel/app/Http/Controllers/PlaylistController.php index 49b1f35..c055601 100644 --- a/laravel/app/Http/Controllers/PlaylistController.php +++ b/laravel/app/Http/Controllers/PlaylistController.php @@ -15,4 +15,16 @@ class PlaylistController extends Controller } abort(404); } + public function post($playlistName, $songID) + { + $pl = Playlist::where('name',$playlistName)->first(); + if (!$pl){ + $pl = new Playlist(); + $pl->name = $playlistName; + } + $pl->save(); + $pl->songs()->attach($songID); + // Do people want to see the favorites playlist when they've added to it, or stay on the song? + return redirect()->route('playlist.show', [ 'playlist' => $playlistName]); + } } diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php index 8e043ac..83c9601 100644 --- a/laravel/app/Http/Controllers/SongController.php +++ b/laravel/app/Http/Controllers/SongController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Playlist; use App\Song; use App\Suggestion; +use App\User; use Illuminate\Http\Request; class SongController extends Controller @@ -14,7 +15,7 @@ class SongController extends Controller $song = Song::where('number', $song )->first(); return redirect()->route('song.show', ['song' => $song ] ); } - public function show( Song $song, Request $request ) + public function show( Song $song, Request $request, User $user ) { $lines = explode( "\n", $song['text'] ); $newText = ''; @@ -67,6 +68,10 @@ class SongController extends Controller $params['suggestions'] = $playlist->songs()->inRandomOrder()->limit(5)->get(); } else { $params['suggestions'] = Song::inRandomOrder()->limit(5)->get(); + $plName = ($user->name ?? 'anon') . 'favs'; + if(! $song->playlists()->where('name',$plName)->exists()){ + $params['addToPlaylist'] = ($user->name ?? 'anon') . 'favs'; + } } foreach ($params['suggestions'] as $sugSong){ $sug = Suggestion::firstOrNew(['from' => $song->id, 'song' => $sugSong->id]); diff --git a/laravel/resources/views/song.blade.php b/laravel/resources/views/song.blade.php index 025c14e..6871a91 100644 --- a/laravel/resources/views/song.blade.php +++ b/laravel/resources/views/song.blade.php @@ -87,6 +87,13 @@ @endforeach + @if ($addToPlaylist ?? null) +
+ @csrf + +
+ @endif + diff --git a/laravel/routes/web.php b/laravel/routes/web.php index 5e5561e..c783313 100644 --- a/laravel/routes/web.php +++ b/laravel/routes/web.php @@ -16,6 +16,7 @@ Route::get('/', function () { })->name('song.list'); Route::post('/new/song', 'SongController@post')->name('song.postnew')/*->middleware('can:create,App\Song')*/; Route::get('/playlist/{playlist}', 'PlaylistController@show')->name('playlist.show'); +Route::post('/playlist/{playlist}/{song}', 'PlaylistController@post')->name('playlist.post'); Route::get('/s/{song}', 'SongController@show')->name('song.show'); Route::get('/s/{song}/suggested/{from}', 'SongController@suggested')->name('song.suggested'); Route::post('/s/{song}', 'SongController@update')->name('song.update')->middleware('can:update,song'); -- cgit 1.4.1