add element updates

This commit is contained in:
Yisroel Baum 2026-05-28 20:08:36 +03:00
parent 6d0acaae56
commit 73b0befc97
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
10 changed files with 223 additions and 13 deletions

View file

@ -8,6 +8,8 @@ interface ElementRepository
{
public function create(CreateElementDto $dto): Element;
public function update(Element $element, UpdateElementDto $dto): Element;
public function find(int $id): ?Element;
public function findRootBySet(DomainSet $set): ?Element;

View file

@ -38,6 +38,36 @@ class EloquentElementRepository implements ElementRepository
);
}
public function update(Element $element, UpdateElementDto $dto): Element
{
$model = ElementModel::find($element->getId());
if ($model === null) {
throw new DomainException(
"Element with id: {$element->getId()} doesnt exist"
);
}
$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->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(),
);
}
public function find(int $id): ?Element
{
$model = ElementModel::find($id);

View file

@ -0,0 +1,16 @@
<?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,
) {
}
}

View file

@ -0,0 +1,57 @@
<?php
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)
{
}
/**
* @throws BadRequestException
* @throws NotFoundException
*/
public function execute(UpdateElementRequest $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');
}
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;
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace App\Element\UseCases\UpdateElement;
class UpdateElementRequest
{
public function __construct(
public ?int $id,
public ?string $title,
public ?string $description,
public ?string $iconImageUrl,
public ?string $richText,
public ?string $pdfPath,
public ?string $youtubeUrl,
) {
}
}