Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

koel/koel

16858 stars
1 code files
View koel/koel on GitHub

app/Services/InteractionService.php

Open in GitHub
use 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));
});
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.