add element updates
This commit is contained in:
parent
6d0acaae56
commit
73b0befc97
10 changed files with 223 additions and 13 deletions
57
backend/app/Element/UseCases/UpdateElement/UpdateElement.php
Normal file
57
backend/app/Element/UseCases/UpdateElement/UpdateElement.php
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue