- Add AI code generation requirements to AGENTS.md - Add module-level docstrings to all 46 Python modules - Add detailed Google-style docstrings to all classes and functions - Remove all inline comments following self-documenting code principle - Include Args, Returns, Raises sections in function docstrings - Add Attributes and Examples sections to class docstrings
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
"""Delete post use case.
|
|
|
|
This module implements the use case for deleting blog posts.
|
|
Includes authorization checks to ensure users can only delete their own posts.
|
|
"""
|
|
|
|
from uuid import UUID
|
|
|
|
from app.application.interfaces import TransactionManager
|
|
from app.domain.exceptions import ForbiddenException, NotFoundException
|
|
from app.domain.repositories import PostRepository
|
|
|
|
|
|
class DeletePostUseCase:
|
|
"""Use case for deleting a blog post.
|
|
|
|
Handles post deletion with authorization checks.
|
|
Users can only delete posts they authored.
|
|
|
|
Attributes:
|
|
_post_repo: Repository for post data access.
|
|
_tx_manager: Transaction manager for commit control.
|
|
|
|
Example:
|
|
>>> use_case = DeletePostUseCase(post_repo, tx_manager)
|
|
>>> await use_case.execute(post_id, user_id)
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
post_repo: PostRepository,
|
|
tx_manager: TransactionManager,
|
|
) -> None:
|
|
"""Initialize use case with dependencies.
|
|
|
|
Args:
|
|
post_repo: Repository for post operations.
|
|
tx_manager: Transaction manager instance.
|
|
"""
|
|
self._post_repo = post_repo
|
|
self._tx_manager = tx_manager
|
|
|
|
async def execute(self, post_id: UUID, current_user_id: str) -> None:
|
|
"""Execute the use case to delete a post.
|
|
|
|
Args:
|
|
post_id: Unique identifier of the post to delete.
|
|
current_user_id: ID of the user requesting deletion.
|
|
|
|
Raises:
|
|
NotFoundException: If post with given ID does not exist.
|
|
ForbiddenException: If user is not the post author.
|
|
"""
|
|
post = await self._post_repo.get_by_id(post_id)
|
|
if not post:
|
|
raise NotFoundException(f"Post with id '{post_id}' not found")
|
|
|
|
if post.author_id != current_user_id:
|
|
raise ForbiddenException("You can only delete your own posts")
|
|
|
|
await self._post_repo.delete(post_id)
|
|
await self._tx_manager.commit()
|