41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Exercise;
|
||
|
|
use App\Models\ExerciseAlias;
|
||
|
|
use Illuminate\Http\RedirectResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
|
||
|
|
class ExerciseAliasController extends Controller
|
||
|
|
{
|
||
|
|
public function store(Request $request, Exercise $exercise): RedirectResponse
|
||
|
|
{
|
||
|
|
// Auth is enforced at the route level. Extra guard below for safety.
|
||
|
|
if (!Auth::check()) {
|
||
|
|
abort(403);
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = $request->validate([
|
||
|
|
'name' => ['nullable', 'string', 'max:255'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$name = trim((string) ($data['name'] ?? ''));
|
||
|
|
$userId = Auth::id();
|
||
|
|
|
||
|
|
if ($name === '') {
|
||
|
|
// Clear alias
|
||
|
|
ExerciseAlias::where('user_id', $userId)->where('exercise_id', $exercise->id)->delete();
|
||
|
|
return back()->with('status', __('app.exercises.rename_cleared'));
|
||
|
|
}
|
||
|
|
|
||
|
|
ExerciseAlias::updateOrCreate(
|
||
|
|
['user_id' => $userId, 'exercise_id' => $exercise->id],
|
||
|
|
['name' => $name]
|
||
|
|
);
|
||
|
|
|
||
|
|
return back()->with('status', __('app.exercises.renamed_success'));
|
||
|
|
}
|
||
|
|
}
|