Last active
August 29, 2015 13:58
-
-
Save j5ik2o/10014034 to your computer and use it in GitHub Desktop.
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
case class Cart | |
(id: CartId, | |
status: StatusType.Value, | |
customerId: CustomerId, | |
cartItems: List[CartItem]) extends Entity[CartId] { | |
// ... | |
def addCartItem(cartItem: CartItem): Cart = { | |
require(cartItem.quantity > 0) | |
cartItems.find(_.itemId == cartItem.itemId).map { | |
currentItem => | |
val newCartItem = currentItem.addQuantity(cartItem.quantity). | |
ensuring(_.quantity > 0) | |
copy(cartItems = newCartItem :: | |
cartItems.filterNot(_.itemId == cartItem.itemId)) | |
}.getOrElse { | |
copy(cartItems = cartItem :: cartItems) | |
} | |
} | |
} |
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
case class Order | |
(id: OrderId, | |
status: StatusType.Value, | |
orderStatus: OrderStatus.Value, | |
orderDate: DateTime, | |
customerId: CustomerId, | |
customerName: String, | |
shippingAddress: PostalAddress, | |
shippingContact: Contact, | |
orderItems: List[OrderItem]) | |
extends Entity[OrderId] { | |
// ... | |
def addOrderItem(orderItem: OrderItem): Order = | |
copy(orderItems = orderItem :: orderItems) | |
// ... | |
} | |
object Order { | |
def clearUp(cart: Cart) | |
(implicit is: IdentifierService, | |
cr: CustomerRepository, | |
ctx: EntityIOContext): Try[Order] = Try { | |
val customer = cart.customer.get | |
Order( | |
OrderId(is.generate), | |
StatusType.Enabled, | |
OrderStatus.Pending, | |
DateTime.now, | |
customer.id, | |
customer.name, | |
customer.profile.postalAddress, | |
customer.profile.contact, | |
cart.cartItems.map { | |
cartItem => | |
OrderItem.fromCartItem(OrderItemId(is.generate), cartItem) | |
} | |
) | |
} | |
} |
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
trait Repository[ID <: Identifier[_], E <: Entity[ID]] { | |
type This <: Repository[ID, E] | |
type Ctx = EntityIOContext | |
def existById(id: ID)(implicit ctx: Ctx): Try[Boolean] | |
def resolve(id: ID)(implicit ctx: Ctx): Try[E] | |
def store(entity: E)(implicit ctx: Ctx): Try[(This, E)] | |
def deleteById(id: ID)(implicit ctx: Ctx): Try[(This, E)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment