Example #1
0
        public Order(Psorder dao, ILocation location, IUser user)
        {
            Dao = dao ?? throw new ArgumentNullException(paramName: nameof(dao));

            if (location.Dao.LocationId != dao.Location.LocationId)
            {
                throw new InvalidOperationException("location inconsistent between dto and dao.");
            }
            Location = location;
            if (user.Dao.UserId != dao.User.UserId)
            {
                throw new InvalidOperationException("user inconsistent between dto and dao.");
            }
            User = user;
            Time = Dao.OrderTime;
            ID   = Dao.OrderId;

            IDictionary <decimal, int> pizzasByPrice = Dao.PsorderPart.ToDictionary(x => x.Price, x => x.Qty);

            ValidatePizzas(pizzasByPrice);
            PizzasByPrice = pizzasByPrice.Where(p => p.Value != 0).ToImmutableDictionary(p => p.Key, p => p.Value);

            TotalValueUsd = pizzasByPrice.Sum(x => x.Key * x.Value);
            if (TotalValueUsd > MaxTotalValueUsd)
            {
                throw new ArgumentException(message: $"order should not exceed ${MaxTotalValueUsd} in total value.",
                                            paramName: nameof(pizzasByPrice));
            }
        }
Example #2
0
        public Order(IDictionary <decimal, int> pizzasByPrice, ILocation location, IUser user) : this(pizzasByPrice)
        {
            Location = location ?? throw new ArgumentNullException(paramName: nameof(location));
            User     = user ?? throw new ArgumentNullException(paramName: nameof(user));

            Dao = new Psorder
            {
                Location  = Location.Dao,
                User      = User.Dao,
                OrderTime = Time
            };
            foreach (KeyValuePair <decimal, int> entry in PizzasByPrice)
            {
                Dao.PsorderPart.Add(new PsorderPart()
                {
                    Price = entry.Key, Qty = entry.Value
                });
            }
            Dao.Location  = Location.Dao;
            Dao.User      = User.Dao;
            Dao.OrderTime = Time;
            PSDBContextProvider.Current.UpdateAndSave(Dao);
            if (Dao.OrderId == default)
            {
                throw new InvalidOperationException("could not register order.");
            }
            ID = Dao.OrderId;
        }