Initial commit
This commit is contained in:
101
app/Http/Controllers/CountryWwlController.php
Normal file
101
app/Http/Controllers/CountryWwlController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\CountryWwl;
|
||||
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 CountryWwlController extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, ValidatesRequests;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Create / Update / Delete pouze pro přihlášené
|
||||
$this->middleware('auth:sanctum')->only(['store', 'update', 'destroy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seznam záznamů country-WWL (stránkovaně).
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = (int) $request->get('per_page', 100);
|
||||
|
||||
$items = CountryWwl::query()
|
||||
->orderBy('country_name')
|
||||
->orderBy('wwl')
|
||||
->paginate($perPage);
|
||||
|
||||
return response()->json($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Vytvoření nového country-WWL záznamu.
|
||||
* Autorizace přes CountryWwlPolicy@create.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorize('create', CountryWwl::class);
|
||||
|
||||
$data = $this->validateData($request);
|
||||
|
||||
$item = CountryWwl::create($data);
|
||||
|
||||
return response()->json($item, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detail jednoho country-WWL záznamu.
|
||||
*/
|
||||
public function show(CountryWwl $country_wwl): JsonResponse
|
||||
{
|
||||
return response()->json($country_wwl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualizace existujícího country-WWL záznamu (partial update).
|
||||
* Autorizace přes CountryWwlPolicy@update.
|
||||
*/
|
||||
public function update(Request $request, CountryWwl $country_wwl): JsonResponse
|
||||
{
|
||||
$this->authorize('update', $country_wwl);
|
||||
|
||||
$data = $this->validateData($request, partial: true);
|
||||
|
||||
$country_wwl->fill($data);
|
||||
$country_wwl->save();
|
||||
|
||||
return response()->json($country_wwl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Smazání country-WWL záznamu.
|
||||
* Autorizace přes CountryWwlPolicy@delete.
|
||||
*/
|
||||
public function destroy(CountryWwl $country_wwl): JsonResponse
|
||||
{
|
||||
$this->authorize('delete', $country_wwl);
|
||||
|
||||
$country_wwl->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validace dat pro store / update.
|
||||
*/
|
||||
protected function validateData(Request $request, bool $partial = false): array
|
||||
{
|
||||
$required = $partial ? 'sometimes' : 'required';
|
||||
|
||||
return $request->validate([
|
||||
'country_name' => [$required, 'string', 'max:150'],
|
||||
'wwl' => [$required, 'string', 'size:4'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user