mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-06-05 22:09:38 +02:00
@@ -3,83 +3,95 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\ApiResource;
|
||||
use F9Web\ApiResponseHelpers;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Maicol07\LaravelJsonApiResource\Http\Resource\JsonApi\Resource;
|
||||
use Maicol07\LaravelJsonApiResource\Http\Resource\JsonApi\ResourceCollection;
|
||||
|
||||
class ApiController extends Controller
|
||||
{
|
||||
use ApiResponseHelpers;
|
||||
|
||||
protected string|Model $model = Model::class;
|
||||
protected string|ApiResource $resource = ApiResource::class;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
public function index(): ResourceCollection
|
||||
{
|
||||
return $this->respondWithSuccess(new $this->resource($this->model::all()));
|
||||
return new ResourceCollection($this->model::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
public function store(Request $request): JsonResponse|Resource
|
||||
{
|
||||
$instance = new $this->model();
|
||||
$instance->fill($request->all());
|
||||
|
||||
$created = $instance->save();
|
||||
|
||||
return $created ? $this->respondCreated(new $this->resource($instance)) : $this->respondError();
|
||||
return $created ? new Resource($instance) : $this->error(Response::HTTP_INTERNAL_SERVER_ERROR, __('Impossibile creare la risorsa'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(int $id): JsonResponse
|
||||
public function show(int $id): Resource|JsonResponse
|
||||
{
|
||||
$instance = $this->model::find($id);
|
||||
|
||||
if (!assert($instance instanceof Model)) {
|
||||
return $this->respondNotFound(__('Risorsa non trovata.'));
|
||||
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
|
||||
}
|
||||
|
||||
return $this->respondWithSuccess(new $this->resource($this->model::find($id)));
|
||||
return new Resource($instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
public function update(Request $request, int $id): Resource|JsonResponse
|
||||
{
|
||||
$instance = $this->model::find($id);
|
||||
if (!assert($instance instanceof Model)) {
|
||||
return $this->respondNotFound(__('Risorsa non trovata.'));
|
||||
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
|
||||
}
|
||||
|
||||
$instance->fill($request->all());
|
||||
$updated = $instance->save();
|
||||
|
||||
return $updated ? $this->respondWithSuccess($instance) : $this->respondError();
|
||||
return $updated ? new Resource($instance) : $this->error(Response::HTTP_INTERNAL_SERVER_ERROR, __('Impossibile salvare le modifiche'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(int $id): JsonResponse
|
||||
public function destroy(int $id): Response|JsonResponse
|
||||
{
|
||||
$instance = $this->model::find($id);
|
||||
|
||||
if (!assert($instance instanceof Model)) {
|
||||
return $this->respondNotFound(__('Risorsa non trovata.'));
|
||||
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
|
||||
}
|
||||
|
||||
$deleted = $instance->delete();
|
||||
|
||||
return $deleted ? $this->respondNoContent() : $this->respondError();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user