46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from dataclasses import dataclass, field
|
|
from decimal import Decimal
|
|
|
|
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.invalid_entity_state_exception import (
|
|
InvalidEntityStateException,
|
|
)
|
|
from domain.marketplace.price import Price
|
|
from domain.marketplace.user_id import UserId
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ClassifiedAd:
|
|
id: ClassifiedAdId
|
|
_ownerId: UserId
|
|
title: ClassifiedAdTitle
|
|
text: ClassifiedAdText
|
|
price: Price = field(default_factory=lambda: Price(amount=Decimal("0.00")))
|
|
state: ClassifiedAdState | None = None
|
|
|
|
def request_to_publish(self):
|
|
errors = []
|
|
if not self.title.title:
|
|
errors.append("title")
|
|
if not self.text.text:
|
|
errors.append("text")
|
|
if self.price.amount <= 0:
|
|
errors.append("price")
|
|
|
|
if errors:
|
|
raise InvalidEntityStateException(
|
|
f"Cannot publish classified ad with missing or invalid: {', '.join(errors)}"
|
|
)
|
|
|
|
return ClassifiedAd(
|
|
id=self.id,
|
|
_ownerId=self._ownerId,
|
|
title=self.title,
|
|
text=self.text,
|
|
price=self.price,
|
|
state=ClassifiedAdState.PendingReview,
|
|
)
|