diff --git a/backend/app/Post/UseCases/SetFeaturedPost/SetFeaturedPost.php b/backend/app/Post/UseCases/SetFeaturedPost/SetFeaturedPost.php new file mode 100644 index 0000000..7c508ad --- /dev/null +++ b/backend/app/Post/UseCases/SetFeaturedPost/SetFeaturedPost.php @@ -0,0 +1,69 @@ +requesterIsAdmin) { + throw new ForbiddenException( + 'only admins can feature a post' + ); + } + if ($request->postId <= 0) { + throw new BadRequestException('postId must be positive'); + } + if (! in_array($request->slot, self::VALID_SLOTS, true)) { + throw new BadRequestException( + 'slot must be 1 or 2' + ); + } + + $post = $this->postRepo->find($request->postId); + if ($post === null) { + throw new DomainException('post not found'); + } + + $existingInSlot = $this->postRepo->findByFeatureSlot($request->slot); + if ( + $existingInSlot !== null + && $existingInSlot->getId() !== $post->getId() + ) { + $this->postRepo->update(new Post( + id: $existingInSlot->getId(), + userId: $existingInSlot->getUserId(), + title: $existingInSlot->getTitle(), + body: $existingInSlot->getBody(), + createdAt: $existingInSlot->getCreatedAt(), + featureSlot: null, + )); + } + + return $this->postRepo->update(new Post( + id: $post->getId(), + userId: $post->getUserId(), + title: $post->getTitle(), + body: $post->getBody(), + createdAt: $post->getCreatedAt(), + featureSlot: $request->slot, + )); + } +} diff --git a/backend/app/Post/UseCases/SetFeaturedPost/SetFeaturedPostRequest.php b/backend/app/Post/UseCases/SetFeaturedPost/SetFeaturedPostRequest.php new file mode 100644 index 0000000..10de537 --- /dev/null +++ b/backend/app/Post/UseCases/SetFeaturedPost/SetFeaturedPostRequest.php @@ -0,0 +1,12 @@ +