middleware('auth:sanctum')->except(['index', 'show']); } /** * Seznam pásem (s stránkováním). */ public function index(Request $request): JsonResponse { $perPage = (int) $request->get('per_page', 100); $bands = Band::query() ->with(['ediBands', 'contests']) ->orderBy('order') ->paginate($perPage); return response()->json($bands); } /** * Vytvoří nové pásmo. */ public function store(Request $request): JsonResponse { $this->authorize('create', Band::class); $data = $this->validateData($request); $relations = $request->validate([ 'edi_band_ids' => ['sometimes', 'array'], 'edi_band_ids.*' => ['integer', 'exists:edi_bands,id'], 'contest_ids' => ['sometimes', 'array'], 'contest_ids.*' => ['integer', 'exists:contests,id'], ]); $band = Band::create($data); if (array_key_exists('edi_band_ids', $relations)) { $band->ediBands()->sync($relations['edi_band_ids']); } if (array_key_exists('contest_ids', $relations)) { $band->contests()->sync($relations['contest_ids']); } return response()->json($band, 201); } /** * Detail jednoho pásma. */ public function show(Band $band): JsonResponse { $band->load(['ediBands', 'contests']); return response()->json($band); } /** * Aktualizace existujícího pásma. */ public function update(Request $request, Band $band): JsonResponse { $this->authorize('update', $band); $data = $this->validateData($request, partial: true); $relations = $request->validate([ 'edi_band_ids' => ['sometimes', 'array'], 'edi_band_ids.*' => ['integer', 'exists:edi_bands,id'], 'contest_ids' => ['sometimes', 'array'], 'contest_ids.*' => ['integer', 'exists:contests,id'], ]); $band->fill($data); $band->save(); if (array_key_exists('edi_band_ids', $relations)) { $band->ediBands()->sync($relations['edi_band_ids']); } if (array_key_exists('contest_ids', $relations)) { $band->contests()->sync($relations['contest_ids']); } return response()->json($band); } /** * Smazání pásma. */ public function destroy(Band $band): JsonResponse { $this->authorize('delete', $band); $band->delete(); return response()->json(null, 204); } /** * Společná validace vstupu pro store/update. */ protected function validateData(Request $request, bool $partial = false): array { $required = $partial ? 'sometimes' : 'required'; return $request->validate([ 'name' => [$required, 'string', 'max:255'], 'order' => [$required, 'integer'], 'edi_band_begin' => [$required, 'integer'], 'edi_band_end' => [$required, 'integer', 'gte:edi_band_begin'], 'has_power_category' => [$required, 'boolean'], ]); } }