public Delivery CreateDelivery(decimal deliveryPrice, long cardNumber, long userId, string description,
                                       List <ShoppingCart> shoppingCart, string deliveryAddress = null)
        {
            CreditCard card = CreditCardDao.FindByNumber(cardNumber);

            if (CreditCardDao.FindByUserId(userId)
                .Contains(card))
            {
                Delivery delivery = new Delivery
                {
                    deliveryDate    = DateTime.Now,
                    deliveryPrice   = deliveryPrice,
                    deliveryAddress = deliveryAddress ?? UserProfileDao.Find(userId).address,
                    cardId          = card.cardId,
                    userId          = userId,
                    description     = description
                };

                DeliveryDao.Create(delivery);

                DeliveryLine deliveryLine;
                foreach (ShoppingCart item in shoppingCart)
                {
                    deliveryLine = new DeliveryLine();

                    if (item.Product.productQuantity - item.Amount >= 0)
                    {
                        DecreaseProductStock(item.Product, item.Amount);
                        deliveryLine.deliveryLineAmount = item.Amount;
                    }
                    else
                    {
                        throw new StockEmptyException(item.Product.productId, item.Product.productName);
                    }

                    deliveryLine.deliveryLinePrice = item.Product.productPrice;
                    deliveryLine.productId         = item.Product.productId;
                    deliveryLine.deliveryId        = delivery.deliveryId;

                    DeliveryLineDao.Create(deliveryLine);
                }

                return(delivery);
            }
            else
            {
                throw new UnmatchingUserAndCardException(userId, cardNumber);
            }
        }
示例#2
0
 public void Setup()
 {
     _tested = new CreditCardDao();
 }