/// <summary>
        /// Бизнес-логика сохранения транзакции в фонд
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="sourceFund"></param>
        /// <param name="receiverFund"></param>
        /// <returns>Кортеж sourse transaction Id, receiver transaction Id</returns>
        public async Task <(string, string)> SaveTransactionToFundAsync(
            Transaction transaction,
            Fund sourceFund,
            Fund receiverFund)
        {
            if (sourceFund.Id == null)
            {
                throw new Exception("Фонд-источник загружен некорректно.");
            }

            (string, string)result = (null, null);

            var transactionRoute = new TransactionRoute();

            transaction.Name = transaction.TransactionType.Name;

            if (sourceFund.Id != receiverFund.Id && receiverFund != null)
            {
                if (receiverFund.Id == null)
                {
                    throw new Exception("Фонд-получатель загружен некорректно.");
                }

                if (transaction.GetReverseType() == null)
                {
                    throw new Exception("Выбран тип транзакции без обратной транзакции.");
                }


                var receiverTransaction = (Transaction)transaction.Clone();

                receiverTransaction.TransactionType = transaction.GetReverseType();

                receiverTransaction.Name = receiverTransaction.TransactionType.Name;

                transactionRoute.ReceiverFund = receiverFund;

                transactionRoute.ReceiverTransaction = receiverTransaction;

                receiverFund.AddTransaction(receiverTransaction);

                result.Item2 = (await _transactionService.SaveAsync(receiverTransaction));

                await _fundService.UpdateAsync(receiverFund);
            }

            transactionRoute.SourceFund = sourceFund;

            transactionRoute.SourceTransaction = transaction;

            sourceFund.AddTransaction(transaction);

            result.Item1 = (await _transactionService.SaveAsync(transaction));

            await _fundService.UpdateAsync(sourceFund);

            await _transactionRouteService.SaveAsync(transactionRoute);

            return(result);
        }
Exemple #2
0
 public static TransactionRouteDTO ToDto(this TransactionRoute item) => new TransactionRouteDTO()
 {
     Id = item.Id,
     SourceTransactionId   = item.SourceTransaction.Id,
     ReceiverTransactionId = item.ReceiverTransaction?.Id,
     SourceFundId          = item.SourceFund.Id,
     ReceiverFundId        = item.ReceiverFund?.Id
 };
 public async Task <bool> UpdateAsync(TransactionRoute entity) =>
 await _transactionRouteRepository.UpdateAsync(entity);
 public async Task <string> SaveAsync(TransactionRoute entity) =>
 await _transactionRouteRepository.AddAsync(entity);