-
app/Services/InteractionService.php
Open in GitHubuse App\Events\SongLikeToggled; use App\Models\Interaction; class InteractionService { public function increasePlayCount(string $songId, User $user): Interaction { return tap(Interaction::firstOrCreate([ 'song_id' => $songId, 'user_id' => $user->id, ]), static function (Interaction $interaction): void { if (!$interaction->exists) { $interaction->liked = false; } ++$interaction->play_count; $interaction->save(); }); } public function toggleLike(string $songId, User $user): Interaction { return tap(Interaction::firstOrCreate([ 'song_id' => $songId, 'user_id' => $user->id, ]), static function (Interaction $interaction): void { $interaction->liked = !$interaction->liked; $interaction->save(); event(new SongLikeToggled($interaction)); }); } // }