Last active
May 27, 2020 22:40
-
-
Save mariobittencourt/2e2978726affcfad217f6de6db186a53 to your computer and use it in GitHub Desktop.
validation of business invariants for payment creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from typing import List | |
from src.domain.models.domain_event import DomainEvent | |
from src.domain.models.payment_created import PaymentCreated | |
class Payment: | |
def __init__(self, payment_id: str): | |
self._payment_id = payment_id | |
self._events = [] | |
@classmethod | |
def create(cls, payment_id: str, amount_due: float) -> Payment: | |
# validation of business invariant | |
if amount_due <= 0: | |
raise Exception('Amount due should be positive') | |
payment = cls(payment_id) | |
payment.record_that(PaymentCreated(payment_id, amount_due)) | |
return payment | |
def apply_payment_created(self, event: PaymentCreated): | |
self._status = 'PENDING_PROCESSING' | |
self._amount_due = event.amount_due | |
# the existing implementation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment