Skip to content

Instantly share code, notes, and snippets.

@erikrozendaal
Created February 5, 2011 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikrozendaal/812405 to your computer and use it in GitHub Desktop.
Save erikrozendaal/812405 to your computer and use it in GitHub Desktop.
Code for immutable domain blog part 3
case class Invoice (
uncommittedEvents: List[InvoiceEvent],
id: Int,
recipient_? : Boolean = false,
nextItemId: Int = 1,
items: Map[Int, InvoiceItem] = Map.empty,
sent_? : Boolean = false,
paid_? : Boolean = false,
dueDate: Option[LocalDate] = None)
extends AggregateRoot[Invoice, InvoiceEvent] {
// [... code omitted ...]
}
def send: Invoice = {
require(!sent_?, "invoice already sent")
require(readyToSend_?, "recipient and items must be specified before sending")
val now = new LocalDate
applyEvent(InvoiceSent(id, sentDate = now, dueDate = now.plusDays(14)))
}
def applyEvent = {
// [... code omitted ...]
case event: InvoiceSent =>
copy(event :: uncommittedEvents, sent_? = true, dueDate = Some(event.dueDate))
// [... code omitted ...]
}
"ready to send invoice" should {
"generate invoice sent event" in {
val invoice = Invoice.create(1)
.changeRecipient(Some("Erik"))
.addItem("Food", 2.95)
.send
invoice.uncommittedEvents must contain(
InvoiceSent(1,
sentDate = new LocalDate(2011, 1, 29),
dueDate = new LocalDate(2011, 2, 12)))
}
}
def markCommitted = copy(uncommittedEvents = Nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment