implement ClearFeaturedPost use case

This commit is contained in:
Yisroel Baum 2026-05-06 22:30:17 +03:00
parent 7bd3ff78b4
commit a8f59afc30
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,48 @@
<?php
namespace App\Post\UseCases\ClearFeaturedPost;
use App\Exceptions\BadRequestException;
use App\Exceptions\ForbiddenException;
use App\Post\Post;
use App\Post\PostRepository;
class ClearFeaturedPost
{
public function __construct(
private PostRepository $postRepo,
) {}
/**
* @throws BadRequestException
* @throws ForbiddenException
*/
public function execute(ClearFeaturedPostRequest $request): void
{
if (! $request->requesterIsAdmin) {
throw new ForbiddenException(
'only admins can unfeature a post'
);
}
if ($request->postId <= 0) {
throw new BadRequestException('postId must be positive');
}
$post = $this->postRepo->find($request->postId);
if ($post === null) {
return;
}
if (! $post->isFeatured()) {
return;
}
$this->postRepo->update(new Post(
id: $post->getId(),
userId: $post->getUserId(),
title: $post->getTitle(),
body: $post->getBody(),
createdAt: $post->getCreatedAt(),
featureSlot: null,
));
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace App\Post\UseCases\ClearFeaturedPost;
class ClearFeaturedPostRequest
{
public function __construct(
public int $postId,
public bool $requesterIsAdmin,
) {}
}