2018-08-18 12:27:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-01-10 21:37:24 +00:00
|
|
|
use App\Events\MultipleSongsLiked;
|
|
|
|
use App\Events\MultipleSongsUnliked;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Events\SongLikeToggled;
|
|
|
|
use App\Models\Interaction;
|
2021-06-04 15:19:33 +00:00
|
|
|
use App\Models\Song;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Models\User;
|
2021-06-04 15:19:33 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
class InteractionService
|
|
|
|
{
|
2024-01-10 21:37:24 +00:00
|
|
|
public function increasePlayCount(Song $song, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return tap(Interaction::query()->firstOrCreate([
|
2024-01-10 21:37:24 +00:00
|
|
|
'song_id' => $song->id,
|
2018-08-18 12:27:26 +00:00
|
|
|
'user_id' => $user->id,
|
2018-08-24 15:27:19 +00:00
|
|
|
]), static function (Interaction $interaction): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
if (!$interaction->exists) {
|
|
|
|
$interaction->liked = false;
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:38:28 +00:00
|
|
|
$interaction->last_played_at = now();
|
|
|
|
|
2020-09-06 21:20:42 +00:00
|
|
|
++$interaction->play_count;
|
2018-08-18 12:27:26 +00:00
|
|
|
$interaction->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-29 06:47:10 +00:00
|
|
|
* Like or unlike a song as a user.
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2024-01-10 21:37:24 +00:00
|
|
|
* @return Interaction The affected Interaction object
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2024-01-10 21:37:24 +00:00
|
|
|
public function toggleLike(Song $song, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return tap(Interaction::query()->firstOrCreate([
|
2024-01-10 21:37:24 +00:00
|
|
|
'song_id' => $song->id,
|
2018-08-18 12:27:26 +00:00
|
|
|
'user_id' => $user->id,
|
2018-09-04 06:25:24 +00:00
|
|
|
]), static function (Interaction $interaction): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
$interaction->liked = !$interaction->liked;
|
|
|
|
$interaction->save();
|
|
|
|
|
2018-09-04 06:25:24 +00:00
|
|
|
event(new SongLikeToggled($interaction));
|
2018-08-18 12:27:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like several songs at once as a user.
|
|
|
|
*
|
2024-01-10 21:37:24 +00:00
|
|
|
* @param array<array-key, Song>|Collection $songs
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2021-06-04 16:24:26 +00:00
|
|
|
* @return array<Interaction>|Collection The array of Interaction objects
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2024-01-10 21:37:24 +00:00
|
|
|
public function likeMany(Collection $songs, User $user): Collection
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2024-01-10 21:37:24 +00:00
|
|
|
$interactions = $songs->map(static function (Song $song) use ($user): Interaction {
|
2022-08-09 18:45:11 +00:00
|
|
|
return tap(Interaction::query()->firstOrCreate([
|
2024-01-10 21:37:24 +00:00
|
|
|
'song_id' => $song->id,
|
2022-08-09 18:45:11 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
]), static function (Interaction $interaction): void {
|
|
|
|
$interaction->play_count ??= 0;
|
|
|
|
$interaction->liked = true;
|
|
|
|
$interaction->save();
|
|
|
|
});
|
|
|
|
});
|
2021-06-04 15:19:33 +00:00
|
|
|
|
2024-01-10 21:37:24 +00:00
|
|
|
event(new MultipleSongsLiked($songs, $user));
|
2021-06-04 15:19:33 +00:00
|
|
|
|
|
|
|
return $interactions;
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike several songs at once.
|
|
|
|
*
|
2024-01-10 21:37:24 +00:00
|
|
|
* @param array<array-key, Song>|Collection $songs
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2024-01-10 21:37:24 +00:00
|
|
|
public function unlikeMany(Collection $songs, User $user): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
Interaction::query()
|
2024-01-10 21:37:24 +00:00
|
|
|
->whereIn('song_id', $songs->pluck('id')->all())
|
2018-08-18 12:27:26 +00:00
|
|
|
->where('user_id', $user->id)
|
2021-06-04 15:19:33 +00:00
|
|
|
->update(['liked' => false]);
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2024-01-10 21:37:24 +00:00
|
|
|
event(new MultipleSongsUnliked($songs, $user));
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
}
|