add set layout api
This commit is contained in:
parent
eb7ac7d261
commit
4222d4c0a2
16 changed files with 487 additions and 6 deletions
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Exceptions\NotFoundException;
|
||||
use App\Set\Set;
|
||||
use App\Set\UseCases\GetSetLayout\ElementLayoutNode;
|
||||
use App\Set\UseCases\GetSetLayout\GetSetLayout;
|
||||
use App\Set\UseCases\ListSets\ListSets;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
|
|
@ -10,6 +13,7 @@ class SetController extends Controller
|
|||
{
|
||||
public function __construct(
|
||||
private ListSets $listSets,
|
||||
private GetSetLayout $getSetLayout,
|
||||
) {}
|
||||
|
||||
public function index(): JsonResponse
|
||||
|
|
@ -23,4 +27,52 @@ class SetController extends Controller
|
|||
|
||||
return new JsonResponse(['sets' => $sets]);
|
||||
}
|
||||
|
||||
public function show(int $setId): JsonResponse
|
||||
{
|
||||
try {
|
||||
$layout = $this->getSetLayout->execute($setId);
|
||||
} catch (NotFoundException $exception) {
|
||||
return new JsonResponse(
|
||||
['error' => $exception->getMessage()],
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
$set = $layout->getSet();
|
||||
|
||||
return new JsonResponse([
|
||||
'set' => [
|
||||
'id' => $set->getId(),
|
||||
'name' => $set->getName(),
|
||||
],
|
||||
'elements' => array_map(
|
||||
$this->elementPayload(...),
|
||||
$layout->getElements(),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* id: int,
|
||||
* name: string,
|
||||
* kind: string,
|
||||
* children: list<mixed>
|
||||
* }
|
||||
*/
|
||||
private function elementPayload(ElementLayoutNode $node): array
|
||||
{
|
||||
$element = $node->getElement();
|
||||
|
||||
return [
|
||||
'id' => $element->getId(),
|
||||
'name' => $element->getName(),
|
||||
'kind' => $element->getKind(),
|
||||
'children' => array_map(
|
||||
$this->elementPayload(...),
|
||||
$node->getChildren(),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue