route set updates
This commit is contained in:
parent
b2e39a98e4
commit
7428612316
7 changed files with 252 additions and 89 deletions
42
backend/app/Set/UseCases/UpdateSet/UpdateDescription.php
Normal file
42
backend/app/Set/UseCases/UpdateSet/UpdateDescription.php
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\UpdateSet;
|
||||||
|
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
|
use App\Exceptions\NotFoundException;
|
||||||
|
use App\Set\Set;
|
||||||
|
use App\Set\SetRepository;
|
||||||
|
|
||||||
|
class UpdateDescription
|
||||||
|
{
|
||||||
|
public function __construct(private SetRepository $setRepository)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
* @throws NotFoundException
|
||||||
|
*/
|
||||||
|
public function execute(UpdateDescriptionRequest $request): Set
|
||||||
|
{
|
||||||
|
if ($request->id === null) {
|
||||||
|
throw new BadRequestException('setId is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$request->description === null
|
||||||
|
|| trim($request->description) === ''
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('description is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
$set = $this->setRepository->find($request->id);
|
||||||
|
if ($set === null) {
|
||||||
|
throw new NotFoundException('Set not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$set->setDescription($request->description);
|
||||||
|
|
||||||
|
return $this->setRepository->update($set);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\UpdateSet;
|
||||||
|
|
||||||
|
class UpdateDescriptionRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?int $id,
|
||||||
|
public ?string $description,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
93
backend/app/Set/UseCases/UpdateSet/UpdateIconImage.php
Normal file
93
backend/app/Set/UseCases/UpdateSet/UpdateIconImage.php
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\UpdateSet;
|
||||||
|
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
|
use App\Exceptions\NotFoundException;
|
||||||
|
use App\Set\Set;
|
||||||
|
use App\Set\SetRepository;
|
||||||
|
use App\Shared\Files\FileToUpload;
|
||||||
|
use App\Shared\Files\Filesystem;
|
||||||
|
|
||||||
|
class UpdateIconImage
|
||||||
|
{
|
||||||
|
private const ALLOWED_MIME_TYPES = [
|
||||||
|
'image/jpeg',
|
||||||
|
'image/png',
|
||||||
|
'image/webp',
|
||||||
|
];
|
||||||
|
|
||||||
|
private const MAX_SIZE_BYTES = 5 * 1024 * 1024;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private SetRepository $setRepository,
|
||||||
|
private Filesystem $filesystem,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
* @throws NotFoundException
|
||||||
|
*/
|
||||||
|
public function execute(UpdateIconImageRequest $request): Set
|
||||||
|
{
|
||||||
|
if ($request->id === null) {
|
||||||
|
throw new BadRequestException('setId is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$request->fileContents === null
|
||||||
|
|| $request->fileOriginalName === null
|
||||||
|
|| $request->fileMimeType === null
|
||||||
|
|| $request->fileSizeBytes === null
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('icon image is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
! in_array(
|
||||||
|
$request->fileMimeType,
|
||||||
|
self::ALLOWED_MIME_TYPES,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'icon image must be a jpeg, png or webp image',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->fileSizeBytes > self::MAX_SIZE_BYTES) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'icon image must be 5MB or smaller',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$set = $this->setRepository->find($request->id);
|
||||||
|
if ($set === null) {
|
||||||
|
throw new NotFoundException('Set not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$oldIconImageUrl = $set->getIconImageUrl();
|
||||||
|
$iconImage = new FileToUpload(
|
||||||
|
contents: $request->fileContents,
|
||||||
|
originalName: $request->fileOriginalName,
|
||||||
|
mimeType: $request->fileMimeType,
|
||||||
|
sizeBytes: $request->fileSizeBytes,
|
||||||
|
);
|
||||||
|
$path = $this->filesystem->upload($iconImage, 'set-icons');
|
||||||
|
$set->setIconImageUrl($path);
|
||||||
|
$updatedSet = $this->setRepository->update($set);
|
||||||
|
$this->deleteManagedSetIcon($oldIconImageUrl);
|
||||||
|
|
||||||
|
return $updatedSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function deleteManagedSetIcon(string $path): void
|
||||||
|
{
|
||||||
|
if (! str_starts_with($path, 'set-icons/')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->filesystem->delete($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\UpdateSet;
|
||||||
|
|
||||||
|
class UpdateIconImageRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?int $id,
|
||||||
|
public ?string $fileContents,
|
||||||
|
public ?string $fileOriginalName,
|
||||||
|
public ?string $fileMimeType,
|
||||||
|
public ?int $fileSizeBytes,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
39
backend/app/Set/UseCases/UpdateSet/UpdateName.php
Normal file
39
backend/app/Set/UseCases/UpdateSet/UpdateName.php
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\UpdateSet;
|
||||||
|
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
|
use App\Exceptions\NotFoundException;
|
||||||
|
use App\Set\Set;
|
||||||
|
use App\Set\SetRepository;
|
||||||
|
|
||||||
|
class UpdateName
|
||||||
|
{
|
||||||
|
public function __construct(private SetRepository $setRepository)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
* @throws NotFoundException
|
||||||
|
*/
|
||||||
|
public function execute(UpdateNameRequest $request): Set
|
||||||
|
{
|
||||||
|
if ($request->id === null) {
|
||||||
|
throw new BadRequestException('setId is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->name === null || trim($request->name) === '') {
|
||||||
|
throw new BadRequestException('name is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
$set = $this->setRepository->find($request->id);
|
||||||
|
if ($set === null) {
|
||||||
|
throw new NotFoundException('Set not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$set->setName(trim($request->name));
|
||||||
|
|
||||||
|
return $this->setRepository->update($set);
|
||||||
|
}
|
||||||
|
}
|
||||||
12
backend/app/Set/UseCases/UpdateSet/UpdateNameRequest.php
Normal file
12
backend/app/Set/UseCases/UpdateSet/UpdateNameRequest.php
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\UpdateSet;
|
||||||
|
|
||||||
|
class UpdateNameRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?int $id,
|
||||||
|
public ?string $name,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,22 +6,14 @@ use App\Exceptions\BadRequestException;
|
||||||
use App\Exceptions\NotFoundException;
|
use App\Exceptions\NotFoundException;
|
||||||
use App\Set\Set;
|
use App\Set\Set;
|
||||||
use App\Set\SetRepository;
|
use App\Set\SetRepository;
|
||||||
use App\Shared\Files\FileToUpload;
|
|
||||||
use App\Shared\Files\Filesystem;
|
|
||||||
|
|
||||||
class UpdateSet
|
class UpdateSet
|
||||||
{
|
{
|
||||||
private const ALLOWED_MIME_TYPES = [
|
|
||||||
'image/jpeg',
|
|
||||||
'image/png',
|
|
||||||
'image/webp',
|
|
||||||
];
|
|
||||||
|
|
||||||
private const MAX_SIZE_BYTES = 5 * 1024 * 1024;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private UpdateName $updateName,
|
||||||
|
private UpdateDescription $updateDescription,
|
||||||
|
private UpdateIconImage $updateIconImage,
|
||||||
private SetRepository $setRepository,
|
private SetRepository $setRepository,
|
||||||
private Filesystem $filesystem,
|
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,14 +26,31 @@ class UpdateSet
|
||||||
if ($request->id === null) {
|
if ($request->id === null) {
|
||||||
throw new BadRequestException('setId is required');
|
throw new BadRequestException('setId is required');
|
||||||
}
|
}
|
||||||
if ($request->name === null || $request->name === '') {
|
|
||||||
throw new BadRequestException('name is required');
|
if ($this->hasNoFields($request)) {
|
||||||
|
throw new BadRequestException('nothing to update');
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
$request->description === null
|
if ($request->name !== null) {
|
||||||
|| $request->description === ''
|
$this->updateName->execute(new UpdateNameRequest(
|
||||||
) {
|
id: $request->id,
|
||||||
throw new BadRequestException('description is required');
|
name: $request->name,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if ($request->description !== null) {
|
||||||
|
$this->updateDescription->execute(new UpdateDescriptionRequest(
|
||||||
|
id: $request->id,
|
||||||
|
description: $request->description,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if ($this->hasIconImageInput($request)) {
|
||||||
|
$this->updateIconImage->execute(new UpdateIconImageRequest(
|
||||||
|
id: $request->id,
|
||||||
|
fileContents: $request->iconImageContents,
|
||||||
|
fileOriginalName: $request->iconImageOriginalName,
|
||||||
|
fileMimeType: $request->iconImageMimeType,
|
||||||
|
fileSizeBytes: $request->iconImageSizeBytes,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$set = $this->setRepository->find($request->id);
|
$set = $this->setRepository->find($request->id);
|
||||||
|
|
@ -49,80 +58,21 @@ class UpdateSet
|
||||||
throw new NotFoundException('Set not found');
|
throw new NotFoundException('Set not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldIconImageUrl = $set->getIconImageUrl();
|
return $set;
|
||||||
$newIconImageUrl = $this->iconImageUrl($request, $oldIconImageUrl);
|
|
||||||
|
|
||||||
$set->setName($request->name);
|
|
||||||
$set->setDescription($request->description);
|
|
||||||
$set->setIconImageUrl($newIconImageUrl);
|
|
||||||
|
|
||||||
$updatedSet = $this->setRepository->update($set);
|
|
||||||
if ($newIconImageUrl !== $oldIconImageUrl) {
|
|
||||||
$this->deleteManagedSetIcon($oldIconImageUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $updatedSet;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function hasNoFields(UpdateSetRequest $request): bool
|
||||||
* @throws BadRequestException
|
|
||||||
*/
|
|
||||||
private function iconImageUrl(
|
|
||||||
UpdateSetRequest $request,
|
|
||||||
string $currentIconImageUrl,
|
|
||||||
): string {
|
|
||||||
if (
|
|
||||||
$request->iconImageContents === null
|
|
||||||
&& $request->iconImageOriginalName === null
|
|
||||||
&& $request->iconImageMimeType === null
|
|
||||||
&& $request->iconImageSizeBytes === null
|
|
||||||
) {
|
|
||||||
return $currentIconImageUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
$request->iconImageContents === null
|
|
||||||
|| $request->iconImageOriginalName === null
|
|
||||||
|| $request->iconImageMimeType === null
|
|
||||||
|| $request->iconImageSizeBytes === null
|
|
||||||
) {
|
|
||||||
throw new BadRequestException('icon image is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
! in_array(
|
|
||||||
$request->iconImageMimeType,
|
|
||||||
self::ALLOWED_MIME_TYPES,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
throw new BadRequestException(
|
|
||||||
'icon image must be a jpeg, png or webp image',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($request->iconImageSizeBytes > self::MAX_SIZE_BYTES) {
|
|
||||||
throw new BadRequestException(
|
|
||||||
'icon image must be 5MB or smaller',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$iconImage = new FileToUpload(
|
|
||||||
contents: $request->iconImageContents,
|
|
||||||
originalName: $request->iconImageOriginalName,
|
|
||||||
mimeType: $request->iconImageMimeType,
|
|
||||||
sizeBytes: $request->iconImageSizeBytes,
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->filesystem->upload($iconImage, 'set-icons');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function deleteManagedSetIcon(string $path): void
|
|
||||||
{
|
{
|
||||||
if (! str_starts_with($path, 'set-icons/')) {
|
return $request->name === null
|
||||||
return;
|
&& $request->description === null
|
||||||
}
|
&& ! $this->hasIconImageInput($request);
|
||||||
|
}
|
||||||
|
|
||||||
$this->filesystem->delete($path);
|
private function hasIconImageInput(UpdateSetRequest $request): bool
|
||||||
|
{
|
||||||
|
return $request->iconImageContents !== null
|
||||||
|
|| $request->iconImageOriginalName !== null
|
||||||
|
|| $request->iconImageMimeType !== null
|
||||||
|
|| $request->iconImageSizeBytes !== null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue