delete child elements
This commit is contained in:
parent
ac0f404fce
commit
f4b42973cb
10 changed files with 271 additions and 17 deletions
|
|
@ -5,6 +5,8 @@ namespace App\Controllers;
|
|||
use App\Element\Element;
|
||||
use App\Element\UseCases\CreateChildElement\CreateChildElement;
|
||||
use App\Element\UseCases\CreateChildElement\CreateChildElementRequest;
|
||||
use App\Element\UseCases\DeleteChildElement\DeleteChildElement;
|
||||
use App\Element\UseCases\DeleteChildElement\DeleteChildElementRequest;
|
||||
use App\Element\UseCases\GetElement\GetElement;
|
||||
use App\Element\UseCases\GetElement\GetElementRequest;
|
||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
||||
|
|
@ -21,6 +23,7 @@ class ElementController
|
|||
public function __construct(
|
||||
private GetElement $getElement,
|
||||
private CreateChildElement $createChildElement,
|
||||
private DeleteChildElement $deleteChildElement,
|
||||
private UpdateElement $updateElement,
|
||||
private FileUploader $fileUploader,
|
||||
) {
|
||||
|
|
@ -82,6 +85,28 @@ class ElementController
|
|||
], 201);
|
||||
}
|
||||
|
||||
public function deleteChild(?int $parentId, ?int $childId): JsonResponse
|
||||
{
|
||||
try {
|
||||
$this->deleteChildElement->execute(
|
||||
new DeleteChildElementRequest(
|
||||
parentElementId: $parentId,
|
||||
childElementId: $childId,
|
||||
)
|
||||
);
|
||||
} catch (BadRequestException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 400);
|
||||
} catch (NotFoundException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 404);
|
||||
}
|
||||
|
||||
return new JsonResponse(null, 204);
|
||||
}
|
||||
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$file = $this->uploadedFileInput($request, 'file');
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ interface ElementRepository
|
|||
|
||||
public function update(Element $element): Element;
|
||||
|
||||
public function delete(Element $element): void;
|
||||
|
||||
public function find(int $id): ?Element;
|
||||
|
||||
public function findRootBySet(DomainSet $set): ?Element;
|
||||
|
|
|
|||
|
|
@ -63,6 +63,18 @@ class EloquentElementRepository implements ElementRepository
|
|||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function delete(Element $element): void
|
||||
{
|
||||
$model = ElementModel::find($element->getId());
|
||||
if ($model === null) {
|
||||
throw new DomainException(
|
||||
"Element with id: {$element->getId()} doesnt exist"
|
||||
);
|
||||
}
|
||||
|
||||
$model->delete();
|
||||
}
|
||||
|
||||
public function find(int $id): ?Element
|
||||
{
|
||||
$model = ElementModel::find($id);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\DeleteChildElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
use App\Shared\Files\FileUploader;
|
||||
|
||||
class DeleteChildElement
|
||||
{
|
||||
public function __construct(
|
||||
private ElementRepository $elementRepository,
|
||||
private FileUploader $fileUploader,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(DeleteChildElementRequest $request): void
|
||||
{
|
||||
if ($request->parentElementId === null) {
|
||||
throw new BadRequestException('parentElementId is required');
|
||||
}
|
||||
|
||||
if ($request->childElementId === null) {
|
||||
throw new BadRequestException('childElementId is required');
|
||||
}
|
||||
|
||||
$parentElement = $this->elementRepository->find(
|
||||
$request->parentElementId
|
||||
);
|
||||
if ($parentElement === null) {
|
||||
throw new NotFoundException('Parent element not found');
|
||||
}
|
||||
|
||||
$childElement = $this->elementRepository->find(
|
||||
$request->childElementId
|
||||
);
|
||||
if ($childElement === null) {
|
||||
throw new NotFoundException('Child element not found');
|
||||
}
|
||||
|
||||
$actualParentElement = $childElement->getParentElement();
|
||||
if (
|
||||
$actualParentElement === null
|
||||
|| $actualParentElement->getId() !== $parentElement->getId()
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'Child element does not belong to parent'
|
||||
);
|
||||
}
|
||||
|
||||
$this->deleteSubtree($childElement);
|
||||
}
|
||||
|
||||
private function deleteSubtree(Element $element): void
|
||||
{
|
||||
$childElements = $this->elementRepository->findByParentElement(
|
||||
$element
|
||||
);
|
||||
foreach ($childElements as $childElement) {
|
||||
$this->deleteSubtree($childElement);
|
||||
}
|
||||
|
||||
$this->deleteManagedFile($element->getIconImageUrl());
|
||||
$this->deleteManagedFile($element->getShortPdfPath());
|
||||
$this->deleteManagedFile($element->getLongPdfPath());
|
||||
$this->elementRepository->delete($element);
|
||||
}
|
||||
|
||||
private function deleteManagedFile(?string $path): void
|
||||
{
|
||||
if ($path === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fileUploader->delete($path);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\DeleteChildElement;
|
||||
|
||||
class DeleteChildElementRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $parentElementId,
|
||||
public ?int $childElementId,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue