feat: add SetTitle to ClassifiedAd with domain events, extract EnsureValidState
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from decimal import Decimal
|
||||
|
||||
from domain.framework.entity import Entity
|
||||
from domain.marketplace.classified_ad_id import ClassifiedAdId
|
||||
from domain.marketplace.classified_ad_state import ClassifiedAdState
|
||||
from domain.marketplace.classified_ad_text import ClassifiedAdText
|
||||
from domain.marketplace.classified_ad_title import ClassifiedAdTitle
|
||||
from domain.marketplace.domain_event import (
|
||||
ClassifiedAdTitleChanged,
|
||||
DomainEvent,
|
||||
)
|
||||
from domain.marketplace.invalid_entity_state_exception import (
|
||||
InvalidEntityStateException,
|
||||
)
|
||||
@@ -13,15 +20,16 @@ from domain.marketplace.user_id import UserId
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ClassifiedAd:
|
||||
class ClassifiedAd(Entity):
|
||||
id: ClassifiedAdId
|
||||
_ownerId: UserId
|
||||
title: ClassifiedAdTitle
|
||||
text: ClassifiedAdText
|
||||
price: Price = field(default_factory=lambda: Price(amount=Decimal("0.00")))
|
||||
state: ClassifiedAdState | None = None
|
||||
_events: tuple[DomainEvent, ...] = ()
|
||||
|
||||
def request_to_publish(self):
|
||||
def _ensure_valid_state(self):
|
||||
errors = []
|
||||
if not self.title.title:
|
||||
errors.append("title")
|
||||
@@ -35,6 +43,9 @@ class ClassifiedAd:
|
||||
f"Cannot publish classified ad with missing or invalid: {', '.join(errors)}"
|
||||
)
|
||||
|
||||
def request_to_publish(self):
|
||||
self._ensure_valid_state()
|
||||
|
||||
return ClassifiedAd(
|
||||
id=self.id,
|
||||
_ownerId=self._ownerId,
|
||||
@@ -42,4 +53,20 @@ class ClassifiedAd:
|
||||
text=self.text,
|
||||
price=self.price,
|
||||
state=ClassifiedAdState.PendingReview,
|
||||
_events=self._events,
|
||||
)
|
||||
|
||||
def set_title(self, title: ClassifiedAdTitle) -> ClassifiedAd:
|
||||
updated = ClassifiedAd(
|
||||
id=self.id,
|
||||
_ownerId=self._ownerId,
|
||||
title=title,
|
||||
text=self.text,
|
||||
price=self.price,
|
||||
state=self.state,
|
||||
_events=self._events,
|
||||
)
|
||||
updated._ensure_valid_state()
|
||||
return updated.raise_event(
|
||||
ClassifiedAdTitleChanged(id=self.id.value, title=title.title)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user