2025-10-02 23:52:07 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Exercise;
|
|
|
|
|
use App\Models\Workout;
|
2026-08-10 12:04:50 +03:00
|
|
|
use App\Models\YogaPose;
|
2025-10-02 23:52:07 +03:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
|
|
class FavoriteController extends Controller
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2026-08-10 12:04:50 +03:00
|
|
|
|
|
|
|
|
public function toggleYogaPose(YogaPose $pose): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
$exists = $user->favoriteYogaPoses()->where('yoga_pose_id', $pose->id)->exists();
|
|
|
|
|
if ($exists) {
|
|
|
|
|
$user->favoriteYogaPoses()->detach($pose->id);
|
|
|
|
|
$msg = 'Removed from favorites';
|
|
|
|
|
} else {
|
|
|
|
|
$user->favoriteYogaPoses()->attach($pose->id);
|
|
|
|
|
$msg = 'Added to favorites';
|
|
|
|
|
}
|
|
|
|
|
return back()->with('status', $msg);
|
|
|
|
|
}
|
2025-10-02 23:52:07 +03:00
|
|
|
}
|