2018-08-18 12:27:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Events\SongLikeToggled;
|
|
|
|
use App\Models\Interaction;
|
2018-10-20 21:46:12 +00:00
|
|
|
use App\Models\Song;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
class InteractionService
|
|
|
|
{
|
|
|
|
private $interaction;
|
|
|
|
|
|
|
|
public function __construct(Interaction $interaction)
|
|
|
|
{
|
|
|
|
$this->interaction = $interaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
return tap($this->interaction->firstOrCreate([
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$interaction->play_count++;
|
|
|
|
$interaction->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like or unlike a song on behalf of a user.
|
|
|
|
*
|
|
|
|
* @return Interaction The affected Interaction object.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function toggleLike(string $songId, User $user): Interaction
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
|
|
|
return tap($this->interaction->firstOrCreate([
|
|
|
|
'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.
|
|
|
|
*
|
2018-08-24 15:27:19 +00:00
|
|
|
* @param string[] $songIds
|
2018-08-18 12:27:26 +00:00
|
|
|
*
|
2018-08-24 15:27:19 +00:00
|
|
|
* @return Interaction[] The array of Interaction objects.
|
2018-08-18 12:27:26 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function batchLike(array $songIds, User $user): array
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2018-08-24 15:27:19 +00:00
|
|
|
return collect($songIds)->map(function ($songId) use ($user): Interaction {
|
2018-08-18 12:27:26 +00:00
|
|
|
return tap($this->interaction->firstOrCreate([
|
|
|
|
'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
|
|
|
if (!$interaction->exists) {
|
|
|
|
$interaction->play_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$interaction->liked = true;
|
|
|
|
$interaction->save();
|
|
|
|
|
2018-09-04 06:25:24 +00:00
|
|
|
event(new SongLikeToggled($interaction));
|
2018-08-18 12:27:26 +00:00
|
|
|
});
|
|
|
|
})->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike several songs at once.
|
|
|
|
*
|
2018-08-24 15:27:19 +00:00
|
|
|
* @param 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
|
|
|
{
|
|
|
|
$this->interaction
|
|
|
|
->whereIn('song_id', $songIds)
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
->get()
|
2018-09-04 06:25:24 +00:00
|
|
|
->each(static function (Interaction $interaction): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
$interaction->liked = false;
|
|
|
|
$interaction->save();
|
|
|
|
|
2018-09-04 06:25:24 +00:00
|
|
|
event(new SongLikeToggled($interaction));
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|