implement DeletePost use case

This commit is contained in:
Yisroel Baum 2026-05-06 21:58:25 +03:00
parent fd91da6bab
commit e9ac16377f
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,42 @@
<?php
namespace App\Post\UseCases\DeletePost;
use App\Exceptions\BadRequestException;
use App\Exceptions\ForbiddenException;
use App\Post\PostRepository;
class DeletePost
{
public function __construct(
private PostRepository $postRepo,
) {}
/**
* @throws BadRequestException
* @throws ForbiddenException
*/
public function execute(DeletePostRequest $request): void
{
if ($request->postId <= 0) {
throw new BadRequestException('postId must be positive');
}
if ($request->requesterId <= 0) {
throw new BadRequestException('requesterId must be positive');
}
$post = $this->postRepo->find($request->postId);
if ($post === null) {
return;
}
$isAuthor = $post->getUserId() === $request->requesterId;
if (! $isAuthor && ! $request->requesterIsAdmin) {
throw new ForbiddenException(
'requester is not allowed to delete this post'
);
}
$this->postRepo->delete($request->postId);
}
}

View file

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