add child creation
This commit is contained in:
parent
7c84eefe78
commit
34b0fd0b44
7 changed files with 332 additions and 2 deletions
|
|
@ -3,6 +3,8 @@
|
|||
namespace App\Controllers;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\UseCases\CreateChildElement\CreateChildElement;
|
||||
use App\Element\UseCases\CreateChildElement\CreateChildElementRequest;
|
||||
use App\Element\UseCases\GetElement\GetElement;
|
||||
use App\Element\UseCases\GetElement\GetElementRequest;
|
||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
||||
|
|
@ -18,6 +20,7 @@ class ElementController
|
|||
{
|
||||
public function __construct(
|
||||
private GetElement $getElement,
|
||||
private CreateChildElement $createChildElement,
|
||||
private UpdateElement $updateElement,
|
||||
private FileUploader $fileUploader,
|
||||
) {
|
||||
|
|
@ -55,6 +58,30 @@ class ElementController
|
|||
], 200);
|
||||
}
|
||||
|
||||
public function createChild(Request $request, ?int $parentId): JsonResponse
|
||||
{
|
||||
try {
|
||||
$element = $this->createChildElement->execute(
|
||||
new CreateChildElementRequest(
|
||||
parentElementId: $parentId,
|
||||
title: $this->stringInput($request, 'title'),
|
||||
)
|
||||
);
|
||||
} catch (BadRequestException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 400);
|
||||
} catch (NotFoundException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 404);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'element' => $this->buildElementPayload($element),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$file = $this->uploadedFileInput($request, 'file');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\CreateChildElement;
|
||||
|
||||
use App\Element\CreateElementDto;
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class CreateChildElement
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(CreateChildElementRequest $request): Element
|
||||
{
|
||||
if ($request->parentElementId === null) {
|
||||
throw new BadRequestException('parentElementId is required');
|
||||
}
|
||||
|
||||
if ($request->title === null || $request->title === '') {
|
||||
throw new BadRequestException('title is required');
|
||||
}
|
||||
|
||||
$parentElement = $this->elementRepository->find(
|
||||
$request->parentElementId
|
||||
);
|
||||
if ($parentElement === null) {
|
||||
throw new NotFoundException('Parent element not found');
|
||||
}
|
||||
|
||||
return $this->elementRepository->create(new CreateElementDto(
|
||||
set: $parentElement->getSet(),
|
||||
title: $request->title,
|
||||
description: '',
|
||||
iconImageUrl: null,
|
||||
richText: '',
|
||||
shortPdfPath: null,
|
||||
longPdfPath: null,
|
||||
youtubeUrl: null,
|
||||
parentElement: $parentElement,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\CreateChildElement;
|
||||
|
||||
class CreateChildElementRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $parentElementId,
|
||||
public ?string $title,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue