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

@ -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;
}
}