Backend for songs.zachdecook.com
* Suggestions: show suggestions and track them
Zach DeCook 2019-01-12
parent fa336a5 · commit b4958b4
-rw-r--r--laravel/app/Http/Controllers/SongController.php15
-rw-r--r--laravel/app/Suggestion.php11
-rw-r--r--laravel/database/migrations/2019_01_12_153012_create_suggested_table.php37
-rw-r--r--laravel/public/css/song.css4
-rw-r--r--laravel/resources/views/song.blade.php6
-rw-r--r--laravel/routes/web.php1
6 files changed, 72 insertions, 2 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php
index 60b3e67..087c358 100644
--- a/laravel/app/Http/Controllers/SongController.php
+++ b/laravel/app/Http/Controllers/SongController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Playlist;
use App\Song;
+use App\Suggestion;
use Illuminate\Http\Request;
class SongController extends Controller
@@ -55,8 +56,22 @@ class SongController extends Controller
$params['back'] = Song::where('id', '<', $song->id)->orderBy('id', 'desc')->first();
$params['next'] = Song::where('id', '>', $song->id)->orderBy('id', 'asc' )->first();
}
+ $params['suggestions'] = Song::inRandomOrder()->limit(5)->get();
+ foreach ($params['suggestions'] as $sugSong){
+ $sug = Suggestion::firstOrNew(['from' => $song->id, 'song' => $sugSong->id]);
+ $sug->shown++;
+ $sug->save();
+ }
return view('song', $params );
}
+ public function suggested($song, $from)
+ {
+ $sug = Suggestion::firstOrNew(['song' => $song, 'from' => $from]);
+ $sug->clicks++;
+ $sug->save();
+ //Suggestion::make(['song' => $song, 'from' => $from]);
+ return redirect(route('song.show', $song));
+ }
/**
* @brief Determine whether or not this line contains chords.
diff --git a/laravel/app/Suggestion.php b/laravel/app/Suggestion.php
new file mode 100644
index 0000000..5d081ff
--- /dev/null
+++ b/laravel/app/Suggestion.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Suggestion extends Model
+{
+ public $fillable = ['song', 'from'];
+ //
+}
diff --git a/laravel/database/migrations/2019_01_12_153012_create_suggested_table.php b/laravel/database/migrations/2019_01_12_153012_create_suggested_table.php
new file mode 100644
index 0000000..1f907ec
--- /dev/null
+++ b/laravel/database/migrations/2019_01_12_153012_create_suggested_table.php
@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateSuggestedTable extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('suggestions', function (Blueprint $table) {
+ $table->increments('id');
+ $table->unsignedInteger('from');
+ $table->unsignedInteger('song');
+ $table->unsignedInteger('clicks')->default(0);
+ $table->unsignedInteger('shown')->default(0);
+ $table->timestamps();
+ $table->foreign('from')->references('id')->on('songs')->onDelete('cascade');
+ $table->foreign('song')->references('id')->on('songs');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('suggestions');
+ }
+}
diff --git a/laravel/public/css/song.css b/laravel/public/css/song.css
index 8465822..1e61f44 100644
--- a/laravel/public/css/song.css
+++ b/laravel/public/css/song.css
@@ -2,14 +2,14 @@ div.controlArea{
display: flex;
justify-content: space-between;
}
-a.back, a.home, a.next{
+a.back, a.home, a.next, a.but{
border: 2px solid var(--gray);
color: var(--dark);
padding: 3px;
margin: 3px;
text-decoration: none;
}
-a.back:hover, a.home:hover, a.next:hover{
+a.back:hover, a.home:hover, a.next:hover, a.but:hover{
color: var(--light);
background-color: var(--secondary);
}
diff --git a/laravel/resources/views/song.blade.php b/laravel/resources/views/song.blade.php
index 4812bc3..6b4b661 100644
--- a/laravel/resources/views/song.blade.php
+++ b/laravel/resources/views/song.blade.php
@@ -62,6 +62,12 @@
<canvas id='ukuleleregion' width="100" height="100" onclick="cycle(ukeObj);"></canvas>
</div>
+ @foreach ($suggestions as $sug)
+ <div class='controlArea'>
+ <a class='but' href='{{route('song.suggested', ['song' => $sug, 'from' => $song])}}'>{{$sug->name}}</a>
+ </div>
+ @endforeach
+
<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 f978467..5e5561e 100644
--- a/laravel/routes/web.php
+++ b/laravel/routes/web.php
@@ -17,6 +17,7 @@ Route::get('/', function () {
Route::post('/new/song', 'SongController@post')->name('song.postnew')/*->middleware('can:create,App\Song')*/;
Route::get('/playlist/{playlist}', 'PlaylistController@show')->name('playlist.show');
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');
Route::get('/s/{song}/edit', 'SongController@edit')->name('song.edit')->middleware('can:update,song');
Route::get('/song/{song}', 'SongController@oldShow')->name('song.oldShow');