2018-08-18 12:27:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Events\SongLikeToggled;
|
2021-06-04 15:19:33 +00:00
|
|
|
use App\Events\SongsBatchLiked;
|
|
|
|
use App\Events\SongsBatchUnliked;
|
2018-08-18 12:27:26 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Increase the number of times a song is played by a user.
|
|
|
|
*
|
|
|
|
* @return Interaction The affected Interaction object
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function increasePlayCount(string $songId, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2021-06-04 15:19:33 +00:00
|
|
|
return tap(Interaction::firstOrCreate([
|
2018-08-18 12:27:26 +00:00
|
|
|
'song_id' => $songId,
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2020-09-06 21:20:42 +00:00
|
|
|
* @return Interaction the affected Interaction object
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function toggleLike(string $songId, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2021-06-04 15:19:33 +00:00
|
|
|
return tap(Interaction::firstOrCreate([
|
2018-08-18 12:27:26 +00:00
|
|
|
'song_id' => $songId,
|
|
|
|
'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.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string> $songIds
|
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
|
|
|
*/
|
2021-06-04 15:19:33 +00:00
|
|
|
public function batchLike(array $songIds, User $user): Collection
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
$interactions = collect($songIds)->map(static fn ($songId): Interaction => tap(Interaction::firstOrCreate([
|
|
|
|
'song_id' => $songId,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]), static function (Interaction $interaction): void {
|
|
|
|
if (!$interaction->exists) {
|
|
|
|
$interaction->play_count = 0;
|
|
|
|
}
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
$interaction->liked = true;
|
|
|
|
$interaction->save();
|
|
|
|
}));
|
2021-06-04 15:19:33 +00:00
|
|
|
|
|
|
|
event(new SongsBatchLiked($interactions->map(static function (Interaction $interaction): Song {
|
|
|
|
return $interaction->song;
|
|
|
|
}), $user));
|
|
|
|
|
|
|
|
return $interactions;
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike several songs at once.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string> $songIds
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function batchUnlike(array $songIds, User $user): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2021-06-04 15:19:33 +00:00
|
|
|
Interaction::whereIn('song_id', $songIds)
|
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
|
|
|
|
2021-06-04 15:19:33 +00:00
|
|
|
event(new SongsBatchUnliked(Song::find($songIds), $user));
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
}
|