You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.8 KiB
93 lines
2.8 KiB
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Api\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\Admin\StoreCompetitionTrackRequest;
|
||
|
|
use App\Http\Requests\Admin\UpdateCompetitionTrackRequest;
|
||
|
|
use App\Models\Competition;
|
||
|
|
use App\Models\CompetitionTrack;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Response;
|
||
|
|
|
||
|
|
class CompetitionTrackController extends Controller
|
||
|
|
{
|
||
|
|
public function index(Competition $competition): JsonResponse
|
||
|
|
{
|
||
|
|
$tracks = $competition->tracks()->orderBy('sort')->orderBy('id')->get()->map(fn (CompetitionTrack $t) => $this->toArray($t));
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'data' => $tracks,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(StoreCompetitionTrackRequest $request, Competition $competition): JsonResponse
|
||
|
|
{
|
||
|
|
/** @var array<string, mixed> $validated */
|
||
|
|
$validated = $request->validated();
|
||
|
|
|
||
|
|
/** @var CompetitionTrack $track */
|
||
|
|
$track = $competition->tracks()->create($validated);
|
||
|
|
|
||
|
|
return response()->json($this->toArray($track), 201);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(Competition $competition, CompetitionTrack $track): JsonResponse
|
||
|
|
{
|
||
|
|
return response()->json($this->ensureTrackBelongsAndReturn($competition, $track));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(UpdateCompetitionTrackRequest $request, Competition $competition, CompetitionTrack $track): JsonResponse
|
||
|
|
{
|
||
|
|
$this->ensureTrackBelongs($competition, $track);
|
||
|
|
|
||
|
|
$track->fill($request->validated());
|
||
|
|
$track->save();
|
||
|
|
|
||
|
|
return response()->json($this->toArray($track));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(Competition $competition, CompetitionTrack $track): Response
|
||
|
|
{
|
||
|
|
$this->ensureTrackBelongs($competition, $track);
|
||
|
|
$track->delete();
|
||
|
|
|
||
|
|
return response()->noContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
private function toArray(CompetitionTrack $track): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => $track->id,
|
||
|
|
'competition_id' => $track->competition_id,
|
||
|
|
'track_code' => $track->track_code,
|
||
|
|
'title' => $track->title,
|
||
|
|
'description' => $track->description,
|
||
|
|
'sort' => $track->sort,
|
||
|
|
'is_enabled' => $track->is_enabled,
|
||
|
|
'created_at' => $track->created_at?->toIso8601String(),
|
||
|
|
'updated_at' => $track->updated_at?->toIso8601String(),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
private function ensureTrackBelongsAndReturn(Competition $competition, CompetitionTrack $track): array
|
||
|
|
{
|
||
|
|
$this->ensureTrackBelongs($competition, $track);
|
||
|
|
|
||
|
|
return $this->toArray($track);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function ensureTrackBelongs(Competition $competition, CompetitionTrack $track): void
|
||
|
|
{
|
||
|
|
if ($track->competition_id !== $competition->id) {
|
||
|
|
abort(404);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|