#15: Number of hits to albums/photos are now recorded in the database

This commit is contained in:
Andy Heathershaw 2017-04-16 10:04:47 +01:00
parent d2d6c5a465
commit def4a28b10
5 changed files with 117 additions and 0 deletions

View File

@ -57,6 +57,11 @@ class AlbumController extends Controller
->get();
}
DB::transaction(function () use ($album) {
$album->hits++;
$album->save();
});
return Theme::render(sprintf('gallery.album_%s', $requestedView), [
'album' => $album,
'allowed_views' => $validViews,

View File

@ -13,6 +13,7 @@ use App\Http\Middleware\VerifyCsrfToken;
use App\Photo;
use Guzzle\Http\Mimetypes;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpFoundation\Request;
@ -60,6 +61,11 @@ class PhotoController extends Controller
$photoStream = $album->getAlbumSource()->fetchPhotoContent($photo, $thumbnail);
$mimeType = Mimetypes::getInstance()->fromFilename($photo->storage_file_name);
DB::transaction(function () use ($photo) {
$photo->hits_download++;
$photo->save();
});
return response()->stream(
function() use ($photoStream)
{
@ -95,6 +101,11 @@ class PhotoController extends Controller
$returnAlbumUrl = $referer;
}
DB::transaction(function () use ($photo) {
$photo->hits++;
$photo->save();
});
return Theme::render('gallery.photo', [
'album' => $album,
'is_original_allowed' => $isOriginalAllowed,

10
app/VisitorHit.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class VisitorHit extends Model
{
//
}

View File

@ -0,0 +1,49 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVisitorHitsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('visitor_hits', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('album_id')->nullable();
$table->unsignedBigInteger('photo_id')->nullable();
$table->unsignedInteger('user_id')->nullable();
$table->string('album_view')->nullable();
$table->timestamp('hit_at');
$table->string('ip_address');
$table->string('user_agent');
$table->foreign('album_id')
->references('id')->on('albums')
->onDelete('no action');
$table->foreign('photo_id')
->references('id')->on('photos')
->onDelete('no action');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('no action');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('visitor_hits');
}
}

View File

@ -0,0 +1,42 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AttachHitColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('albums', function (Blueprint $table) {
$table->bigInteger('hits');
});
Schema::table('photos', function (Blueprint $table) {
$table->bigInteger('hits');
$table->bigInteger('hits_download');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('photos', function (Blueprint $table) {
$table->bigInteger('hits_download');
$table->dropColumn('hits');
});
Schema::table('albums', function (Blueprint $table) {
$table->dropColumn('hits');
});
}
}