chore: 🔥 Rimossa vecchia API

This commit is contained in:
Maicol Battistini 2022-02-14 18:23:16 +01:00
parent 21fc998d8a
commit 68f949922a
No known key found for this signature in database
GPG Key ID: 4FDB0F87CDB1D34A
2 changed files with 0 additions and 131 deletions

View File

@ -1,120 +0,0 @@
<?php /** @noinspection PhpUndefinedMethodInspection */
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Maicol07\LaravelJsonApiResource\Http\Resource\JsonApi\Resource;
use Maicol07\LaravelJsonApiResource\Http\Resource\JsonApi\ResourceCollection;
class ApiController extends Controller
{
/**
* The parent model used by the controller.
*/
protected string|Model $model = Model::class;
/**
* @var array{string: Model | array}
*/
protected array $relationships = [];
/**
* Display a listing of the resource.
*/
public function index(Request $request): ResourceCollection
{
return (new ResourceCollection($this->model::all()))
->include(explode(',', $request->input('include')));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): JsonResponse|Resource
{
$instance = new $this->model();
$instance->fill($request->input('data.attributes'));
foreach ($request->input('data.relationships') as $key => $data) {
$model = $this->relationships[$key];
$relation = $instance->{$key}();
if ($relation instanceof HasOne) {
$relation->save($model::find(Arr::get($data, 'data.id')));
} elseif ($relation instanceof HasMany) {
$relation->saveMany($model::findMany(Arr::get($data, 'data.id')));
}
}
$created = $instance->save();
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, Request $request): Resource|JsonResponse
{
$instance = $this->model::find($id);
if (!assert($instance instanceof Model)) {
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
}
return (new Resource($instance))
->include(explode(',', $request->input('include')));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, int $id): Resource|JsonResponse
{
$instance = $this->model::find($id);
if (!assert($instance instanceof Model)) {
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
}
$instance->fill($request->input('data.attributes'));
$updated = $instance->save();
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): Response|JsonResponse
{
$instance = $this->model::find($id);
if (!assert($instance instanceof Model)) {
return $this->error(Response::HTTP_NOT_FOUND, __('Risorsa non trovata.'));
}
$deleted = $instance->delete();
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);
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace App\Http\Controllers\Api;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
class UserController extends ApiController
{
protected string|Model $model = User::class;
}