favorites
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Exercise;
|
||||
use App\Models\Workout;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class FavoriteController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function toggleExercise(Exercise $exercise): RedirectResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
$exists = $user->favoriteExercises()->where('exercise_id', $exercise->id)->exists();
|
||||
if ($exists) {
|
||||
$user->favoriteExercises()->detach($exercise->id);
|
||||
$msg = 'Removed from favorites';
|
||||
} else {
|
||||
$user->favoriteExercises()->attach($exercise->id);
|
||||
$msg = 'Added to favorites';
|
||||
}
|
||||
return back()->with('status', $msg);
|
||||
}
|
||||
|
||||
public function toggleWorkout(Workout $workout): RedirectResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
if ($workout->user_id && $workout->user_id !== $user->id) {
|
||||
// Users can still favorite others' workouts if shared later; for now no restriction besides existence
|
||||
}
|
||||
$exists = $user->favoriteWorkouts()->where('workout_id', $workout->id)->exists();
|
||||
if ($exists) {
|
||||
$user->favoriteWorkouts()->detach($workout->id);
|
||||
$msg = 'Removed from favorites';
|
||||
} else {
|
||||
$user->favoriteWorkouts()->attach($workout->id);
|
||||
$msg = 'Added to favorites';
|
||||
}
|
||||
return back()->with('status', $msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user