Last active
August 29, 2015 13:58
-
-
Save j5ik2o/9967068 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] |
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] { | |
val sizeOfCartItems = cartItems.size | |
val quantityOfCartItems = cartItems.foldLeft(0)(_ + _.quantity) | |
def containsItemId(itemId: ItemId): Boolean = | |
cartItems.exists { | |
_.itemId == itemId | |
} | |
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) | |
} | |
} | |
def addCartItem | |
(cartItemId: CartItemId, | |
item: Item, | |
quantity: Int, | |
isInStock: Boolean): Cart = | |
addCartItem(CartItem( | |
cartItemId, StatusType.Enabled, | |
cartItems.size + 1, item.id, | |
quantity, isInStock)) | |
def removeCartItemByItemId(itemId: ItemId): Cart = | |
cartItems.find(_.itemId == itemId).map { | |
e => | |
copy(cartItems = cartItems.filterNot(_.itemId == itemId)) | |
}.getOrElse(this) | |
def incrementQuantityByItemId(itemId: ItemId): Cart = | |
cartItems.find(_.itemId == itemId).map { | |
cartItem => | |
val newCartItem = cartItem.incrementQuantity. | |
ensuring(_.quantity > 0) | |
copy(cartItems = newCartItem :: | |
cartItems.filterNot(_.itemId == itemId)) | |
}.getOrElse(this) | |
def updateQuantityByItemId(itemId: ItemId, quantity: Int): Cart = { | |
require(quantity > 0) | |
cartItems.find(_.itemId == itemId).map { | |
cartItem => | |
val newCartItem = cartItem.withQuantity(quantity). | |
ensuring(_.quantity > 0) | |
copy(cartItems = newCartItem :: | |
cartItems.filterNot(_.itemId == itemId)) | |
}.getOrElse(this) | |
} | |
} |
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 CartItem | |
(id: CartItemId, | |
status: StatusType.Value, | |
no: Int, | |
itemId: ItemId, | |
quantity: Int, | |
inStock: Boolean) |
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 CartItem | |
(id: CartItemId, | |
status: StatusType.Value, | |
no: Int, | |
itemId: ItemId, | |
quantity: Int, | |
inStock: Boolean) { | |
def incrementQuantity: CartItem = addQuantity(1) | |
def addQuantity(otherQuantity: Int): CartItem = | |
copy(quantity = quantity + otherQuantity) | |
def withQuantity(quantity: Int): CartItem = | |
copy(quantity = quantity) | |
} |
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 Contact(email: String, phone: String) |
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 Customer | |
(id: CustomerId, | |
status: StatusType.Value, | |
name: String, | |
sexType: SexType.Value, | |
zipCode: ZipCode, | |
pref: Pref.Value, | |
cityName: String, | |
addressName: String, | |
buildingName: Option[String] = None, | |
loginName: String, | |
password: String) | |
extends Entity[CustomerId] |
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 Customer | |
(id: CustomerId, | |
status: StatusType.Value, | |
name: String, | |
sexType: SexType.Value, | |
profile: CustomerProfile, | |
config: CustomerConfig) | |
extends Entity[CustomerId] |
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 CustomerConfig | |
(loginName: String, | |
password: String) |
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 CustomerProfile | |
(postalAddress: PostalAddress, | |
contact: Contact) |
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 Entity[ID <: Identifier[_]] { | |
val id: ID | |
override def equals(obj: Any): Boolean = this match { | |
case that: Entity[_] => id == that.id | |
case _ => false | |
} | |
override def hashCode: Int = 31 * id.## | |
} |
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 Item | |
(id: ItemId, | |
status: StatusType.Value, | |
name: String, | |
description: Option[String] = None, | |
price: BigDecimal) | |
extends Entity[ItemId] |
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 PostalAddress | |
(zipCode: ZipCode, | |
pref: Pref.Value, | |
cityName: String, | |
addressName: String, | |
buildingName: Option[String] = None) |
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
object Pref extends Enumeration { | |
val 北海道,青森県,岩手県,宮城県,秋田県,山形県,福島県, | |
茨城県,栃木県,群馬県,埼玉県,千葉県,東京都,神奈川県, | |
新潟県,富山県,石川県,福井県,山梨県,長野県,岐阜県, | |
静岡県,愛知県,三重県,滋賀県,京都府,大阪府,兵庫県, | |
奈良県,和歌山県,鳥取県,島根県,岡山県,広島県,山口県, | |
徳島県,香川県,愛媛県,高知県,福岡県,佐賀県,長崎県, | |
熊本県,大分県,宮崎県,鹿児島県,沖縄県 = Value | |
} |
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 ZipCode(areaCode: String, cityCode: String) { | |
def asString = s"$areaCode-$cityCode" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment