Initial commit
This commit is contained in:
93
app/Http/Controllers/EdiBandController.php
Normal file
93
app/Http/Controllers/EdiBandController.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\EdiBand;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class EdiBandController extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, ValidatesRequests;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth:sanctum')->except(['index', 'show']);
|
||||
}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = (int) $request->get('per_page', 100);
|
||||
|
||||
$items = EdiBand::query()
|
||||
->with('bands') // eager load pokud chceš mít vazby
|
||||
->orderBy('value')
|
||||
->paginate($perPage);
|
||||
|
||||
return response()->json($items);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorize('create', EdiBand::class);
|
||||
|
||||
$data = $request->validate([
|
||||
'value' => ['required', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$relations = $request->validate([
|
||||
'band_ids' => ['sometimes', 'array'],
|
||||
'band_ids.*' => ['integer', 'exists:bands,id'],
|
||||
]);
|
||||
|
||||
$item = EdiBand::create($data);
|
||||
|
||||
if (array_key_exists('band_ids', $relations)) {
|
||||
$item->bands()->sync($relations['band_ids']);
|
||||
}
|
||||
|
||||
return response()->json($item, 201);
|
||||
}
|
||||
|
||||
public function show(EdiBand $edi_band): JsonResponse
|
||||
{
|
||||
$edi_band->load('bands');
|
||||
|
||||
return response()->json($edi_band);
|
||||
}
|
||||
|
||||
public function update(Request $request, EdiBand $edi_band): JsonResponse
|
||||
{
|
||||
$this->authorize('update', $edi_band);
|
||||
|
||||
$data = $request->validate([
|
||||
'value' => ['sometimes', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$relations = $request->validate([
|
||||
'band_ids' => ['sometimes', 'array'],
|
||||
'band_ids.*' => ['integer', 'exists:bands,id'],
|
||||
]);
|
||||
|
||||
$edi_band->fill($data);
|
||||
$edi_band->save();
|
||||
|
||||
if (array_key_exists('band_ids', $relations)) {
|
||||
$edi_band->bands()->sync($relations['band_ids']);
|
||||
}
|
||||
|
||||
return response()->json($edi_band);
|
||||
}
|
||||
|
||||
public function destroy(EdiBand $edi_band): JsonResponse
|
||||
{
|
||||
$this->authorize('delete', $edi_band);
|
||||
|
||||
$edi_band->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user