45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\NovaCards;
|
|
|
|
use App\Jobs\UpdateNovaCardStatsJob;
|
|
use App\Models\NovaCard;
|
|
use App\Models\NovaCardReaction;
|
|
use App\Models\User;
|
|
|
|
class NovaCardReactionService
|
|
{
|
|
public function setReaction(User $user, NovaCard $card, string $type, bool $active): array
|
|
{
|
|
$existing = NovaCardReaction::query()
|
|
->where('user_id', $user->id)
|
|
->where('card_id', $card->id)
|
|
->where('type', $type)
|
|
->first();
|
|
|
|
if ($active && ! $existing) {
|
|
NovaCardReaction::query()->create([
|
|
'user_id' => $user->id,
|
|
'card_id' => $card->id,
|
|
'type' => $type,
|
|
]);
|
|
}
|
|
|
|
if (! $active && $existing) {
|
|
$existing->delete();
|
|
}
|
|
|
|
UpdateNovaCardStatsJob::dispatch($card->id);
|
|
|
|
$card->refresh();
|
|
|
|
return [
|
|
'liked' => NovaCardReaction::query()->where('user_id', $user->id)->where('card_id', $card->id)->where('type', NovaCardReaction::TYPE_LIKE)->exists(),
|
|
'favorited' => NovaCardReaction::query()->where('user_id', $user->id)->where('card_id', $card->id)->where('type', NovaCardReaction::TYPE_FAVORITE)->exists(),
|
|
'likes_count' => (int) $card->fresh()->likes_count,
|
|
'favorites_count' => (int) $card->fresh()->favorites_count,
|
|
];
|
|
}
|
|
} |