2026-05-29 00:33:00 +03:00
|
|
|
<?php
|
|
|
|
|
namespace App\Models;
|
2026-05-30 11:23:24 +03:00
|
|
|
use App\Services\ActivityService;
|
2026-05-29 00:33:00 +03:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
|
|
|
|
|
class UserGame extends Model
|
|
|
|
|
{
|
|
|
|
|
protected $fillable = ['user_id','game_id','status','rating','start_date','finish_date','hours_played','notes','is_private'];
|
|
|
|
|
protected $casts = ['start_date'=>'date','finish_date'=>'date','is_private'=>'boolean'];
|
|
|
|
|
|
|
|
|
|
const STATUSES = ['playing','played','want_to_play','on_hold','dropped'];
|
|
|
|
|
|
2026-05-30 11:23:24 +03:00
|
|
|
protected static function boot(): void
|
|
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
|
|
static::created(function (UserGame $ug) {
|
|
|
|
|
ActivityService::log($ug->user, 'added_to_library', $ug->game, ['status' => $ug->status]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
static::updated(function (UserGame $ug) {
|
|
|
|
|
if ($ug->isDirty('rating') && $ug->rating) {
|
|
|
|
|
ActivityService::log($ug->user, 'rated_game', $ug->game, ['rating' => $ug->rating]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 00:33:00 +03:00
|
|
|
public function user(): BelongsTo { return $this->belongsTo(User::class); }
|
|
|
|
|
public function game(): BelongsTo { return $this->belongsTo(Game::class); }
|
|
|
|
|
}
|