Files
SkinbaseNova/app/Services/NovaCards/NovaCardChallengeService.php
2026-03-28 19:15:39 +01:00

36 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\NovaCards;
use App\Models\NovaCard;
use App\Models\NovaCardChallenge;
use App\Models\NovaCardChallengeEntry;
use App\Models\User;
class NovaCardChallengeService
{
public function submit(User $user, NovaCardChallenge $challenge, NovaCard $card, ?string $note = null): NovaCardChallengeEntry
{
$entry = NovaCardChallengeEntry::query()->updateOrCreate([
'challenge_id' => $challenge->id,
'card_id' => $card->id,
], [
'user_id' => $user->id,
'status' => NovaCardChallengeEntry::STATUS_ACTIVE,
'note' => $note,
]);
$challenge->forceFill([
'entries_count' => NovaCardChallengeEntry::query()->where('challenge_id', $challenge->id)->count(),
])->save();
$card->forceFill([
'challenge_entries_count' => NovaCardChallengeEntry::query()->where('card_id', $card->id)->count(),
'last_engaged_at' => now(),
])->save();
return $entry;
}
}