Sync 5 Fix front loading after bo Overhaul

This commit is contained in:
Jérémy Munsch 2023-11-24 10:03:42 +01:00
parent b79b7dc151
commit 93caefb196
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
4 changed files with 46 additions and 16 deletions

View file

@ -3,6 +3,8 @@
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\IsCachedAsCollection;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
@ -28,6 +30,8 @@ use Illuminate\Database\Eloquent\Model;
*/
class Prize extends Model
{
use IsCachedAsCollection;
/**
* The attributes that are mass assignable.
*
@ -55,13 +59,18 @@ class Prize extends Model
static::creating(function (self $model) {
self::manageFields($model);
});
static::created(function () {
self::removeCached();
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
self::removeCached();
FileStorageHelper::removeOldFile($model, 'pdf_winning_proof');
});
static::deleted(function (self $model) {
self::removeCached();
FileStorageHelper::removeFile($model, 'pdf_winning_proof');
});
}
@ -98,4 +107,17 @@ class Prize extends Model
{
return $this->hasMany(InstantWin::class, 'prize_id');
}
// * CACHE METHOD
/**
* Change query for cached elements
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected static function queryCached(Builder $query): Builder
{
return $query->with('instantWins');
}
}

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\Traits\Models\IsCachedAsCollection;
use Illuminate\Database\Eloquent\Model;
/**
@ -33,6 +34,8 @@ use Illuminate\Database\Eloquent\Model;
*/
class Settings extends Model
{
use IsCachedAsCollection;
/**
* The attributes that are mass assignable.
*
@ -72,4 +75,22 @@ class Settings extends Model
'teaser_end_activity' => 'datetime',
'close_page_active_date' => 'datetime',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::created(function () {
self::removeCached();
});
static::updated(function () {
self::removeCached();
});
static::deleted(function () {
self::removeCached();
});
}
}

View file

@ -44,24 +44,11 @@ class ViewServiceProvider extends ServiceProvider
Paginator::useBootstrapFive();
}
// FIXME: USe trait cache here
// On ramène le nom du jeu et le nom de la société sur toute les pages.
$settingsGlobal = Cache::get('settingsGlobal');
if (!$settingsGlobal) {
$settingsGlobal = app(Settings::class)
->select('name', 'company', 'analytics', 'start_date', 'end_date')
->first();
Cache::put('settingsGlobal', $settingsGlobal);
}
View::share('settingsGlobal', $settingsGlobal);
View::share('settingsGlobal', Settings::getCached()->first());
// On ramène aussi tout les prix car on en a besoin sur le header.
$prizesGlobal = Cache::get('prizesGlobal');
if (!$prizesGlobal) {
$prizesGlobal = Prize::all();
Cache::put('prizesGlobal', $prizesGlobal);
}
View::share('prizesGlobal', $prizesGlobal);
View::share('prizesGlobal', Prize::getCached());
// * CUSTOM IF STATEMENTS

View file

@ -18,7 +18,7 @@ trait IsCachedAsCollection
* Get all available instances from cache.
* If cache was reset, it will fetch them from DB.
*
* @return \Illuminate\Database\Eloquent\Collection
* @return \Illuminate\Database\Eloquent\Collection<self>
*/
public static function getCached(): Collection
{