messages implemented
This commit is contained in:
@@ -6,26 +6,32 @@ namespace App\Observers;
|
||||
|
||||
use App\Models\ArtworkAward;
|
||||
use App\Services\ArtworkAwardService;
|
||||
use App\Services\UserStatsService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ArtworkAwardObserver
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ArtworkAwardService $service
|
||||
private readonly ArtworkAwardService $service,
|
||||
private readonly UserStatsService $userStats,
|
||||
) {}
|
||||
|
||||
public function created(ArtworkAward $award): void
|
||||
{
|
||||
$this->refresh($award);
|
||||
$this->trackCreatorStats($award, +1);
|
||||
}
|
||||
|
||||
public function updated(ArtworkAward $award): void
|
||||
{
|
||||
$this->refresh($award);
|
||||
// Medal changed – count stays the same; no stat change needed.
|
||||
}
|
||||
|
||||
public function deleted(ArtworkAward $award): void
|
||||
{
|
||||
$this->refresh($award);
|
||||
$this->trackCreatorStats($award, -1);
|
||||
}
|
||||
|
||||
private function refresh(ArtworkAward $award): void
|
||||
@@ -37,4 +43,21 @@ class ArtworkAwardObserver
|
||||
$this->service->syncToSearch($artwork);
|
||||
}
|
||||
}
|
||||
|
||||
private function trackCreatorStats(ArtworkAward $award, int $delta): void
|
||||
{
|
||||
$creatorId = DB::table('artworks')
|
||||
->where('id', $award->artwork_id)
|
||||
->value('user_id');
|
||||
|
||||
if (! $creatorId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($delta > 0) {
|
||||
$this->userStats->incrementAwardsReceived((int) $creatorId);
|
||||
} else {
|
||||
$this->userStats->decrementAwardsReceived((int) $creatorId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user