"""Domain entity for Blog Post.""" from dataclasses import dataclass, field from typing import Any from app.domain.entities.base import BaseEntity from app.domain.value_objects.content import Content from app.domain.value_objects.slug import Slug from app.domain.value_objects.title import Title @dataclass(kw_only=True) class Post(BaseEntity): """Blog post domain entity.""" title: Title content: Content slug: Slug author_id: str published: bool = False tags: list[str] = field(default_factory=list) def publish(self) -> None: """Publish the post.""" self.published = True self.touch() def unpublish(self) -> None: """Unpublish the post.""" self.published = False self.touch() def update_content(self, content: Content) -> None: """Update post content.""" self.content = content self.touch() def update_title(self, title: Title) -> None: """Update post title and regenerate slug.""" self.title = title self.slug = Slug.from_title(title.value) self.touch() def add_tag(self, tag: str) -> None: """Add a tag to the post.""" if tag not in self.tags: self.tags.append(tag) self.touch() def remove_tag(self, tag: str) -> None: """Remove a tag from the post.""" if tag in self.tags: self.tags.remove(tag) self.touch() def to_dict(self) -> dict[str, Any]: """Convert entity to dictionary.""" return { "id": str(self.id), "title": self.title.value, "content": self.content.value, "slug": self.slug.value, "author_id": self.author_id, "published": self.published, "tags": self.tags.copy(), "created_at": self.created_at.isoformat(), "updated_at": self.updated_at.isoformat(), } @classmethod def create( cls, title_str: str, content_str: str, author_id: str, tags: list[str] | None = None, ) -> "Post": """Factory method to create a new post.""" title = Title(title_str) content = Content(content_str) slug = Slug.from_title(title_str) return cls( title=title, content=content, slug=slug, author_id=author_id, tags=tags or [], )