2021-10-08 16:56:09 +02:00
|
|
|
<?php /** @noinspection PhpUndefinedMethodInspection */
|
2021-10-04 10:30:51 +02:00
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2021-10-06 19:15:12 +02:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Maicol07\LaravelJsonApiResource\Http\Resource\JsonApi\Resource;
|
|
|
|
use Maicol07\LaravelJsonApiResource\Http\Resource\JsonApi\ResourceCollection;
|
2021-10-04 10:30:51 +02:00
|
|
|
|
|
|
|
class ApiController extends Controller
|
|
|
|
{
|
|
|
|
protected string|Model $model = Model::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*/
|
2021-10-06 19:15:12 +02:00
|
|
|
public function index(): ResourceCollection
|
2021-10-04 10:30:51 +02:00
|
|
|
{
|
2021-10-06 19:15:12 +02:00
|
|
|
return new ResourceCollection($this->model::all());
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*/
|
2021-10-06 19:15:12 +02:00
|
|
|
public function store(Request $request): JsonResponse|Resource
|
2021-10-04 10:30:51 +02:00
|
|
|
{
|
|
|
|
$instance = new $this->model();
|
2021-10-08 16:56:09 +02:00
|
|
|
$instance->fill($request->input('data.attributes'));
|
2021-10-04 10:30:51 +02:00
|
|
|
|
|
|
|
$created = $instance->save();
|
|
|
|
|
2021-10-06 19:15:12 +02:00
|
|
|
return $created ? new Resource($instance) : $this->error(Response::HTTP_INTERNAL_SERVER_ERROR, __('Impossibile creare la risorsa'));
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*/
|
2021-10-06 19:15:12 +02:00
|
|
|
public function show(int $id): Resource|JsonResponse
|
2021-10-04 10:30:51 +02:00
|
|
|
{
|
|
|
|
$instance = $this->model::find($id);
|
|
|
|
|
|
|
|
if (!assert($instance instanceof Model)) {
|
2021-10-06 19:15:12 +02:00
|
|
|
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-06 19:15:12 +02:00
|
|
|
return new Resource($instance);
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*/
|
2021-10-06 19:15:12 +02:00
|
|
|
public function update(Request $request, int $id): Resource|JsonResponse
|
2021-10-04 10:30:51 +02:00
|
|
|
{
|
|
|
|
$instance = $this->model::find($id);
|
|
|
|
if (!assert($instance instanceof Model)) {
|
2021-10-06 19:15:12 +02:00
|
|
|
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 16:56:09 +02:00
|
|
|
$instance->fill($request->input('data.attributes'));
|
2021-10-04 10:30:51 +02:00
|
|
|
$updated = $instance->save();
|
|
|
|
|
2021-10-06 19:15:12 +02:00
|
|
|
return $updated ? new Resource($instance) : $this->error(Response::HTTP_INTERNAL_SERVER_ERROR, __('Impossibile salvare le modifiche'));
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*/
|
2021-10-06 19:15:12 +02:00
|
|
|
public function destroy(int $id): Response|JsonResponse
|
2021-10-04 10:30:51 +02:00
|
|
|
{
|
|
|
|
$instance = $this->model::find($id);
|
|
|
|
|
|
|
|
if (!assert($instance instanceof Model)) {
|
2021-10-06 19:15:12 +02:00
|
|
|
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$deleted = $instance->delete();
|
|
|
|
|
2021-10-06 19:15:12 +02:00
|
|
|
return $deleted ? response()->noContent() : $this->error(Response::HTTP_INTERNAL_SERVER_ERROR, __('Impossibile eliminare la risorsa'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @noinspection PhpSameParameterValueInspection */
|
|
|
|
private function error(int $status, string $title, ?string $detail = null): JsonResponse
|
|
|
|
{
|
|
|
|
return response()->json([
|
|
|
|
'errors' => [
|
|
|
|
[
|
|
|
|
'status' => $status,
|
|
|
|
'title' => $title,
|
|
|
|
'detail' => $detail,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
2021-10-04 10:30:51 +02:00
|
|
|
}
|
|
|
|
}
|