implement DeleteComment use case
This commit is contained in:
parent
5bbef871db
commit
d24bde3761
2 changed files with 54 additions and 0 deletions
42
backend/app/Comment/UseCases/DeleteComment/DeleteComment.php
Normal file
42
backend/app/Comment/UseCases/DeleteComment/DeleteComment.php
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Comment\UseCases\DeleteComment;
|
||||||
|
|
||||||
|
use App\Comment\CommentRepository;
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
|
use App\Exceptions\ForbiddenException;
|
||||||
|
|
||||||
|
class DeleteComment
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private CommentRepository $commentRepo,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BadRequestException
|
||||||
|
* @throws ForbiddenException
|
||||||
|
*/
|
||||||
|
public function execute(DeleteCommentRequest $request): void
|
||||||
|
{
|
||||||
|
if ($request->commentId <= 0) {
|
||||||
|
throw new BadRequestException('commentId must be positive');
|
||||||
|
}
|
||||||
|
if ($request->requesterId <= 0) {
|
||||||
|
throw new BadRequestException('requesterId must be positive');
|
||||||
|
}
|
||||||
|
|
||||||
|
$comment = $this->commentRepo->find($request->commentId);
|
||||||
|
if ($comment === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$isAuthor = $comment->getUserId() === $request->requesterId;
|
||||||
|
if (! $isAuthor && ! $request->requesterIsAdmin) {
|
||||||
|
throw new ForbiddenException(
|
||||||
|
'requester is not allowed to delete this comment'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->commentRepo->delete($request->commentId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Comment\UseCases\DeleteComment;
|
||||||
|
|
||||||
|
class DeleteCommentRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $commentId,
|
||||||
|
public int $requesterId,
|
||||||
|
public bool $requesterIsAdmin,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue