示例#1
0
        public void PlaceOrder(Id <Contact> contact, IDictionary <Id <Coffee>, OrderQuantity> details)
        {
            if (NotifiedContacts.All(c => c != contact))
            {
                throw new ContactNotPartOfRoastingEventException(contact);
            }

            if (Orders.Any(o => o.Contact == contact))
            {
                throw new ContactAlreadyPlacedOrderException(contact);
            }

            var invalidCoffees = new List <Id <Coffee> >();
            var totalCost      = 0m;

            foreach (var detail in details)
            {
                var(coffee, qty) = detail;
                var offeredCoffee = OfferedCoffees.Values.FirstOrDefault(c => c.Id == coffee);

                if (offeredCoffee == null)
                {
                    invalidCoffees.Add(coffee);
                    continue;
                }

                var subTotal = offeredCoffee.Price * qty;
                totalCost += subTotal;
            }

            if (invalidCoffees.Any())
            {
                throw new OrderContainedInvalidCoffeesException(invalidCoffees);
            }

            var order = CreateOrder(contact, details, totalCost);

            Orders = Orders.Concat(new [] { order });
        }
示例#2
0
 public void AddNotifiedContact(Id <Contact> contact)
 {
     NotifiedContacts = NotifiedContacts.Concat(new[] { contact }).Distinct();
 }