public async Task <ProductDTO> BuyProduct(Guid userId, TypeProduct typeProduct)
        {
            ProductDTO product        = null;
            var        creatorProduct = await _vendingMachineService.GetInfoProductAsync(typeProduct);

            var depositCustomer = await _userDepositRepository.GetAmountDepositAsync(userId);

            if (creatorProduct.Product.Price > depositCustomer)
            {
                throw new ApplicationException("The amount of the deposit is less than the value of the goods");
            }

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    // we reduce the buyer's deposit by the amount of the goods
                    await _userDepositRepository.RetrieveDepositAsync(userId, creatorProduct.Product.Price);

                    // release the goods from the car
                    product = await _vendingMachineService.CreateProductAsync(typeProduct);

                    var customerProduct = new DAL.Entities.CustomerProduct
                    {
                        CustomerId = userId,
                        Name       = product.Name,
                        Price      = product.Price
                    };

                    // save the issued goods to the user history
                    await _customerProductRepository.Create(customerProduct);

                    scope.Complete();
                }
                catch (System.Exception)
                {
                    // TODO: Handle failure
                    throw new ApplicationException("The problem of buying product!");
                }
            }

            return(product);
        }
Esempio n. 2
0
        public async Task <ProductDTO> BuyProduct(Guid userId, TypeProduct typeProduct)
        {
            ProductDTO product        = null;
            var        creatorProduct = await _vendingMachineService.GetInfoProductAsync(typeProduct);

            var depositCustomer = await _userDepositRepository.GetAmountDepositAsync(userId);

            if (creatorProduct.Product.Price > depositCustomer)
            {
                throw new ApplicationException("The amount of the deposit is less than the value of the goods");
            }

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    // уменьшаем депозит покупателя на сумму товара
                    await _userDepositRepository.RetrieveDepositAsync(userId, creatorProduct.Product.Price);

                    // выдать товар из машины
                    product = await _vendingMachineService.CreateProductAsync(typeProduct);

                    var customerProduct = new DAL.Entities.CustomerProduct
                    {
                        CustomerId = userId,
                        Name       = product.Name,
                        Price      = product.Price
                    };

                    // сохранить в историю юзера выданный товар
                    await _customerProductRepository.Create(customerProduct);

                    scope.Complete();
                }
                catch (System.Exception)
                {
                    // TODO: Handle failure
                    throw new ApplicationException("The problem of buying product!");
                }
            }

            return(product);
        }