implement ClearFeaturedPost use case
This commit is contained in:
parent
7bd3ff78b4
commit
a8f59afc30
2 changed files with 59 additions and 0 deletions
|
|
@ -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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Post\UseCases\ClearFeaturedPost;
|
||||||
|
|
||||||
|
class ClearFeaturedPostRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $postId,
|
||||||
|
public bool $requesterIsAdmin,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue