refactor element updates
This commit is contained in:
parent
581852ecbc
commit
d334745ade
19 changed files with 453 additions and 75 deletions
|
|
@ -29,31 +29,61 @@ class Element
|
|||
return $this->title;
|
||||
}
|
||||
|
||||
public function setTitle(string $title): void
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setDescription(string $description): void
|
||||
{
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
public function getIconImageUrl(): ?string
|
||||
{
|
||||
return $this->iconImageUrl;
|
||||
}
|
||||
|
||||
public function setIconImageUrl(?string $iconImageUrl): void
|
||||
{
|
||||
$this->iconImageUrl = $iconImageUrl;
|
||||
}
|
||||
|
||||
public function getRichText(): string
|
||||
{
|
||||
return $this->richText;
|
||||
}
|
||||
|
||||
public function setRichText(string $richText): void
|
||||
{
|
||||
$this->richText = $richText;
|
||||
}
|
||||
|
||||
public function getPdfPath(): ?string
|
||||
{
|
||||
return $this->pdfPath;
|
||||
}
|
||||
|
||||
public function setPdfPath(?string $pdfPath): void
|
||||
{
|
||||
$this->pdfPath = $pdfPath;
|
||||
}
|
||||
|
||||
public function getYoutubeUrl(): ?string
|
||||
{
|
||||
return $this->youtubeUrl;
|
||||
}
|
||||
|
||||
public function setYoutubeUrl(?string $youtubeUrl): void
|
||||
{
|
||||
$this->youtubeUrl = $youtubeUrl;
|
||||
}
|
||||
|
||||
public function getSet(): Set
|
||||
{
|
||||
return $this->set;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ interface ElementRepository
|
|||
{
|
||||
public function create(CreateElementDto $dto): Element;
|
||||
|
||||
public function update(Element $element, UpdateElementDto $dto): Element;
|
||||
public function update(Element $element): Element;
|
||||
|
||||
public function find(int $id): ?Element;
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class EloquentElementRepository implements ElementRepository
|
|||
);
|
||||
}
|
||||
|
||||
public function update(Element $element, UpdateElementDto $dto): Element
|
||||
public function update(Element $element): Element
|
||||
{
|
||||
$model = ElementModel::find($element->getId());
|
||||
if ($model === null) {
|
||||
|
|
@ -47,25 +47,17 @@ class EloquentElementRepository implements ElementRepository
|
|||
);
|
||||
}
|
||||
|
||||
$model->title = $dto->title;
|
||||
$model->description = $dto->description;
|
||||
$model->icon_image_url = $dto->iconImageUrl;
|
||||
$model->rich_text = $dto->richText;
|
||||
$model->pdf_path = $dto->pdfPath;
|
||||
$model->youtube_url = $dto->youtubeUrl;
|
||||
$model->set_id = $element->getSet()->getId();
|
||||
$model->title = $element->getTitle();
|
||||
$model->description = $element->getDescription();
|
||||
$model->icon_image_url = $element->getIconImageUrl();
|
||||
$model->rich_text = $element->getRichText();
|
||||
$model->pdf_path = $element->getPdfPath();
|
||||
$model->youtube_url = $element->getYoutubeUrl();
|
||||
$model->parent_element_id = $element->getParentElement()?->getId();
|
||||
$model->save();
|
||||
|
||||
return new Element(
|
||||
id: $element->getId(),
|
||||
title: $dto->title,
|
||||
description: $dto->description,
|
||||
iconImageUrl: $dto->iconImageUrl,
|
||||
richText: $dto->richText,
|
||||
pdfPath: $dto->pdfPath,
|
||||
youtubeUrl: $dto->youtubeUrl,
|
||||
set: $element->getSet(),
|
||||
parentElement: $element->getParentElement(),
|
||||
);
|
||||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Element
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
class UpdateElementDto
|
||||
{
|
||||
public function __construct(
|
||||
public string $title,
|
||||
public string $description,
|
||||
public ?string $iconImageUrl,
|
||||
public string $richText,
|
||||
public ?string $pdfPath,
|
||||
public ?string $youtubeUrl,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdateDescription
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdateDescriptionRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->description === null) {
|
||||
throw new BadRequestException('description is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$element->setDescription($request->description);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateDescriptionRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $description,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -4,14 +4,20 @@ namespace App\Element\UseCases\UpdateElement;
|
|||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Element\UpdateElementDto;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdateElement
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
public function __construct(
|
||||
private UpdateTitle $updateTitle,
|
||||
private UpdateDescription $updateDescription,
|
||||
private UpdateIconImageUrl $updateIconImageUrl,
|
||||
private UpdateRichText $updateRichText,
|
||||
private UpdatePdfPath $updatePdfPath,
|
||||
private UpdateYoutubeUrl $updateYoutubeUrl,
|
||||
private ElementRepository $elementRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -24,8 +30,53 @@ class UpdateElement
|
|||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->title === null || trim($request->title) === '') {
|
||||
throw new BadRequestException('title is required');
|
||||
$hasNoFields = $request->title === null
|
||||
&& $request->description === null
|
||||
&& $request->iconImageUrl === null
|
||||
&& $request->richText === null
|
||||
&& $request->pdfPath === null
|
||||
&& $request->youtubeUrl === null;
|
||||
if ($hasNoFields) {
|
||||
throw new BadRequestException('nothing to update');
|
||||
}
|
||||
|
||||
if ($request->title !== null) {
|
||||
$this->updateTitle->execute(new UpdateTitleRequest(
|
||||
id: $request->id,
|
||||
title: $request->title,
|
||||
));
|
||||
}
|
||||
if ($request->description !== null) {
|
||||
$this->updateDescription->execute(new UpdateDescriptionRequest(
|
||||
id: $request->id,
|
||||
description: $request->description,
|
||||
));
|
||||
}
|
||||
if ($request->iconImageUrl !== null) {
|
||||
$this->updateIconImageUrl->execute(
|
||||
new UpdateIconImageUrlRequest(
|
||||
id: $request->id,
|
||||
iconImageUrl: $request->iconImageUrl,
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($request->richText !== null) {
|
||||
$this->updateRichText->execute(new UpdateRichTextRequest(
|
||||
id: $request->id,
|
||||
richText: $request->richText,
|
||||
));
|
||||
}
|
||||
if ($request->pdfPath !== null) {
|
||||
$this->updatePdfPath->execute(new UpdatePdfPathRequest(
|
||||
id: $request->id,
|
||||
pdfPath: $request->pdfPath,
|
||||
));
|
||||
}
|
||||
if ($request->youtubeUrl !== null) {
|
||||
$this->updateYoutubeUrl->execute(new UpdateYoutubeUrlRequest(
|
||||
id: $request->id,
|
||||
youtubeUrl: $request->youtubeUrl,
|
||||
));
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
|
|
@ -33,25 +84,6 @@ class UpdateElement
|
|||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
return $this->elementRepository->update(
|
||||
$element,
|
||||
new UpdateElementDto(
|
||||
title: $request->title,
|
||||
description: $request->description ?? '',
|
||||
iconImageUrl: $this->nullableString($request->iconImageUrl),
|
||||
richText: $request->richText ?? '',
|
||||
pdfPath: $this->nullableString($request->pdfPath),
|
||||
youtubeUrl: $this->nullableString($request->youtubeUrl),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private function nullableString(?string $value): ?string
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdateIconImageUrl
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdateIconImageUrlRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->iconImageUrl === null) {
|
||||
throw new BadRequestException('iconImageUrl is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$element->setIconImageUrl($this->nullableString(
|
||||
$request->iconImageUrl,
|
||||
));
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
|
||||
private function nullableString(string $value): ?string
|
||||
{
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateIconImageUrlRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $iconImageUrl,
|
||||
) {
|
||||
}
|
||||
}
|
||||
48
backend/app/Element/UseCases/UpdateElement/UpdatePdfPath.php
Normal file
48
backend/app/Element/UseCases/UpdateElement/UpdatePdfPath.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdatePdfPath
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdatePdfPathRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->pdfPath === null) {
|
||||
throw new BadRequestException('pdfPath is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$element->setPdfPath($this->nullableString($request->pdfPath));
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
|
||||
private function nullableString(string $value): ?string
|
||||
{
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdatePdfPathRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $pdfPath,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdateRichText
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdateRichTextRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->richText === null) {
|
||||
throw new BadRequestException('richText is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$element->setRichText($request->richText);
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateRichTextRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $richText,
|
||||
) {
|
||||
}
|
||||
}
|
||||
39
backend/app/Element/UseCases/UpdateElement/UpdateTitle.php
Normal file
39
backend/app/Element/UseCases/UpdateElement/UpdateTitle.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdateTitle
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdateTitleRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->title === null || trim($request->title) === '') {
|
||||
throw new BadRequestException('title is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$element->setTitle(trim($request->title));
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateTitleRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $title,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
|
||||
class UpdateYoutubeUrl
|
||||
{
|
||||
public function __construct(private ElementRepository $elementRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(UpdateYoutubeUrlRequest $request): Element
|
||||
{
|
||||
if ($request->id === null) {
|
||||
throw new BadRequestException('id is required');
|
||||
}
|
||||
|
||||
if ($request->youtubeUrl === null) {
|
||||
throw new BadRequestException('youtubeUrl is required');
|
||||
}
|
||||
|
||||
$element = $this->elementRepository->find($request->id);
|
||||
if ($element === null) {
|
||||
throw new NotFoundException('Element not found');
|
||||
}
|
||||
|
||||
$element->setYoutubeUrl($this->nullableString(
|
||||
$request->youtubeUrl,
|
||||
));
|
||||
|
||||
return $this->elementRepository->update($element);
|
||||
}
|
||||
|
||||
private function nullableString(string $value): ?string
|
||||
{
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\UpdateElement;
|
||||
|
||||
class UpdateYoutubeUrlRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id,
|
||||
public ?string $youtubeUrl,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue