public static toggleLike ( string $songId, |
||
$songId | string | |
$user | ||
return |
public static function toggleLike($songId, User $user)
{
$interaction = self::firstOrCreate(['song_id' => $songId, 'user_id' => $user->id]);
if (!$interaction->exists) {
$interaction->play_count = 0;
}
$interaction->liked = !$interaction->liked;
$interaction->save();
event(new SongLikeToggled($interaction));
return $interaction;
}
/** * Like or unlike a song as the currently authenticated user. * * @param Request $request * * @return \Illuminate\Http\JsonResponse */ public function like(Request $request) { return response()->json(Interaction::toggleLike($request->input('song'), $request->user())); }