diff --git a/backend/app/Set/UseCases/UpdateSet/UpdateDescription.php b/backend/app/Set/UseCases/UpdateSet/UpdateDescription.php new file mode 100644 index 0000000..d4ef68d --- /dev/null +++ b/backend/app/Set/UseCases/UpdateSet/UpdateDescription.php @@ -0,0 +1,42 @@ +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); + } +} diff --git a/backend/app/Set/UseCases/UpdateSet/UpdateDescriptionRequest.php b/backend/app/Set/UseCases/UpdateSet/UpdateDescriptionRequest.php new file mode 100644 index 0000000..1f975f0 --- /dev/null +++ b/backend/app/Set/UseCases/UpdateSet/UpdateDescriptionRequest.php @@ -0,0 +1,12 @@ +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); + } +} diff --git a/backend/app/Set/UseCases/UpdateSet/UpdateIconImageRequest.php b/backend/app/Set/UseCases/UpdateSet/UpdateIconImageRequest.php new file mode 100644 index 0000000..33f7df3 --- /dev/null +++ b/backend/app/Set/UseCases/UpdateSet/UpdateIconImageRequest.php @@ -0,0 +1,15 @@ +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); + } +} diff --git a/backend/app/Set/UseCases/UpdateSet/UpdateNameRequest.php b/backend/app/Set/UseCases/UpdateSet/UpdateNameRequest.php new file mode 100644 index 0000000..aa68a18 --- /dev/null +++ b/backend/app/Set/UseCases/UpdateSet/UpdateNameRequest.php @@ -0,0 +1,12 @@ +id === null) { 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 - || $request->description === '' - ) { - throw new BadRequestException('description is required'); + + if ($request->name !== null) { + $this->updateName->execute(new UpdateNameRequest( + id: $request->id, + 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); @@ -49,80 +58,21 @@ class UpdateSet throw new NotFoundException('Set not found'); } - $oldIconImageUrl = $set->getIconImageUrl(); - $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; + return $set; } - /** - * @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 + private function hasNoFields(UpdateSetRequest $request): bool { - if (! str_starts_with($path, 'set-icons/')) { - return; - } + return $request->name === null + && $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; } }