feat: add comments feature with nested replies and recursive rendering
All checks were successful
ci/woodpecker/pr/pipeline Pipeline was successful
All checks were successful
ci/woodpecker/pr/pipeline Pipeline was successful
Implement full comments system: domain entities (Comment, CommentLike), value objects (CommentContent), use cases (CRUD, like toggle), SQLAlchemy repository, API v1 endpoints, web UI with comment form and nested replies, i18n translations (EN/RU/FR/DE), and E2E tests. Fix nested reply (reply-to-reply) not displaying — the flat reply_comments dict was only queried for top-level comment IDs, so deeply nested replies were saved to DB (incrementing comment count) but never rendered. Switch to a recursive Jinja2 macro that renders any nesting depth.
This commit is contained in:
@@ -4,14 +4,24 @@ This module re-exports all application layer components including
|
||||
DTOs, interfaces, and use cases for convenient importing.
|
||||
"""
|
||||
|
||||
from app.application.dtos import CreatePostDTO, PostResponseDTO, UpdatePostDTO
|
||||
from app.application.dtos import (
|
||||
CommentResponseDTO,
|
||||
CreateCommentDTO,
|
||||
CreatePostDTO,
|
||||
PostResponseDTO,
|
||||
UpdatePostDTO,
|
||||
)
|
||||
from app.application.interfaces import TransactionManager
|
||||
from app.application.use_cases import (
|
||||
CreateCommentUseCase,
|
||||
CreatePostUseCase,
|
||||
DeleteCommentUseCase,
|
||||
DeletePostUseCase,
|
||||
GetPostUseCase,
|
||||
ListCommentsUseCase,
|
||||
ListPostsUseCase,
|
||||
PublishPostUseCase,
|
||||
ToggleCommentLikeUseCase,
|
||||
TogglePostLikeUseCase,
|
||||
UpdatePostUseCase,
|
||||
)
|
||||
@@ -20,6 +30,8 @@ __all__ = [
|
||||
"CreatePostDTO",
|
||||
"UpdatePostDTO",
|
||||
"PostResponseDTO",
|
||||
"CreateCommentDTO",
|
||||
"CommentResponseDTO",
|
||||
"TransactionManager",
|
||||
"CreatePostUseCase",
|
||||
"GetPostUseCase",
|
||||
@@ -28,4 +40,8 @@ __all__ = [
|
||||
"ListPostsUseCase",
|
||||
"PublishPostUseCase",
|
||||
"TogglePostLikeUseCase",
|
||||
"CreateCommentUseCase",
|
||||
"DeleteCommentUseCase",
|
||||
"ListCommentsUseCase",
|
||||
"ToggleCommentLikeUseCase",
|
||||
]
|
||||
|
||||
@@ -4,6 +4,13 @@ This module re-exports all Data Transfer Objects used in the
|
||||
application layer for data communication.
|
||||
"""
|
||||
|
||||
from app.application.dtos.comment import CommentResponseDTO, CreateCommentDTO
|
||||
from app.application.dtos.post import CreatePostDTO, PostResponseDTO, UpdatePostDTO
|
||||
|
||||
__all__ = ["CreatePostDTO", "UpdatePostDTO", "PostResponseDTO"]
|
||||
__all__ = [
|
||||
"CreatePostDTO",
|
||||
"UpdatePostDTO",
|
||||
"PostResponseDTO",
|
||||
"CreateCommentDTO",
|
||||
"CommentResponseDTO",
|
||||
]
|
||||
|
||||
55
app/application/dtos/comment.py
Normal file
55
app/application/dtos/comment.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""DTOs for comment use cases.
|
||||
|
||||
This module defines Data Transfer Objects used for communication between
|
||||
application layer comment use cases and presentation layer.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CreateCommentDTO:
|
||||
"""DTO for creating a comment.
|
||||
|
||||
Carries comment creation data from API to use case.
|
||||
|
||||
Attributes:
|
||||
post_id: UUID of the post to comment on.
|
||||
author_id: Identifier of the comment author.
|
||||
content: Comment content string (Markdown supported).
|
||||
parent_id: Optional UUID of parent comment for replies.
|
||||
"""
|
||||
|
||||
post_id: UUID
|
||||
author_id: str
|
||||
content: str
|
||||
parent_id: UUID | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CommentResponseDTO:
|
||||
"""DTO for comment response.
|
||||
|
||||
Carries complete comment data for API responses.
|
||||
|
||||
Attributes:
|
||||
id: Unique comment identifier.
|
||||
post_id: UUID of the parent post.
|
||||
author_id: Comment author identifier.
|
||||
content: Comment content string.
|
||||
parent_id: Optional UUID of parent comment.
|
||||
like_count: Number of likes on this comment.
|
||||
created_at: Creation timestamp.
|
||||
updated_at: Last update timestamp.
|
||||
"""
|
||||
|
||||
id: UUID
|
||||
post_id: UUID
|
||||
author_id: str
|
||||
content: str
|
||||
parent_id: UUID | None = None
|
||||
like_count: int = 0
|
||||
created_at: datetime | None = None
|
||||
updated_at: datetime | None = None
|
||||
@@ -101,3 +101,4 @@ class PostResponseDTO:
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
like_count: int = 0
|
||||
comment_count: int = 0
|
||||
|
||||
@@ -4,11 +4,15 @@ This module re-exports all application use cases that implement
|
||||
business logic operations for the blog API.
|
||||
"""
|
||||
|
||||
from app.application.use_cases.create_comment import CreateCommentUseCase
|
||||
from app.application.use_cases.create_post import CreatePostUseCase
|
||||
from app.application.use_cases.delete_comment import DeleteCommentUseCase
|
||||
from app.application.use_cases.delete_post import DeletePostUseCase
|
||||
from app.application.use_cases.get_post import GetPostUseCase
|
||||
from app.application.use_cases.list_comments import ListCommentsUseCase
|
||||
from app.application.use_cases.list_posts import ListPostsUseCase
|
||||
from app.application.use_cases.publish_post import PublishPostUseCase
|
||||
from app.application.use_cases.toggle_comment_like import ToggleCommentLikeUseCase
|
||||
from app.application.use_cases.toggle_like import TogglePostLikeUseCase
|
||||
from app.application.use_cases.update_post import UpdatePostUseCase
|
||||
|
||||
@@ -20,4 +24,8 @@ __all__ = [
|
||||
"ListPostsUseCase",
|
||||
"PublishPostUseCase",
|
||||
"TogglePostLikeUseCase",
|
||||
"CreateCommentUseCase",
|
||||
"DeleteCommentUseCase",
|
||||
"ListCommentsUseCase",
|
||||
"ToggleCommentLikeUseCase",
|
||||
]
|
||||
|
||||
100
app/application/use_cases/create_comment.py
Normal file
100
app/application/use_cases/create_comment.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""Create comment use case.
|
||||
|
||||
This module implements the use case for creating comments on blog posts.
|
||||
Supports both top-level comments and nested replies via parent_id.
|
||||
"""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.application.dtos.comment import CommentResponseDTO
|
||||
from app.application.interfaces import TransactionManager
|
||||
from app.domain.entities.comment import Comment
|
||||
from app.domain.exceptions import NotFoundException
|
||||
from app.domain.repositories import CommentRepository, PostRepository
|
||||
|
||||
|
||||
class CreateCommentUseCase:
|
||||
"""Use case for creating a comment on a blog post.
|
||||
|
||||
Handles top-level comments and replies to existing comments.
|
||||
Validates that the target post exists before creating.
|
||||
|
||||
Attributes:
|
||||
_post_repo: Repository for post data access.
|
||||
_comment_repo: Repository for comment data access.
|
||||
_tx_manager: Transaction manager for commit control.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
post_repo: PostRepository,
|
||||
comment_repo: CommentRepository,
|
||||
tx_manager: TransactionManager,
|
||||
) -> None:
|
||||
"""Initialize use case with dependencies.
|
||||
|
||||
Args:
|
||||
post_repo: Repository for post operations.
|
||||
comment_repo: Repository for comment operations.
|
||||
tx_manager: Transaction manager instance.
|
||||
"""
|
||||
self._post_repo = post_repo
|
||||
self._comment_repo = comment_repo
|
||||
self._tx_manager = tx_manager
|
||||
|
||||
async def execute(
|
||||
self,
|
||||
post_id: UUID,
|
||||
author_id: str,
|
||||
content: str,
|
||||
parent_id: UUID | None = None,
|
||||
) -> CommentResponseDTO:
|
||||
"""Execute the use case to create a comment.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post to comment on.
|
||||
author_id: Identifier of the comment author.
|
||||
content: Comment content (Markdown supported).
|
||||
parent_id: Optional UUID of parent comment for replies.
|
||||
|
||||
Returns:
|
||||
CommentResponseDTO with created comment data.
|
||||
|
||||
Raises:
|
||||
NotFoundException: If the target post does not exist.
|
||||
"""
|
||||
post = await self._post_repo.get_by_id(post_id)
|
||||
if not post:
|
||||
raise NotFoundException(f"Post with id '{post_id}' not found")
|
||||
|
||||
comment = Comment.create(
|
||||
post_id=post_id,
|
||||
author_id=author_id,
|
||||
content_str=content,
|
||||
parent_id=parent_id,
|
||||
)
|
||||
|
||||
await self._comment_repo.add(comment)
|
||||
await self._tx_manager.commit()
|
||||
|
||||
return self._map_to_dto(comment)
|
||||
|
||||
def _map_to_dto(self, comment: Comment) -> CommentResponseDTO:
|
||||
"""Map domain entity to response DTO.
|
||||
|
||||
Args:
|
||||
comment: Domain Comment entity.
|
||||
|
||||
Returns:
|
||||
CommentResponseDTO with all comment attributes.
|
||||
"""
|
||||
return CommentResponseDTO(
|
||||
id=comment.id,
|
||||
post_id=comment.post_id,
|
||||
author_id=comment.author_id,
|
||||
content=comment.content.value,
|
||||
parent_id=comment.parent_id,
|
||||
like_count=comment.like_count,
|
||||
created_at=comment.created_at,
|
||||
updated_at=comment.updated_at,
|
||||
)
|
||||
60
app/application/use_cases/delete_comment.py
Normal file
60
app/application/use_cases/delete_comment.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""Delete comment use case.
|
||||
|
||||
This module implements the use case for deleting comments.
|
||||
Users can delete their own comments.
|
||||
"""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.application.interfaces import TransactionManager
|
||||
from app.domain.exceptions import ForbiddenException, NotFoundException
|
||||
from app.domain.repositories import CommentRepository
|
||||
|
||||
|
||||
class DeleteCommentUseCase:
|
||||
"""Use case for deleting a comment.
|
||||
|
||||
Allows users to delete their own comments.
|
||||
|
||||
Attributes:
|
||||
_comment_repo: Repository for comment data access.
|
||||
_tx_manager: Transaction manager for commit control.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
comment_repo: CommentRepository,
|
||||
tx_manager: TransactionManager,
|
||||
) -> None:
|
||||
"""Initialize use case with dependencies.
|
||||
|
||||
Args:
|
||||
comment_repo: Repository for comment operations.
|
||||
tx_manager: Transaction manager instance.
|
||||
"""
|
||||
self._comment_repo = comment_repo
|
||||
self._tx_manager = tx_manager
|
||||
|
||||
async def execute(
|
||||
self,
|
||||
comment_id: UUID,
|
||||
user_id: str,
|
||||
) -> None:
|
||||
"""Delete a comment.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment to delete.
|
||||
user_id: Identifier of the user requesting deletion.
|
||||
|
||||
Raises:
|
||||
NotFoundException: If the comment does not exist.
|
||||
"""
|
||||
comment = await self._comment_repo.get_by_id(comment_id)
|
||||
if not comment:
|
||||
raise NotFoundException(f"Comment with id '{comment_id}' not found")
|
||||
|
||||
if comment.author_id != user_id:
|
||||
raise ForbiddenException("You are not allowed to delete this comment")
|
||||
|
||||
await self._comment_repo.delete(comment_id)
|
||||
await self._tx_manager.commit()
|
||||
63
app/application/use_cases/list_comments.py
Normal file
63
app/application/use_cases/list_comments.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""List comments use case.
|
||||
|
||||
This module implements the use case for listing comments on a blog post.
|
||||
"""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.application.dtos.comment import CommentResponseDTO
|
||||
from app.domain.entities.comment import Comment
|
||||
from app.domain.repositories import CommentRepository
|
||||
|
||||
|
||||
class ListCommentsUseCase:
|
||||
"""Use case for listing comments on a blog post.
|
||||
|
||||
Retrieves all comments for a given post ordered by creation time.
|
||||
|
||||
Attributes:
|
||||
_comment_repo: Repository for comment data access.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
comment_repo: CommentRepository,
|
||||
) -> None:
|
||||
"""Initialize use case with dependencies.
|
||||
|
||||
Args:
|
||||
comment_repo: Repository for comment operations.
|
||||
"""
|
||||
self._comment_repo = comment_repo
|
||||
|
||||
async def execute(self, post_id: UUID) -> list[CommentResponseDTO]:
|
||||
"""List all comments for a post.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post.
|
||||
|
||||
Returns:
|
||||
List of CommentResponseDTO for the post.
|
||||
"""
|
||||
comments = await self._comment_repo.get_by_post(post_id)
|
||||
return [self._map_to_dto(c) for c in comments]
|
||||
|
||||
def _map_to_dto(self, comment: Comment) -> CommentResponseDTO:
|
||||
"""Map domain entity to response DTO.
|
||||
|
||||
Args:
|
||||
comment: Domain Comment entity.
|
||||
|
||||
Returns:
|
||||
CommentResponseDTO with all comment attributes.
|
||||
"""
|
||||
return CommentResponseDTO(
|
||||
id=comment.id,
|
||||
post_id=comment.post_id,
|
||||
author_id=comment.author_id,
|
||||
content=comment.content.value,
|
||||
parent_id=comment.parent_id,
|
||||
like_count=comment.like_count,
|
||||
created_at=comment.created_at,
|
||||
updated_at=comment.updated_at,
|
||||
)
|
||||
96
app/application/use_cases/toggle_comment_like.py
Normal file
96
app/application/use_cases/toggle_comment_like.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""Toggle comment like use case.
|
||||
|
||||
This module implements the use case for toggling likes on comments.
|
||||
If the user already liked the comment, the like is removed (unlike).
|
||||
If not, a new like is added.
|
||||
"""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.application.dtos.comment import CommentResponseDTO
|
||||
from app.application.interfaces import TransactionManager
|
||||
from app.domain.entities.comment import Comment
|
||||
from app.domain.entities.comment_like import CommentLike
|
||||
from app.domain.exceptions import NotFoundException
|
||||
from app.domain.repositories import CommentRepository
|
||||
|
||||
|
||||
class ToggleCommentLikeUseCase:
|
||||
"""Use case for toggling a like on a comment.
|
||||
|
||||
Handles like/unlike toggle logic. If the user has already liked
|
||||
the comment, the like is removed. Otherwise, a new like is created.
|
||||
|
||||
Attributes:
|
||||
_comment_repo: Repository for comment and like data access.
|
||||
_tx_manager: Transaction manager for commit control.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
comment_repo: CommentRepository,
|
||||
tx_manager: TransactionManager,
|
||||
) -> None:
|
||||
"""Initialize use case with dependencies.
|
||||
|
||||
Args:
|
||||
comment_repo: Repository for comment and like operations.
|
||||
tx_manager: Transaction manager instance.
|
||||
"""
|
||||
self._comment_repo = comment_repo
|
||||
self._tx_manager = tx_manager
|
||||
|
||||
async def execute(self, comment_id: UUID, liked_by: str) -> CommentResponseDTO:
|
||||
"""Toggle like on a comment.
|
||||
|
||||
If the user already liked the comment, remove the like.
|
||||
Otherwise, add a new like.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment to toggle like on.
|
||||
liked_by: User ID.
|
||||
|
||||
Returns:
|
||||
CommentResponseDTO with updated like_count.
|
||||
|
||||
Raises:
|
||||
NotFoundException: If comment with given ID does not exist.
|
||||
"""
|
||||
comment = await self._comment_repo.get_by_id(comment_id)
|
||||
if not comment:
|
||||
raise NotFoundException(f"Comment with id '{comment_id}' not found")
|
||||
|
||||
existing_like = await self._comment_repo.get_like(comment_id, liked_by)
|
||||
|
||||
if existing_like:
|
||||
await self._comment_repo.remove_like(comment_id, liked_by)
|
||||
comment.like_count = max(0, comment.like_count - 1)
|
||||
else:
|
||||
new_like = CommentLike(comment_id=comment_id, liked_by=liked_by)
|
||||
await self._comment_repo.add_like(new_like)
|
||||
comment.like_count += 1
|
||||
|
||||
await self._comment_repo.update(comment)
|
||||
await self._tx_manager.commit()
|
||||
|
||||
return self._map_to_dto(comment)
|
||||
|
||||
def _map_to_dto(self, comment: Comment) -> CommentResponseDTO:
|
||||
"""Map domain entity to response DTO.
|
||||
|
||||
Args:
|
||||
comment: Domain Comment entity.
|
||||
|
||||
Returns:
|
||||
CommentResponseDTO with all comment attributes including like_count.
|
||||
"""
|
||||
return CommentResponseDTO(
|
||||
id=comment.id,
|
||||
post_id=comment.post_id,
|
||||
author_id=comment.author_id,
|
||||
content=comment.content.value,
|
||||
parent_id=comment.parent_id,
|
||||
like_count=comment.like_count,
|
||||
created_at=comment.created_at,
|
||||
updated_at=comment.updated_at,
|
||||
)
|
||||
@@ -4,7 +4,7 @@ This module re-exports all domain layer components including
|
||||
entities, value objects, repositories, and exceptions.
|
||||
"""
|
||||
|
||||
from app.domain.entities import BaseEntity, Post, PostLike
|
||||
from app.domain.entities import BaseEntity, Comment, CommentLike, Post, PostLike
|
||||
from app.domain.exceptions import (
|
||||
AlreadyExistsException,
|
||||
DomainException,
|
||||
@@ -13,19 +13,22 @@ from app.domain.exceptions import (
|
||||
UnauthorizedException,
|
||||
ValidationException,
|
||||
)
|
||||
from app.domain.repositories import PostRepository, Repository
|
||||
from app.domain.repositories import CommentRepository, PostRepository, Repository
|
||||
from app.domain.value_objects import Content, Slug, Title, ValueObject
|
||||
|
||||
__all__ = [
|
||||
"BaseEntity",
|
||||
"Post",
|
||||
"PostLike",
|
||||
"Comment",
|
||||
"CommentLike",
|
||||
"ValueObject",
|
||||
"Title",
|
||||
"Content",
|
||||
"Slug",
|
||||
"Repository",
|
||||
"PostRepository",
|
||||
"CommentRepository",
|
||||
"DomainException",
|
||||
"ValidationException",
|
||||
"NotFoundException",
|
||||
|
||||
@@ -5,7 +5,9 @@ core business objects with identity.
|
||||
"""
|
||||
|
||||
from app.domain.entities.base import BaseEntity
|
||||
from app.domain.entities.comment import Comment
|
||||
from app.domain.entities.comment_like import CommentLike
|
||||
from app.domain.entities.like import PostLike
|
||||
from app.domain.entities.post import Post
|
||||
|
||||
__all__ = ["BaseEntity", "Post", "PostLike"]
|
||||
__all__ = ["BaseEntity", "Post", "PostLike", "Comment", "CommentLike"]
|
||||
|
||||
79
app/domain/entities/comment.py
Normal file
79
app/domain/entities/comment.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Domain entity for Comment.
|
||||
|
||||
This module defines the Comment entity that represents a comment on a blog
|
||||
post. Comments can be top-level (parent_id=None) or replies to other
|
||||
comments (parent_id set).
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from app.domain.entities.base import BaseEntity
|
||||
from app.domain.value_objects.comment_content import CommentContent
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Comment(BaseEntity):
|
||||
"""Comment domain entity.
|
||||
|
||||
Represents a comment on a blog post with optional parent reference
|
||||
for nested replies. Supports Markdown content and like tracking.
|
||||
|
||||
Attributes:
|
||||
post_id: UUID of the post this comment belongs to.
|
||||
author_id: Identifier of the comment author.
|
||||
content: CommentContent value object with Markdown text.
|
||||
parent_id: UUID of parent comment for replies, or None.
|
||||
like_count: Number of likes on this comment.
|
||||
"""
|
||||
|
||||
post_id: UUID
|
||||
author_id: str
|
||||
content: CommentContent
|
||||
parent_id: UUID | None = None
|
||||
like_count: int = 0
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert entity to dictionary.
|
||||
|
||||
Returns:
|
||||
Dictionary representation with all comment attributes.
|
||||
"""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"post_id": str(self.post_id),
|
||||
"author_id": self.author_id,
|
||||
"content": self.content.value,
|
||||
"parent_id": str(self.parent_id) if self.parent_id else None,
|
||||
"like_count": self.like_count,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
post_id: UUID,
|
||||
author_id: str,
|
||||
content_str: str,
|
||||
parent_id: UUID | None = None,
|
||||
) -> "Comment":
|
||||
"""Factory method to create a new comment.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post to comment on.
|
||||
author_id: Identifier of the comment author.
|
||||
content_str: Comment content string (Markdown supported).
|
||||
parent_id: Optional UUID of parent comment for replies.
|
||||
|
||||
Returns:
|
||||
New Comment instance with validated content.
|
||||
"""
|
||||
content = CommentContent(content_str)
|
||||
return cls(
|
||||
post_id=post_id,
|
||||
author_id=author_id,
|
||||
content=content,
|
||||
parent_id=parent_id,
|
||||
)
|
||||
40
app/domain/entities/comment_like.py
Normal file
40
app/domain/entities/comment_like.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Domain entity for CommentLike.
|
||||
|
||||
This module defines the CommentLike entity that tracks which users
|
||||
have liked which comments.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from app.domain.entities.base import BaseEntity
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class CommentLike(BaseEntity):
|
||||
"""Comment like domain entity.
|
||||
|
||||
Tracks a like on a comment by a user. Each like is uniquely
|
||||
identified by its entity ID.
|
||||
|
||||
Attributes:
|
||||
comment_id: UUID of the liked comment.
|
||||
liked_by: Identifier of the user who liked.
|
||||
"""
|
||||
|
||||
comment_id: UUID
|
||||
liked_by: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert entity to dictionary.
|
||||
|
||||
Returns:
|
||||
Dictionary with all CommentLike attributes.
|
||||
"""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"comment_id": str(self.comment_id),
|
||||
"liked_by": self.liked_by,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
}
|
||||
@@ -5,6 +5,7 @@ the contract for data access operations.
|
||||
"""
|
||||
|
||||
from app.domain.repositories.base import Repository
|
||||
from app.domain.repositories.comment import CommentRepository
|
||||
from app.domain.repositories.post import PostRepository
|
||||
|
||||
__all__ = ["Repository", "PostRepository"]
|
||||
__all__ = ["Repository", "PostRepository", "CommentRepository"]
|
||||
|
||||
80
app/domain/repositories/comment.py
Normal file
80
app/domain/repositories/comment.py
Normal file
@@ -0,0 +1,80 @@
|
||||
"""Comment repository interface.
|
||||
|
||||
This module defines the repository interface for Comment entities
|
||||
including nested comment queries and like management.
|
||||
"""
|
||||
|
||||
from abc import abstractmethod
|
||||
from uuid import UUID
|
||||
|
||||
from app.domain.entities.comment import Comment
|
||||
from app.domain.entities.comment_like import CommentLike
|
||||
from app.domain.repositories.base import Repository
|
||||
|
||||
|
||||
class CommentRepository(Repository[Comment]):
|
||||
"""Repository interface for Comments.
|
||||
|
||||
Extends the generic repository with comment-specific operations
|
||||
including post-based listing and like management.
|
||||
|
||||
Example:
|
||||
>>> comments = await repo.get_by_post(post_id)
|
||||
>>> like = await repo.get_like(comment_id, "user-123")
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def get_by_post(self, post_id: UUID) -> list[Comment]:
|
||||
"""Get all comments for a post, ordered by creation time.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post.
|
||||
|
||||
Returns:
|
||||
List of Comment entities for the post.
|
||||
"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def get_like(self, comment_id: UUID, liked_by: str) -> CommentLike | None:
|
||||
"""Get a like by comment and user.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment.
|
||||
liked_by: User ID.
|
||||
|
||||
Returns:
|
||||
CommentLike if found, None otherwise.
|
||||
"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def add_like(self, like: CommentLike) -> None:
|
||||
"""Add a new like to a comment.
|
||||
|
||||
Args:
|
||||
like: CommentLike entity to add.
|
||||
"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def count_by_post(self, post_id: UUID) -> int:
|
||||
"""Get comment count for a post.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post.
|
||||
|
||||
Returns:
|
||||
Number of comments on the post.
|
||||
"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def remove_like(self, comment_id: UUID, liked_by: str) -> None:
|
||||
"""Remove a like from a comment by user.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment.
|
||||
liked_by: User ID.
|
||||
"""
|
||||
...
|
||||
@@ -5,8 +5,9 @@ immutable validated domain concepts.
|
||||
"""
|
||||
|
||||
from app.domain.value_objects.base import ValueObject
|
||||
from app.domain.value_objects.comment_content import CommentContent
|
||||
from app.domain.value_objects.content import Content
|
||||
from app.domain.value_objects.slug import Slug
|
||||
from app.domain.value_objects.title import Title
|
||||
|
||||
__all__ = ["ValueObject", "Title", "Content", "Slug"]
|
||||
__all__ = ["ValueObject", "Title", "Content", "Slug", "CommentContent"]
|
||||
|
||||
47
app/domain/value_objects/comment_content.py
Normal file
47
app/domain/value_objects/comment_content.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Value object for comment content.
|
||||
|
||||
This module defines the CommentContent value object that validates
|
||||
and encapsulates comment text content.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from app.domain.value_objects.base import ValueObject
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CommentContent(ValueObject[str]):
|
||||
"""Comment content value object.
|
||||
|
||||
Wraps and validates comment content ensuring it meets length
|
||||
requirements and is not empty.
|
||||
|
||||
Attributes:
|
||||
value: The comment content string.
|
||||
MAX_LENGTH: Maximum allowed content length (5000 characters).
|
||||
|
||||
Raises:
|
||||
ValueError: If content is empty or too long.
|
||||
|
||||
Example:
|
||||
>>> content = CommentContent("This is a **bold** comment.")
|
||||
>>> content.value
|
||||
'This is a **bold** comment.'
|
||||
"""
|
||||
|
||||
MAX_LENGTH: int = 5000
|
||||
|
||||
def _validate(self) -> None:
|
||||
"""Validate comment content.
|
||||
|
||||
Checks that content is a non-empty string within length bounds.
|
||||
|
||||
Raises:
|
||||
ValueError: If content fails validation criteria.
|
||||
"""
|
||||
if not isinstance(self.value, str):
|
||||
raise ValueError("Comment content must be a string")
|
||||
if not self.value.strip():
|
||||
raise ValueError("Comment content cannot be empty")
|
||||
if len(self.value) > self.MAX_LENGTH:
|
||||
raise ValueError(f"Comment content must be at most {self.MAX_LENGTH} characters")
|
||||
@@ -4,10 +4,12 @@ This module defines the database ORM models that map to database tables.
|
||||
Models are used by repositories for data persistence.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, String, Text
|
||||
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, declarative_base, mapped_column, relationship
|
||||
|
||||
Base = declarative_base()
|
||||
@@ -43,7 +45,10 @@ class PostORM(Base): # type: ignore[valid-type,misc]
|
||||
author_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
published: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True)
|
||||
like_count: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
likes: Mapped[list["PostLikeORM"]] = relationship(
|
||||
likes: Mapped[list[PostLikeORM]] = relationship(
|
||||
back_populates="post", cascade="all, delete-orphan"
|
||||
)
|
||||
comments: Mapped[list[CommentORM]] = relationship(
|
||||
back_populates="post", cascade="all, delete-orphan"
|
||||
)
|
||||
tags: Mapped[list[str]] = mapped_column(JSON, default=list)
|
||||
@@ -87,3 +92,87 @@ class PostLikeORM(Base): # type: ignore[valid-type,misc]
|
||||
)
|
||||
|
||||
post: Mapped[PostORM] = relationship(back_populates="likes")
|
||||
|
||||
|
||||
class CommentORM(Base): # type: ignore[valid-type,misc]
|
||||
"""SQLAlchemy model for Comment.
|
||||
|
||||
Database table representation of comments on blog posts.
|
||||
Maps to the 'comments' table with support for nested replies.
|
||||
|
||||
Attributes:
|
||||
id: Primary key as UUID string.
|
||||
post_id: Foreign key to the parent post.
|
||||
author_id: Comment author reference.
|
||||
content: Comment text content (Markdown supported).
|
||||
parent_id: Optional foreign key to parent comment (replies).
|
||||
like_count: Number of likes on this comment.
|
||||
created_at: Creation timestamp.
|
||||
updated_at: Last update timestamp.
|
||||
"""
|
||||
|
||||
__tablename__ = "comments"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
|
||||
post_id: Mapped[str] = mapped_column(
|
||||
String(36), ForeignKey("posts.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
author_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
parent_id: Mapped[str | None] = mapped_column(
|
||||
String(36),
|
||||
ForeignKey("comments.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
like_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
post: Mapped[PostORM] = relationship(back_populates="comments")
|
||||
replies: Mapped[list[CommentORM]] = relationship(
|
||||
back_populates="parent",
|
||||
)
|
||||
parent: Mapped[CommentORM | None] = relationship(
|
||||
back_populates="replies",
|
||||
remote_side="CommentORM.id",
|
||||
)
|
||||
|
||||
|
||||
class CommentLikeORM(Base): # type: ignore[valid-type,misc]
|
||||
"""SQLAlchemy model for CommentLike.
|
||||
|
||||
Database table representation of comment likes.
|
||||
Maps to the 'comment_likes' table tracking which users liked which comments.
|
||||
|
||||
Attributes:
|
||||
id: Primary key as UUID string.
|
||||
comment_id: Foreign key to the liked comment.
|
||||
liked_by: User identifier.
|
||||
created_at: Creation timestamp.
|
||||
"""
|
||||
|
||||
__tablename__ = "comment_likes"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
|
||||
comment_id: Mapped[str] = mapped_column(
|
||||
String(36),
|
||||
ForeignKey("comments.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
liked_by: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
@@ -10,19 +10,24 @@ from dishka import Provider, Scope, provide
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
|
||||
|
||||
from app.application import (
|
||||
CreateCommentUseCase,
|
||||
CreatePostUseCase,
|
||||
DeleteCommentUseCase,
|
||||
DeletePostUseCase,
|
||||
GetPostUseCase,
|
||||
ListCommentsUseCase,
|
||||
ListPostsUseCase,
|
||||
PublishPostUseCase,
|
||||
ToggleCommentLikeUseCase,
|
||||
TogglePostLikeUseCase,
|
||||
UpdatePostUseCase,
|
||||
)
|
||||
from app.application.interfaces import TransactionManager
|
||||
from app.domain.repositories import PostRepository
|
||||
from app.domain.repositories import CommentRepository, PostRepository
|
||||
from app.infrastructure.auth import KeycloakAuthClient, MockKeycloakClient
|
||||
from app.infrastructure.config.settings import settings
|
||||
from app.infrastructure.database.connection import AsyncSessionLocal, engine
|
||||
from app.infrastructure.repositories.comment import SQLAlchemyCommentRepository
|
||||
from app.infrastructure.repositories.post import SQLAlchemyPostRepository
|
||||
|
||||
|
||||
@@ -81,6 +86,18 @@ class RepositoryProvider(Provider):
|
||||
"""
|
||||
return SQLAlchemyPostRepository(session)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_comment_repository(self, session: AsyncSession) -> CommentRepository:
|
||||
"""Provide CommentRepository implementation.
|
||||
|
||||
Args:
|
||||
session: Database session from DI container.
|
||||
|
||||
Returns:
|
||||
SQLAlchemyCommentRepository instance.
|
||||
"""
|
||||
return SQLAlchemyCommentRepository(session)
|
||||
|
||||
|
||||
class TransactionManagerProvider(Provider):
|
||||
"""Provider for transaction manager.
|
||||
@@ -257,6 +274,86 @@ class UseCaseProvider(Provider):
|
||||
tx_manager=tx_manager,
|
||||
)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_create_comment_use_case(
|
||||
self,
|
||||
post_repo: PostRepository,
|
||||
comment_repo: CommentRepository,
|
||||
tx_manager: TransactionManager,
|
||||
) -> CreateCommentUseCase:
|
||||
"""Provide CreateCommentUseCase.
|
||||
|
||||
Args:
|
||||
post_repo: Post repository dependency.
|
||||
comment_repo: Comment repository dependency.
|
||||
tx_manager: Transaction manager dependency.
|
||||
|
||||
Returns:
|
||||
Configured CreateCommentUseCase instance.
|
||||
"""
|
||||
return CreateCommentUseCase(
|
||||
post_repo=post_repo,
|
||||
comment_repo=comment_repo,
|
||||
tx_manager=tx_manager,
|
||||
)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_list_comments_use_case(
|
||||
self,
|
||||
comment_repo: CommentRepository,
|
||||
) -> ListCommentsUseCase:
|
||||
"""Provide ListCommentsUseCase.
|
||||
|
||||
Args:
|
||||
comment_repo: Comment repository dependency.
|
||||
|
||||
Returns:
|
||||
Configured ListCommentsUseCase instance.
|
||||
"""
|
||||
return ListCommentsUseCase(
|
||||
comment_repo=comment_repo,
|
||||
)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_delete_comment_use_case(
|
||||
self,
|
||||
comment_repo: CommentRepository,
|
||||
tx_manager: TransactionManager,
|
||||
) -> DeleteCommentUseCase:
|
||||
"""Provide DeleteCommentUseCase.
|
||||
|
||||
Args:
|
||||
comment_repo: Comment repository dependency.
|
||||
tx_manager: Transaction manager dependency.
|
||||
|
||||
Returns:
|
||||
Configured DeleteCommentUseCase instance.
|
||||
"""
|
||||
return DeleteCommentUseCase(
|
||||
comment_repo=comment_repo,
|
||||
tx_manager=tx_manager,
|
||||
)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
def get_toggle_comment_like_use_case(
|
||||
self,
|
||||
comment_repo: CommentRepository,
|
||||
tx_manager: TransactionManager,
|
||||
) -> ToggleCommentLikeUseCase:
|
||||
"""Provide ToggleCommentLikeUseCase.
|
||||
|
||||
Args:
|
||||
comment_repo: Comment repository dependency.
|
||||
tx_manager: Transaction manager dependency.
|
||||
|
||||
Returns:
|
||||
Configured ToggleCommentLikeUseCase instance.
|
||||
"""
|
||||
return ToggleCommentLikeUseCase(
|
||||
comment_repo=comment_repo,
|
||||
tx_manager=tx_manager,
|
||||
)
|
||||
|
||||
|
||||
class KeycloakProvider(Provider):
|
||||
"""Provider for Keycloak authentication client.
|
||||
|
||||
@@ -43,6 +43,16 @@ TRANSLATIONS: dict[str, dict[str, str]] = {
|
||||
"post.edit": "Edit",
|
||||
"post.delete": "Delete",
|
||||
"post.delete_confirm": "Are you sure you want to delete this post?",
|
||||
"post.comments": "Comments",
|
||||
"post.write_comment": "Write a Comment",
|
||||
"post.comment_placeholder": "Write a comment... Markdown is supported.",
|
||||
"post.replying_to": "Replying to",
|
||||
"post.cancel_reply": "Cancel reply",
|
||||
"post.cancel": "Cancel",
|
||||
"post.submit_comment": "Submit Comment",
|
||||
"post.reply": "Reply",
|
||||
"post.no_comments": "No comments yet. Be the first to comment!",
|
||||
"post.comment_error": "Failed to post comment. Please try again.",
|
||||
"post_form.title_edit": "Edit Post",
|
||||
"post_form.title_new": "New Post",
|
||||
"post_form.page_title_edit": "Edit Post",
|
||||
@@ -125,6 +135,16 @@ TRANSLATIONS: dict[str, dict[str, str]] = {
|
||||
"post.edit": "Редактировать",
|
||||
"post.delete": "Удалить",
|
||||
"post.delete_confirm": "Вы уверены, что хотите удалить эту статью?",
|
||||
"post.comments": "Комментарии",
|
||||
"post.write_comment": "Написать комментарий",
|
||||
"post.comment_placeholder": "Напишите комментарий... Поддерживается Markdown.",
|
||||
"post.replying_to": "Ответ",
|
||||
"post.cancel_reply": "Отменить ответ",
|
||||
"post.cancel": "Отмена",
|
||||
"post.submit_comment": "Отправить",
|
||||
"post.reply": "Ответить",
|
||||
"post.no_comments": "Пока нет комментариев. Будьте первым!",
|
||||
"post.comment_error": "Не удалось отправить комментарий. Попробуйте снова.",
|
||||
"post_form.title_edit": "Редактировать статью",
|
||||
"post_form.title_new": "Новая статья",
|
||||
"post_form.page_title_edit": "Редактировать статью",
|
||||
@@ -207,6 +227,16 @@ TRANSLATIONS: dict[str, dict[str, str]] = {
|
||||
"post.edit": "Modifier",
|
||||
"post.delete": "Supprimer",
|
||||
"post.delete_confirm": "Êtes-vous sûr de vouloir supprimer cet article ?",
|
||||
"post.comments": "Commentaires",
|
||||
"post.write_comment": "Écrire un commentaire",
|
||||
"post.comment_placeholder": "Écrivez un commentaire... Markdown est supporté.",
|
||||
"post.replying_to": "Répondre à",
|
||||
"post.cancel_reply": "Annuler la réponse",
|
||||
"post.cancel": "Annuler",
|
||||
"post.submit_comment": "Soumettre",
|
||||
"post.reply": "Répondre",
|
||||
"post.no_comments": "Aucun commentaire pour le moment. Soyez le premier !",
|
||||
"post.comment_error": "Échec de l'envoi du commentaire. Veuillez réessayer.",
|
||||
"post_form.title_edit": "Modifier l'article",
|
||||
"post_form.title_new": "Nouvel article",
|
||||
"post_form.page_title_edit": "Modifier l'article",
|
||||
@@ -289,6 +319,16 @@ TRANSLATIONS: dict[str, dict[str, str]] = {
|
||||
"post.edit": "Bearbeiten",
|
||||
"post.delete": "Löschen",
|
||||
"post.delete_confirm": "Sind Sie sicher, dass Sie diesen Beitrag löschen möchten?",
|
||||
"post.comments": "Kommentare",
|
||||
"post.write_comment": "Kommentar schreiben",
|
||||
"post.comment_placeholder": "Schreiben Sie einen Kommentar... Markdown wird unterstützt.",
|
||||
"post.replying_to": "Antwort an",
|
||||
"post.cancel_reply": "Antwort abbrechen",
|
||||
"post.cancel": "Abbrechen",
|
||||
"post.submit_comment": "Absenden",
|
||||
"post.reply": "Antworten",
|
||||
"post.no_comments": "Noch keine Kommentare. Seien Sie der Erste!",
|
||||
"post.comment_error": "Kommentar konnte nicht gesendet werden. Bitte versuchen Sie es erneut.",
|
||||
"post_form.title_edit": "Beitrag bearbeiten",
|
||||
"post_form.title_new": "Neuer Beitrag",
|
||||
"post_form.page_title_edit": "Beitrag bearbeiten",
|
||||
|
||||
240
app/infrastructure/repositories/comment.py
Normal file
240
app/infrastructure/repositories/comment.py
Normal file
@@ -0,0 +1,240 @@
|
||||
"""SQLAlchemy implementation of CommentRepository.
|
||||
|
||||
This module provides the concrete implementation of CommentRepository
|
||||
using SQLAlchemy ORM for data persistence.
|
||||
"""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.entities.comment import Comment
|
||||
from app.domain.entities.comment_like import CommentLike
|
||||
from app.domain.repositories import CommentRepository
|
||||
from app.domain.value_objects.comment_content import CommentContent
|
||||
from app.infrastructure.database.models import CommentLikeORM, CommentORM
|
||||
|
||||
|
||||
class SQLAlchemyCommentRepository(CommentRepository):
|
||||
"""SQLAlchemy implementation of Comment repository.
|
||||
|
||||
Provides data access methods for Comment entities using SQLAlchemy ORM.
|
||||
Handles conversion between domain entities and ORM models.
|
||||
|
||||
Attributes:
|
||||
_session: SQLAlchemy async session for database operations.
|
||||
"""
|
||||
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
"""Initialize repository with session.
|
||||
|
||||
Args:
|
||||
session: SQLAlchemy async session instance.
|
||||
"""
|
||||
self._session = session
|
||||
|
||||
def _to_domain(self, orm: CommentORM) -> Comment:
|
||||
"""Convert ORM model to domain entity.
|
||||
|
||||
Args:
|
||||
orm: SQLAlchemy ORM model instance.
|
||||
|
||||
Returns:
|
||||
Domain Comment entity with validated value objects.
|
||||
"""
|
||||
return Comment(
|
||||
id=UUID(orm.id),
|
||||
post_id=UUID(orm.post_id),
|
||||
author_id=orm.author_id,
|
||||
content=CommentContent(orm.content),
|
||||
parent_id=UUID(orm.parent_id) if orm.parent_id else None,
|
||||
like_count=orm.like_count,
|
||||
created_at=orm.created_at,
|
||||
updated_at=orm.updated_at,
|
||||
)
|
||||
|
||||
def _to_orm(self, comment: Comment) -> CommentORM:
|
||||
"""Convert domain entity to ORM model.
|
||||
|
||||
Args:
|
||||
comment: Domain Comment entity.
|
||||
|
||||
Returns:
|
||||
SQLAlchemy ORM model instance.
|
||||
"""
|
||||
return CommentORM(
|
||||
id=str(comment.id),
|
||||
post_id=str(comment.post_id),
|
||||
author_id=comment.author_id,
|
||||
content=comment.content.value,
|
||||
parent_id=str(comment.parent_id) if comment.parent_id else None,
|
||||
like_count=comment.like_count,
|
||||
created_at=comment.created_at,
|
||||
updated_at=comment.updated_at,
|
||||
)
|
||||
|
||||
async def get_by_id(self, entity_id: UUID) -> Comment | None:
|
||||
"""Get comment by ID.
|
||||
|
||||
Args:
|
||||
entity_id: Unique identifier of the comment.
|
||||
|
||||
Returns:
|
||||
Comment entity if found, None otherwise.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentORM).where(CommentORM.id == str(entity_id))
|
||||
)
|
||||
orm = result.scalar_one_or_none()
|
||||
return self._to_domain(orm) if orm else None
|
||||
|
||||
async def get_all(self) -> list[Comment]:
|
||||
"""Get all comments.
|
||||
|
||||
Returns:
|
||||
List of all Comment entities.
|
||||
"""
|
||||
result = await self._session.execute(select(CommentORM))
|
||||
orms = result.scalars().all()
|
||||
return [self._to_domain(orm) for orm in orms]
|
||||
|
||||
async def add(self, entity: Comment) -> None:
|
||||
"""Add new comment.
|
||||
|
||||
Args:
|
||||
entity: Comment entity to add.
|
||||
"""
|
||||
orm = self._to_orm(entity)
|
||||
self._session.add(orm)
|
||||
|
||||
async def update(self, entity: Comment) -> None:
|
||||
"""Update existing comment.
|
||||
|
||||
Args:
|
||||
entity: Comment entity with updated data.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentORM).where(CommentORM.id == str(entity.id))
|
||||
)
|
||||
orm = result.scalar_one()
|
||||
|
||||
orm.content = entity.content.value
|
||||
orm.like_count = entity.like_count
|
||||
orm.updated_at = entity.updated_at
|
||||
|
||||
async def delete(self, entity_id: UUID) -> None:
|
||||
"""Delete comment by ID.
|
||||
|
||||
Args:
|
||||
entity_id: Unique identifier of the comment to delete.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentORM).where(CommentORM.id == str(entity_id))
|
||||
)
|
||||
orm = result.scalar_one_or_none()
|
||||
if orm:
|
||||
await self._session.delete(orm)
|
||||
|
||||
async def exists(self, entity_id: UUID) -> bool:
|
||||
"""Check if comment exists.
|
||||
|
||||
Args:
|
||||
entity_id: Unique identifier of the comment.
|
||||
|
||||
Returns:
|
||||
True if comment exists, False otherwise.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentORM).where(CommentORM.id == str(entity_id))
|
||||
)
|
||||
return result.scalar_one_or_none() is not None
|
||||
|
||||
async def get_by_post(self, post_id: UUID) -> list[Comment]:
|
||||
"""Get all comments for a post, ordered by creation time.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post.
|
||||
|
||||
Returns:
|
||||
List of Comment entities for the post.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentORM)
|
||||
.where(CommentORM.post_id == str(post_id))
|
||||
.order_by(CommentORM.created_at.asc())
|
||||
)
|
||||
orms = result.scalars().all()
|
||||
return [self._to_domain(orm) for orm in orms]
|
||||
|
||||
async def count_by_post(self, post_id: UUID) -> int:
|
||||
"""Get comment count for a post.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post.
|
||||
|
||||
Returns:
|
||||
Number of comments on the post.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(func.count()).select_from(CommentORM).where(CommentORM.post_id == str(post_id))
|
||||
)
|
||||
count: int = result.scalar() or 0
|
||||
return count
|
||||
|
||||
async def get_like(self, comment_id: UUID, liked_by: str) -> CommentLike | None:
|
||||
"""Get a like by comment and user.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment.
|
||||
liked_by: User ID.
|
||||
|
||||
Returns:
|
||||
CommentLike if found, None otherwise.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentLikeORM).where(
|
||||
CommentLikeORM.comment_id == str(comment_id),
|
||||
CommentLikeORM.liked_by == liked_by,
|
||||
)
|
||||
)
|
||||
orm = result.scalar_one_or_none()
|
||||
if not orm:
|
||||
return None
|
||||
return CommentLike(
|
||||
id=UUID(orm.id),
|
||||
comment_id=UUID(orm.comment_id),
|
||||
liked_by=orm.liked_by,
|
||||
created_at=orm.created_at,
|
||||
)
|
||||
|
||||
async def add_like(self, like: CommentLike) -> None:
|
||||
"""Add a new like to a comment.
|
||||
|
||||
Args:
|
||||
like: CommentLike entity to add.
|
||||
"""
|
||||
orm = CommentLikeORM(
|
||||
id=str(like.id),
|
||||
comment_id=str(like.comment_id),
|
||||
liked_by=like.liked_by,
|
||||
created_at=like.created_at,
|
||||
)
|
||||
self._session.add(orm)
|
||||
|
||||
async def remove_like(self, comment_id: UUID, liked_by: str) -> None:
|
||||
"""Remove a like from a comment by user.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment.
|
||||
liked_by: User ID.
|
||||
"""
|
||||
result = await self._session.execute(
|
||||
select(CommentLikeORM).where(
|
||||
CommentLikeORM.comment_id == str(comment_id),
|
||||
CommentLikeORM.liked_by == liked_by,
|
||||
)
|
||||
)
|
||||
orm = result.scalar_one_or_none()
|
||||
if orm:
|
||||
await self._session.delete(orm)
|
||||
@@ -11,11 +11,15 @@ from fastapi import Depends, Request
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from app.application import (
|
||||
CreateCommentUseCase,
|
||||
CreatePostUseCase,
|
||||
DeleteCommentUseCase,
|
||||
DeletePostUseCase,
|
||||
GetPostUseCase,
|
||||
ListCommentsUseCase,
|
||||
ListPostsUseCase,
|
||||
PublishPostUseCase,
|
||||
ToggleCommentLikeUseCase,
|
||||
TogglePostLikeUseCase,
|
||||
UpdatePostUseCase,
|
||||
)
|
||||
@@ -31,6 +35,11 @@ ListPostsDep = FromDishka[ListPostsUseCase]
|
||||
PublishPostDep = FromDishka[PublishPostUseCase]
|
||||
ToggleLikeDep = FromDishka[TogglePostLikeUseCase]
|
||||
|
||||
CreateCommentDep = FromDishka[CreateCommentUseCase]
|
||||
DeleteCommentDep = FromDishka[DeleteCommentUseCase]
|
||||
ListCommentsDep = FromDishka[ListCommentsUseCase]
|
||||
ToggleCommentLikeDep = FromDishka[ToggleCommentLikeUseCase]
|
||||
|
||||
security = HTTPBearer(auto_error=False)
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ all v1 endpoint routers.
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.presentation.api.v1.comments import router as comments_router
|
||||
from app.presentation.api.v1.posts import router as posts_router
|
||||
|
||||
router = APIRouter(prefix="/v1")
|
||||
router.include_router(posts_router)
|
||||
router.include_router(comments_router)
|
||||
|
||||
131
app/presentation/api/v1/comments.py
Normal file
131
app/presentation/api/v1/comments.py
Normal file
@@ -0,0 +1,131 @@
|
||||
"""Comments API routes.
|
||||
|
||||
This module defines FastAPI routes for comment operations including
|
||||
CRUD and like/unlike toggle.
|
||||
"""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from dishka.integrations.fastapi import DishkaRoute
|
||||
from fastapi import APIRouter, status
|
||||
|
||||
from app.presentation.api.deps import (
|
||||
CreateCommentDep,
|
||||
CurrentRoleDep,
|
||||
CurrentUserDep,
|
||||
DeleteCommentDep,
|
||||
ListCommentsDep,
|
||||
ToggleCommentLikeDep,
|
||||
)
|
||||
from app.presentation.schemas import (
|
||||
CommentCreateSchema,
|
||||
CommentLikeResponseSchema,
|
||||
CommentResponseSchema,
|
||||
)
|
||||
|
||||
router = APIRouter(tags=["comments"], route_class=DishkaRoute)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/posts/{post_id}/comments",
|
||||
response_model=CommentResponseSchema,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
summary="Create a comment on a post",
|
||||
)
|
||||
async def create_comment(
|
||||
post_id: UUID,
|
||||
schema: CommentCreateSchema,
|
||||
use_case: CreateCommentDep,
|
||||
current_user_id: CurrentUserDep,
|
||||
) -> CommentResponseSchema:
|
||||
"""Create a comment on a blog post.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post to comment on.
|
||||
schema: Comment creation data.
|
||||
use_case: CreateCommentUseCase dependency.
|
||||
current_user_id: Authenticated user ID.
|
||||
|
||||
Returns:
|
||||
CommentResponseSchema with created comment data.
|
||||
"""
|
||||
result = await use_case.execute(
|
||||
post_id=post_id,
|
||||
author_id=current_user_id,
|
||||
content=schema.content,
|
||||
parent_id=schema.parent_id,
|
||||
)
|
||||
return CommentResponseSchema(**result.__dict__)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/posts/{post_id}/comments",
|
||||
response_model=list[CommentResponseSchema],
|
||||
summary="List comments for a post",
|
||||
)
|
||||
async def list_comments(
|
||||
post_id: UUID,
|
||||
use_case: ListCommentsDep,
|
||||
) -> list[CommentResponseSchema]:
|
||||
"""Get all comments for a blog post.
|
||||
|
||||
Args:
|
||||
post_id: UUID of the post.
|
||||
use_case: ListCommentsUseCase dependency.
|
||||
|
||||
Returns:
|
||||
List of CommentResponseSchema for the post.
|
||||
"""
|
||||
results = await use_case.execute(post_id=post_id)
|
||||
return [CommentResponseSchema(**r.__dict__) for r in results]
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/comments/{comment_id}",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
summary="Delete a comment",
|
||||
)
|
||||
async def delete_comment(
|
||||
comment_id: UUID,
|
||||
use_case: DeleteCommentDep,
|
||||
current_user_id: CurrentUserDep,
|
||||
role: CurrentRoleDep,
|
||||
) -> None:
|
||||
"""Delete a comment.
|
||||
|
||||
Users can delete their own comments.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment to delete.
|
||||
use_case: DeleteCommentUseCase dependency.
|
||||
current_user_id: Authenticated user ID.
|
||||
role: Current user role.
|
||||
"""
|
||||
await use_case.execute(comment_id=comment_id, user_id=current_user_id)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/comments/{comment_id}/like",
|
||||
response_model=CommentLikeResponseSchema,
|
||||
summary="Toggle like on a comment",
|
||||
)
|
||||
async def toggle_comment_like(
|
||||
comment_id: UUID,
|
||||
use_case: ToggleCommentLikeDep,
|
||||
current_user_id: CurrentUserDep,
|
||||
) -> CommentLikeResponseSchema:
|
||||
"""Toggle like/unlike on a comment.
|
||||
|
||||
If the user already liked the comment, the like is removed (unlike).
|
||||
Otherwise, a new like is added.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment.
|
||||
use_case: ToggleCommentLikeUseCase dependency.
|
||||
current_user_id: Authenticated user ID.
|
||||
|
||||
Returns:
|
||||
CommentLikeResponseSchema with updated like_count.
|
||||
"""
|
||||
result = await use_case.execute(comment_id, current_user_id)
|
||||
return CommentLikeResponseSchema(id=result.id, like_count=result.like_count)
|
||||
@@ -4,6 +4,11 @@ This module re-exports all Pydantic schemas used for
|
||||
request/response validation in the API layer.
|
||||
"""
|
||||
|
||||
from app.presentation.schemas.comment import (
|
||||
CommentCreateSchema,
|
||||
CommentLikeResponseSchema,
|
||||
CommentResponseSchema,
|
||||
)
|
||||
from app.presentation.schemas.post import (
|
||||
PostBaseSchema,
|
||||
PostCreateSchema,
|
||||
@@ -22,4 +27,7 @@ __all__ = [
|
||||
"PostListResponseSchema",
|
||||
"PostSearchSchema",
|
||||
"PostPublishSchema",
|
||||
"CommentCreateSchema",
|
||||
"CommentResponseSchema",
|
||||
"CommentLikeResponseSchema",
|
||||
]
|
||||
|
||||
58
app/presentation/schemas/comment.py
Normal file
58
app/presentation/schemas/comment.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""Pydantic schemas for comments.
|
||||
|
||||
This module defines Pydantic models for comment request/response
|
||||
validation in the API layer.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class CommentCreateSchema(BaseModel):
|
||||
"""Schema for creating a comment.
|
||||
|
||||
Attributes:
|
||||
content: Comment text content (Markdown supported).
|
||||
parent_id: Optional parent comment ID for replies.
|
||||
"""
|
||||
|
||||
content: str = Field(..., min_length=1, max_length=5000, description="Comment content")
|
||||
parent_id: UUID | None = Field(default=None, description="Parent comment ID for replies")
|
||||
|
||||
|
||||
class CommentResponseSchema(BaseModel):
|
||||
"""Schema for comment response.
|
||||
|
||||
Attributes:
|
||||
id: Unique comment identifier.
|
||||
post_id: UUID of the parent post.
|
||||
author_id: Comment author identifier.
|
||||
content: Comment content text.
|
||||
parent_id: Optional parent comment ID.
|
||||
like_count: Number of likes on this comment.
|
||||
created_at: Creation timestamp.
|
||||
updated_at: Last update timestamp.
|
||||
"""
|
||||
|
||||
id: UUID
|
||||
post_id: UUID
|
||||
author_id: str
|
||||
content: str
|
||||
parent_id: UUID | None = None
|
||||
like_count: int = 0
|
||||
created_at: datetime | None = None
|
||||
updated_at: datetime | None = None
|
||||
|
||||
|
||||
class CommentLikeResponseSchema(BaseModel):
|
||||
"""Schema for comment like response.
|
||||
|
||||
Attributes:
|
||||
id: Comment identifier.
|
||||
like_count: Updated like count.
|
||||
"""
|
||||
|
||||
id: UUID
|
||||
like_count: int
|
||||
@@ -55,6 +55,9 @@
|
||||
<span class="post-card-meta-item" data-testid="like-count-{{ post.id }}">
|
||||
👍 {{ post.like_count }}
|
||||
</span>
|
||||
<span class="post-card-meta-item" data-testid="comment-count-{{ post.id }}">
|
||||
💬 {{ post.comment_count }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="post-card-content" data-testid="post-content-preview-{{ post.id }}">
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<article class="post-detail" data-testid="post-detail">
|
||||
<header class="post-detail-header" data-testid="post-detail-header">
|
||||
<h1 class="post-detail-title" data-testid="post-detail-title">{{ post.title }}</h1>
|
||||
|
||||
|
||||
<div class="post-detail-meta" data-testid="post-detail-meta">
|
||||
<span class="post-card-meta-item" data-testid="post-detail-author">
|
||||
<span class="avatar avatar-sm" data-testid="post-detail-author-avatar">{{ post.author_id[0]|upper }}</span>
|
||||
@@ -40,22 +40,25 @@
|
||||
👍 <span id="like-count">{{ post.like_count }}</span>
|
||||
</button>
|
||||
</span>
|
||||
<span class="post-card-meta-item" data-testid="post-detail-comment-count">
|
||||
💬 {{ post.comment_count }}
|
||||
</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
|
||||
<div class="post-detail-content markdown-body" data-testid="post-detail-content">
|
||||
{{ post.content|markdown|safe }}
|
||||
</div>
|
||||
|
||||
|
||||
<footer class="post-detail-footer" data-testid="post-detail-footer">
|
||||
<div class="post-detail-tags" data-testid="post-detail-tags">
|
||||
{% for tag in post.tags %}
|
||||
<span class="tag" data-testid="post-detail-tag-{{ loop.index }}">{{ tag }}</span>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
|
||||
<div class="divider" data-testid="post-detail-divider"></div>
|
||||
|
||||
|
||||
<div class="flex justify-between items-center" data-testid="post-detail-actions">
|
||||
<a href="/" class="btn" data-testid="btn-back-to-posts">
|
||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" style="margin-right: 0.5rem;">
|
||||
@@ -63,7 +66,7 @@
|
||||
</svg>
|
||||
{{ _('post.back_to_posts', current_locale) }}
|
||||
</a>
|
||||
|
||||
|
||||
{% if can_edit or can_delete %}
|
||||
<div class="flex gap-2" data-testid="post-detail-edit-actions">
|
||||
{% if can_edit %}
|
||||
@@ -89,41 +92,267 @@
|
||||
</div>
|
||||
</footer>
|
||||
</article>
|
||||
|
||||
<section class="comments-section" data-testid="comments-section">
|
||||
<div class="comments-header" data-testid="comments-header">
|
||||
<h2 class="comments-title" data-testid="comments-title">
|
||||
💬 {{ _('post.comments', current_locale) }}
|
||||
<span class="comments-count" data-testid="comments-count">({{ post.comment_count }})</span>
|
||||
</h2>
|
||||
{% if user %}
|
||||
<button id="btn-show-comment-form" class="btn btn-primary" data-testid="btn-show-comment-form">
|
||||
{{ _('post.write_comment', current_locale) }}
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% if user %}
|
||||
<div id="comment-form-wrapper" class="comment-form-wrapper" data-testid="comment-form-wrapper" style="display: none;">
|
||||
<form id="comment-form" class="comment-form" data-testid="form-create-comment" data-post-slug="{{ post.slug }}">
|
||||
<div class="form-group">
|
||||
<textarea id="comment-content" class="form-textarea" data-testid="input-comment-content"
|
||||
rows="4" placeholder="{{ _('post.comment_placeholder', current_locale) }}"
|
||||
required minlength="1" maxlength="5000"></textarea>
|
||||
<input type="hidden" id="comment-parent-id" name="parent_id" value="">
|
||||
<p class="form-help" data-testid="comment-form-help" id="reply-info" style="display: none;">
|
||||
{{ _('post.replying_to', current_locale) }}
|
||||
<button type="button" class="btn-cancel-reply" data-testid="btn-cancel-reply">{{ _('post.cancel_reply', current_locale) }}</button>
|
||||
</p>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn btn-primary" data-testid="submit-comment">
|
||||
{{ _('post.submit_comment', current_locale) }}
|
||||
</button>
|
||||
<button type="button" class="btn btn-cancel-comment" data-testid="btn-cancel-comment" style="display: none;">
|
||||
{{ _('post.cancel', current_locale) }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<div id="comment-error" class="comment-error" data-testid="comment-error" style="display: none;"></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% macro render_comment(comment, depth) %}
|
||||
<div class="comment{% if depth > 0 %} comment-reply{% endif %}" data-testid="comment-{{ comment.id }}" data-comment-id="{{ comment.id }}">
|
||||
<div class="comment-avatar" data-testid="comment-avatar-{{ comment.id }}">
|
||||
<span class="avatar avatar-sm">{{ comment.author_id[0]|upper }}</span>
|
||||
</div>
|
||||
<div class="comment-body" data-testid="comment-body-{{ comment.id }}">
|
||||
<div class="comment-meta" data-testid="comment-meta-{{ comment.id }}">
|
||||
<span class="comment-author" data-testid="comment-author-{{ comment.id }}">{{ comment.author_id }}</span>
|
||||
<span class="comment-date" data-testid="comment-date-{{ comment.id }}">
|
||||
{% if comment.created_at %}{{ comment.created_at.strftime('%B %d, %Y') }}{% endif %}
|
||||
</span>
|
||||
</div>
|
||||
<div class="comment-content" data-testid="comment-content-{{ comment.id }}">{{ comment.content }}</div>
|
||||
<div class="comment-actions" data-testid="comment-actions-{{ comment.id }}">
|
||||
{% if user %}
|
||||
<button class="btn-comment-reply btn btn-sm" data-testid="btn-comment-reply-{{ comment.id }}"
|
||||
data-comment-id="{{ comment.id }}" data-comment-author="{{ comment.author_id }}">
|
||||
{{ _('post.reply', current_locale) }}
|
||||
</button>
|
||||
{% endif %}
|
||||
<button class="btn-comment-like btn btn-sm" data-testid="btn-comment-like-{{ comment.id }}"
|
||||
data-comment-id="{{ comment.id }}">
|
||||
👍 <span class="comment-like-count" data-testid="comment-like-count-{{ comment.id }}">{{ comment.like_count }}</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{% set key = comment.id|string %}
|
||||
{% if key in reply_comments %}
|
||||
<div class="comment-replies" data-testid="comment-replies-{{ comment.id }}">
|
||||
{% for child in reply_comments[key] %}
|
||||
{{ render_comment(child, depth + 1) }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
<div class="comments-list" data-testid="comments-list">
|
||||
{% if top_level_comments %}
|
||||
{% for comment in top_level_comments %}
|
||||
{{ render_comment(comment, 0) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<div class="comments-empty" data-testid="comments-empty">
|
||||
<p>{{ _('post.no_comments', current_locale) }}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</section>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
<script data-testid="like-script">
|
||||
<script data-testid="comment-script">
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var likeButton = document.getElementById('like-button');
|
||||
if (!likeButton) return;
|
||||
if (likeButton) {
|
||||
likeButton.addEventListener('click', function() {
|
||||
var slug = this.getAttribute('data-post-slug');
|
||||
var countSpan = document.getElementById('like-count');
|
||||
|
||||
likeButton.addEventListener('click', function() {
|
||||
var slug = this.getAttribute('data-post-slug');
|
||||
var countSpan = document.getElementById('like-count');
|
||||
fetch('/web/posts/' + slug + '/like', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(function(response) {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/auth/dev-login';
|
||||
return;
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error('Like request failed');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(function(data) {
|
||||
if (data && data.like_count !== undefined) {
|
||||
countSpan.textContent = data.like_count;
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.error('Like error:', error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fetch('/web/posts/' + slug + '/like', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
var showFormBtn = document.getElementById('btn-show-comment-form');
|
||||
var formWrapper = document.getElementById('comment-form-wrapper');
|
||||
var cancelBtn = document.querySelector('.btn-cancel-comment');
|
||||
var commentForm = document.getElementById('comment-form');
|
||||
var commentContent = document.getElementById('comment-content');
|
||||
var commentParentId = document.getElementById('comment-parent-id');
|
||||
var replyInfo = document.getElementById('reply-info');
|
||||
var commentError = document.getElementById('comment-error');
|
||||
|
||||
function showCommentForm(parentId, authorName) {
|
||||
commentParentId.value = parentId || '';
|
||||
if (parentId && authorName) {
|
||||
replyInfo.style.display = 'block';
|
||||
replyInfo.innerHTML = '{{ _("post.replying_to", current_locale) }} <strong>' + authorName + '</strong> — <button type="button" class="btn-cancel-reply" id="btn-cancel-reply">{{ _("post.cancel_reply", current_locale) }}</button>';
|
||||
document.getElementById('btn-cancel-reply').addEventListener('click', function() {
|
||||
commentParentId.value = '';
|
||||
replyInfo.style.display = 'none';
|
||||
});
|
||||
} else {
|
||||
replyInfo.style.display = 'none';
|
||||
}
|
||||
formWrapper.style.display = 'block';
|
||||
if (showFormBtn) showFormBtn.style.display = 'none';
|
||||
if (cancelBtn) cancelBtn.style.display = 'inline-flex';
|
||||
commentContent.focus();
|
||||
commentError.style.display = 'none';
|
||||
}
|
||||
|
||||
function hideCommentForm() {
|
||||
formWrapper.style.display = 'none';
|
||||
if (showFormBtn) showFormBtn.style.display = 'inline-flex';
|
||||
if (cancelBtn) cancelBtn.style.display = 'none';
|
||||
commentContent.value = '';
|
||||
commentParentId.value = '';
|
||||
replyInfo.style.display = 'none';
|
||||
commentError.style.display = 'none';
|
||||
}
|
||||
|
||||
if (showFormBtn) {
|
||||
showFormBtn.addEventListener('click', function() {
|
||||
showCommentForm(null, null);
|
||||
});
|
||||
}
|
||||
|
||||
if (cancelBtn) {
|
||||
cancelBtn.addEventListener('click', hideCommentForm);
|
||||
}
|
||||
|
||||
if (commentForm) {
|
||||
commentForm.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
var content = commentContent.value.trim();
|
||||
if (!content) return;
|
||||
|
||||
var slug = this.getAttribute('data-post-slug');
|
||||
var parentId = commentParentId.value || null;
|
||||
|
||||
var payload = {content: content};
|
||||
if (parentId) {
|
||||
payload.parent_id = parentId;
|
||||
}
|
||||
})
|
||||
.then(function(response) {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/auth/dev-login';
|
||||
return;
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error('Like request failed');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(function(data) {
|
||||
if (data && data.like_count !== undefined) {
|
||||
countSpan.textContent = data.like_count;
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.error('Like error:', error);
|
||||
|
||||
fetch('/web/posts/' + slug + '/comments', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
})
|
||||
.then(function(response) {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/auth/dev-login';
|
||||
return;
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error('Comment creation failed');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(function(data) {
|
||||
if (data) {
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
commentError.textContent = '{{ _("post.comment_error", current_locale) }}';
|
||||
commentError.style.display = 'block';
|
||||
console.error('Comment error:', error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var replyButtons = document.querySelectorAll('.btn-comment-reply');
|
||||
replyButtons.forEach(function(btn) {
|
||||
btn.addEventListener('click', function() {
|
||||
var commentId = this.getAttribute('data-comment-id');
|
||||
var author = this.getAttribute('data-comment-author');
|
||||
showCommentForm(commentId, author);
|
||||
});
|
||||
});
|
||||
|
||||
var commentLikeButtons = document.querySelectorAll('.btn-comment-like');
|
||||
commentLikeButtons.forEach(function(btn) {
|
||||
btn.addEventListener('click', function() {
|
||||
var commentId = this.getAttribute('data-comment-id');
|
||||
var countSpan = this.querySelector('.comment-like-count');
|
||||
|
||||
fetch('/web/comments/' + commentId + '/like', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(function(response) {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/auth/dev-login';
|
||||
return;
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error('Comment like failed');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(function(data) {
|
||||
if (data && data.like_count !== undefined) {
|
||||
countSpan.textContent = data.like_count;
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.error('Comment like error:', error);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ integration with the application's use cases and domain layer.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from dishka.integrations.fastapi import DishkaRoute, FromDishka
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
@@ -19,11 +20,14 @@ from pygments.util import ClassNotFound
|
||||
|
||||
from app.application.dtos import CreatePostDTO, UpdatePostDTO
|
||||
from app.application.use_cases import (
|
||||
CreateCommentUseCase,
|
||||
CreatePostUseCase,
|
||||
DeletePostUseCase,
|
||||
GetPostUseCase,
|
||||
ListCommentsUseCase,
|
||||
ListPostsUseCase,
|
||||
PublishPostUseCase,
|
||||
ToggleCommentLikeUseCase,
|
||||
TogglePostLikeUseCase,
|
||||
UpdatePostUseCase,
|
||||
)
|
||||
@@ -32,6 +36,7 @@ from app.domain.exceptions import (
|
||||
NotFoundException,
|
||||
ValidationException,
|
||||
)
|
||||
from app.domain.repositories import CommentRepository
|
||||
from app.domain.roles import Role, get_effective_role
|
||||
from app.infrastructure.auth import TokenInfo
|
||||
from app.infrastructure.config.settings import settings
|
||||
@@ -177,6 +182,7 @@ async def home(
|
||||
request: Request,
|
||||
user: OptionalUserDep,
|
||||
list_use_case: FromDishka[ListPostsUseCase],
|
||||
comment_repo: FromDishka[CommentRepository],
|
||||
) -> HTMLResponse:
|
||||
"""Render the home page with list of posts.
|
||||
|
||||
@@ -184,10 +190,13 @@ async def home(
|
||||
request: The HTTP request object for template context.
|
||||
user: Current user from dependency.
|
||||
list_use_case: Use case for listing posts.
|
||||
comment_repo: Repository for fetching comment counts.
|
||||
|
||||
Returns:
|
||||
HTMLResponse with rendered posts list template.
|
||||
"""
|
||||
from dataclasses import replace
|
||||
|
||||
page_str = request.query_params.get("page", "1")
|
||||
page = max(1, int(page_str) if page_str.isdigit() else 1)
|
||||
offset = (page - 1) * _DEFAULT_PAGE_SIZE
|
||||
@@ -196,6 +205,10 @@ async def home(
|
||||
list_use_case, user, _DEFAULT_PAGE_SIZE, offset
|
||||
)
|
||||
|
||||
for i, post in enumerate(visible_posts):
|
||||
count = await comment_repo.count_by_post(post.id)
|
||||
visible_posts[i] = replace(post, comment_count=count)
|
||||
|
||||
locale = getattr(request.state, "locale", DEFAULT_LOCALE)
|
||||
context = _get_base_context(user, locale)
|
||||
return templates.TemplateResponse(
|
||||
@@ -217,6 +230,7 @@ async def list_posts(
|
||||
request: Request,
|
||||
user: OptionalUserDep,
|
||||
list_use_case: FromDishka[ListPostsUseCase],
|
||||
comment_repo: FromDishka[CommentRepository],
|
||||
) -> HTMLResponse:
|
||||
"""Render the posts listing page.
|
||||
|
||||
@@ -224,10 +238,13 @@ async def list_posts(
|
||||
request: The HTTP request object for template context.
|
||||
user: Current user from dependency.
|
||||
list_use_case: Use case for listing posts.
|
||||
comment_repo: Repository for fetching comment counts.
|
||||
|
||||
Returns:
|
||||
HTMLResponse with rendered posts list template.
|
||||
"""
|
||||
from dataclasses import replace
|
||||
|
||||
page_str = request.query_params.get("page", "1")
|
||||
page = max(1, int(page_str) if page_str.isdigit() else 1)
|
||||
offset = (page - 1) * _DEFAULT_PAGE_SIZE
|
||||
@@ -236,6 +253,10 @@ async def list_posts(
|
||||
list_use_case, user, _DEFAULT_PAGE_SIZE, offset
|
||||
)
|
||||
|
||||
for i, post in enumerate(visible_posts):
|
||||
count = await comment_repo.count_by_post(post.id)
|
||||
visible_posts[i] = replace(post, comment_count=count)
|
||||
|
||||
locale = getattr(request.state, "locale", DEFAULT_LOCALE)
|
||||
context = _get_base_context(user, locale)
|
||||
return templates.TemplateResponse(
|
||||
@@ -339,14 +360,18 @@ async def post_detail(
|
||||
post_slug: str,
|
||||
user: OptionalUserDep,
|
||||
get_use_case: FromDishka[GetPostUseCase],
|
||||
list_comments_use_case: FromDishka[ListCommentsUseCase],
|
||||
comment_repo: FromDishka[CommentRepository],
|
||||
) -> HTMLResponse:
|
||||
"""Render a single post detail page.
|
||||
"""Render a single post detail page with comments.
|
||||
|
||||
Args:
|
||||
request: The HTTP request object for template context.
|
||||
post_slug: The URL-friendly slug of the post to display.
|
||||
user: Current user from dependency.
|
||||
get_use_case: Use case for retrieving posts.
|
||||
list_comments_use_case: Use case for listing comments.
|
||||
comment_repo: Repository for fetching comment count.
|
||||
|
||||
Returns:
|
||||
HTMLResponse with rendered post detail template.
|
||||
@@ -354,6 +379,8 @@ async def post_detail(
|
||||
Raises:
|
||||
HTTPException: If post not found or not visible to user.
|
||||
"""
|
||||
from dataclasses import replace
|
||||
|
||||
try:
|
||||
post = await get_use_case.by_slug(post_slug)
|
||||
except NotFoundException:
|
||||
@@ -362,6 +389,19 @@ async def post_detail(
|
||||
if not post.published and not can_see_draft(user, post.author_id):
|
||||
raise HTTPException(status_code=404, detail="Post not found")
|
||||
|
||||
comments = await list_comments_use_case.execute(post.id)
|
||||
comment_count = await comment_repo.count_by_post(post.id)
|
||||
post = replace(post, comment_count=comment_count)
|
||||
|
||||
children: dict[str, list[Any]] = {}
|
||||
for c in comments:
|
||||
pid = str(c.parent_id) if c.parent_id else ""
|
||||
if pid not in children:
|
||||
children[pid] = []
|
||||
children[pid].append(c)
|
||||
|
||||
top_level = children.pop("", [])
|
||||
|
||||
locale = getattr(request.state, "locale", DEFAULT_LOCALE)
|
||||
context = _get_base_context(user, locale)
|
||||
|
||||
@@ -371,6 +411,8 @@ async def post_detail(
|
||||
{
|
||||
**context,
|
||||
"post": post,
|
||||
"top_level_comments": top_level,
|
||||
"reply_comments": children,
|
||||
"active_page": "posts",
|
||||
"can_edit": can_edit_post(user, post.author_id),
|
||||
"can_delete": can_delete_post(user, post.author_id),
|
||||
@@ -378,6 +420,89 @@ async def post_detail(
|
||||
)
|
||||
|
||||
|
||||
@router.post("/posts/{post_slug}/comments")
|
||||
async def create_comment_web(
|
||||
request: Request,
|
||||
post_slug: str,
|
||||
user: OptionalUserDep,
|
||||
get_use_case: FromDishka[GetPostUseCase],
|
||||
create_use_case: FromDishka[CreateCommentUseCase],
|
||||
) -> dict[str, object]:
|
||||
"""Create a comment on a post via web UI.
|
||||
|
||||
Args:
|
||||
request: The HTTP request object with JSON body.
|
||||
post_slug: The URL-friendly slug of the post.
|
||||
user: Current user from cookie or None.
|
||||
get_use_case: Use case for retrieving post.
|
||||
create_use_case: Use case for creating comments.
|
||||
|
||||
Returns:
|
||||
JSON dict with created comment data.
|
||||
|
||||
Raises:
|
||||
HTTPException: If user not authenticated or post not found.
|
||||
"""
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
try:
|
||||
post = await get_use_case.by_slug(post_slug)
|
||||
except NotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Post not found") from None
|
||||
|
||||
body = await request.json()
|
||||
content = str(body.get("content", "")).strip()
|
||||
parent_id_str = body.get("parent_id")
|
||||
|
||||
parent_id: UUID | None = None
|
||||
if parent_id_str:
|
||||
parent_id = UUID(parent_id_str)
|
||||
|
||||
result = await create_use_case.execute(
|
||||
post_id=post.id,
|
||||
author_id=user.user_id,
|
||||
content=content,
|
||||
parent_id=parent_id,
|
||||
)
|
||||
|
||||
return {
|
||||
"id": str(result.id),
|
||||
"post_id": str(result.post_id),
|
||||
"author_id": result.author_id,
|
||||
"content": result.content,
|
||||
"parent_id": str(result.parent_id) if result.parent_id else None,
|
||||
"like_count": result.like_count,
|
||||
"created_at": result.created_at.isoformat() if result.created_at else None,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/comments/{comment_id}/like")
|
||||
async def toggle_comment_like_web(
|
||||
comment_id: UUID,
|
||||
user: OptionalUserDep,
|
||||
toggle_use_case: FromDishka[ToggleCommentLikeUseCase],
|
||||
) -> dict[str, object]:
|
||||
"""Toggle like on a comment via web UI.
|
||||
|
||||
Args:
|
||||
comment_id: UUID of the comment.
|
||||
user: Current user from cookie or None.
|
||||
toggle_use_case: Use case for toggling comment likes.
|
||||
|
||||
Returns:
|
||||
JSON dict with updated like_count.
|
||||
|
||||
Raises:
|
||||
HTTPException: If user not authenticated.
|
||||
"""
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
result = await toggle_use_case.execute(comment_id, user.user_id)
|
||||
return {"like_count": result.like_count, "id": str(result.id)}
|
||||
|
||||
|
||||
@router.get("/posts/{post_slug}/edit", response_class=HTMLResponse)
|
||||
async def edit_post_form(
|
||||
request: Request,
|
||||
|
||||
Reference in New Issue
Block a user