Add resources defined in config and a bundle of the site's own screens
This commit is contained in:
1 parent
37a29d9195
commit
3345797141
35 files changed
+905
-19
No files matched your search
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Resources;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use IriksIT\Manager\Support\AuditTrail;
|
||||
use IriksIT\Manager\Support\Fields;
|
||||
use IriksIT\Manager\Support\Resources;
|
||||
|
||||
class ResourceController
|
||||
{
|
||||
public function index(string $resource): JsonResponse
|
||||
{
|
||||
$def = $this->resource($resource, 'view');
|
||||
$page = Resources::query($def)->paginate($def['per_page']);
|
||||
|
||||
return response()->json([
|
||||
'resource' => $this->describe($def),
|
||||
'rows' => $page->getCollection()->map(fn (Model $model) => Resources::row($def, $model))->values(),
|
||||
'current_page' => $page->currentPage(),
|
||||
'last_page' => $page->lastPage(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(string $resource): JsonResponse
|
||||
{
|
||||
$def = $this->resource($resource, 'manage');
|
||||
|
||||
return response()->json(['resource' => $this->describe($def), 'record' => ['id' => null, 'values' => Fields::values($def['fields'], [])]]);
|
||||
}
|
||||
|
||||
public function edit(string $resource, string $id): JsonResponse
|
||||
{
|
||||
$def = $this->resource($resource, 'manage');
|
||||
|
||||
return response()->json(['resource' => $this->describe($def), 'record' => Resources::row($def, $this->record($def, $id))]);
|
||||
}
|
||||
|
||||
public function store(Request $request, string $resource): JsonResponse
|
||||
{
|
||||
$def = $this->resource($resource, 'manage');
|
||||
$model = Resources::save($def, null, $this->values($request, $def));
|
||||
app(AuditTrail::class)->about($model);
|
||||
|
||||
return response()->json(['record' => Resources::row($def, $model)], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $resource, string $id): JsonResponse
|
||||
{
|
||||
$def = $this->resource($resource, 'manage');
|
||||
$model = Resources::save($def, $this->record($def, $id), $this->values($request, $def));
|
||||
app(AuditTrail::class)->about($model);
|
||||
|
||||
return response()->json(['record' => Resources::row($def, $model)]);
|
||||
}
|
||||
|
||||
public function destroy(string $resource, string $id): JsonResponse
|
||||
{
|
||||
$def = $this->resource($resource, 'manage');
|
||||
$model = $this->record($def, $id);
|
||||
app(AuditTrail::class)->about($model);
|
||||
$model->delete();
|
||||
|
||||
return response()->json(['deleted' => true]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function resource(string $key, string $ability): array
|
||||
{
|
||||
$def = Resources::find($key);
|
||||
abort_if($def === null, 404);
|
||||
Gate::authorize($key.'.'.$ability);
|
||||
|
||||
return $def;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $def */
|
||||
private function record(array $def, string $id): Model
|
||||
{
|
||||
return Resources::record($def, $id) ?? abort(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $def
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function values(Request $request, array $def): array
|
||||
{
|
||||
$data = $request->validate(Fields::rules($def['fields']));
|
||||
|
||||
return Fields::clean($def['fields'], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $def
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function describe(array $def): array
|
||||
{
|
||||
return [
|
||||
'key' => $def['key'], 'label' => $def['label'], 'singular' => $def['singular'], 'columns' => $def['columns'],
|
||||
'fields' => array_values($def['fields']), 'can_manage' => Gate::allows($def['key'].'.manage'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user