add element updates
This commit is contained in:
parent
6d0acaae56
commit
73b0befc97
10 changed files with 223 additions and 13 deletions
|
|
@ -2,16 +2,22 @@
|
|||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\UseCases\GetElement\GetElement;
|
||||
use App\Element\UseCases\GetElement\GetElementRequest;
|
||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
||||
use App\Element\UseCases\UpdateElement\UpdateElementRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ElementController
|
||||
{
|
||||
public function __construct(private GetElement $getElement)
|
||||
{
|
||||
public function __construct(
|
||||
private GetElement $getElement,
|
||||
private UpdateElement $updateElement,
|
||||
) {
|
||||
}
|
||||
|
||||
public function show(?int $id): JsonResponse
|
||||
|
|
@ -42,15 +48,73 @@ class ElementController
|
|||
|
||||
return new JsonResponse([
|
||||
'childElements' => $childElements,
|
||||
'element' => [
|
||||
'id' => $element->getId(),
|
||||
'title' => $element->getTitle(),
|
||||
'description' => $element->getDescription(),
|
||||
'iconImageUrl' => $element->getIconImageUrl(),
|
||||
'richText' => $element->getRichText(),
|
||||
'pdfPath' => $element->getPdfPath(),
|
||||
'youtubeUrl' => $element->getYoutubeUrl(),
|
||||
],
|
||||
'element' => $this->buildElementPayload($element),
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function update(?int $id, Request $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$element = $this->updateElement->execute(
|
||||
new UpdateElementRequest(
|
||||
id: $id,
|
||||
title: $this->stringInput($request, 'title'),
|
||||
description: $this->stringInput($request, 'description'),
|
||||
iconImageUrl: $this->stringInput(
|
||||
$request,
|
||||
'iconImageUrl',
|
||||
),
|
||||
richText: $this->stringInput($request, 'richText'),
|
||||
pdfPath: $this->stringInput($request, 'pdfPath'),
|
||||
youtubeUrl: $this->stringInput($request, 'youtubeUrl'),
|
||||
)
|
||||
);
|
||||
} catch (BadRequestException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 400);
|
||||
} catch (NotFoundException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => $exception->getMessage(),
|
||||
], 404);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'element' => $this->buildElementPayload($element),
|
||||
], 200);
|
||||
}
|
||||
|
||||
private function stringInput(Request $request, string $key): ?string
|
||||
{
|
||||
$value = $request->input($key);
|
||||
if (! is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* id: int,
|
||||
* title: string,
|
||||
* description: string,
|
||||
* iconImageUrl: string|null,
|
||||
* richText: string,
|
||||
* pdfPath: string|null,
|
||||
* youtubeUrl: string|null,
|
||||
* }
|
||||
*/
|
||||
private function buildElementPayload(Element $element): array
|
||||
{
|
||||
return [
|
||||
'id' => $element->getId(),
|
||||
'title' => $element->getTitle(),
|
||||
'description' => $element->getDescription(),
|
||||
'iconImageUrl' => $element->getIconImageUrl(),
|
||||
'richText' => $element->getRichText(),
|
||||
'pdfPath' => $element->getPdfPath(),
|
||||
'youtubeUrl' => $element->getYoutubeUrl(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
16
backend/app/Element/UpdateElementDto.php
Normal file
16
backend/app/Element/UpdateElementDto.php
Normal 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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -12,3 +12,5 @@ Route::get('/me', [AuthController::class, 'me'])
|
|||
->middleware(AuthMiddleware::class);
|
||||
Route::get('/sets', [SetController::class, 'index']);
|
||||
Route::get('/elements/{id}', [ElementController::class, 'show']);
|
||||
Route::patch('/elements/{id}', [ElementController::class, 'update'])
|
||||
->middleware(AuthMiddleware::class);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace Tests\Fakes;
|
|||
use App\Element\CreateElementDto;
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Element\UpdateElementDto;
|
||||
use App\Set\Set as DomainSet;
|
||||
|
||||
class FakeElementRepository implements ElementRepository
|
||||
|
|
@ -33,6 +34,24 @@ class FakeElementRepository implements ElementRepository
|
|||
return $element;
|
||||
}
|
||||
|
||||
public function update(Element $element, UpdateElementDto $dto): Element
|
||||
{
|
||||
$updatedElement = 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(),
|
||||
);
|
||||
$this->elementsById[$element->getId()] = $updatedElement;
|
||||
|
||||
return $this->cloneElement($updatedElement);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Element
|
||||
{
|
||||
if (! isset($this->elementsById[$id])) {
|
||||
|
|
|
|||
|
|
@ -158,7 +158,8 @@ class ElementsEndpointTest extends TestCase
|
|||
));
|
||||
$this->createSession('valid-token');
|
||||
|
||||
$response = $this->withCookie('auth_token', 'valid-token')
|
||||
$response = $this->withCredentials()
|
||||
->withUnencryptedCookie('auth_token', 'valid-token')
|
||||
->patchJson("/api/elements/{$element->getId()}", [
|
||||
'title' => 'Updated title',
|
||||
'description' => 'Updated description',
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use App\Controllers\ElementController;
|
|||
use App\Element\CreateElementDto;
|
||||
use App\Element\Element;
|
||||
use App\Element\UseCases\GetElement\GetElement;
|
||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
||||
use App\Set\Set as DomainSet;
|
||||
use Tests\Fakes\FakeElementRepository;
|
||||
use Tests\TestCase;
|
||||
|
|
@ -20,7 +21,8 @@ class ElementControllerTest extends TestCase
|
|||
{
|
||||
$this->elementRepo = new FakeElementRepository();
|
||||
$getElement = new GetElement($this->elementRepo);
|
||||
$this->controller = new ElementController($getElement);
|
||||
$updateElement = new UpdateElement($this->elementRepo);
|
||||
$this->controller = new ElementController($getElement, $updateElement);
|
||||
}
|
||||
|
||||
public function testShowReturnsElementPayload(): void
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue