Backend for songs.zachdecook.com
* Feature: Add to "favorites" playlist
| -rw-r--r-- | laravel/app/Http/Controllers/PlaylistController.php | 12 | ||||
| -rw-r--r-- | laravel/app/Http/Controllers/SongController.php | 7 | ||||
| -rw-r--r-- | laravel/resources/views/song.blade.php | 7 | ||||
| -rw-r--r-- | laravel/routes/web.php | 1 |
4 files changed, 26 insertions, 1 deletions
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 @@ </div> @endforeach + @if ($addToPlaylist ?? null) + <form action="/playlist/{{ $addToPlaylist }}/{{ $song->id }}" method="post"> + @csrf + <button>Add to favorites</button> + </form> + @endif + <script src='/js/chordsdata/chords.js'></script> <script src='/js/chordsdata/ukulelechords.js'></script> <script src='/js/page.js'></script> 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'); |