Exemple #1
0
        public ResultModel AddTransactionsByFile(IFormFile transactionsFile)
        {
            var resultModel = new ResultModel();
            IEnumerable <Transaction> transactions;
            var fileConverter = FileConverterToTransactionsListFactory.GetFileCOnverterToTransactionsList(Path.GetExtension(transactionsFile.FileName));

            try
            {
                transactions = fileConverter.Convert(transactionsFile);
            }
            catch (Exception ex)
            {
                logger.LogError(Constants.PARSING_FILE_ERROR, ex);
                resultModel.IsError = true;
                resultModel.Errors.Add(Constants.PARSING_FILE_ERROR);
                return(resultModel);
            }

            foreach (var item in transactions)
            {
                if (!item.IsValid())
                {
                    if (!resultModel.IsError)
                    {
                        resultModel.IsError = true;
                    }

                    resultModel.Errors.Add($"{Constants.TRANSACTION_IS_INVALID} Transaction id: {item.ID}");
                }
            }

            return(resultModel.IsError ? resultModel : transactionRepository.AddRange(transactions));
        }
        /// <summary>
        /// Совершает покупку
        /// </summary>
        /// <returns></returns>
        public async Task <bool> BuyAsync(List <OperationData> operationData)
        {
            if (operationData.Any(o => o.SellVolume == 0 || o.BuyVolume == 0))
            {
                return(false);
            }

            try
            {
                foreach (var item in operationData)
                {
                    var reservedTransaction    = _transactionRepository.GetList(item.UserId, item.CurrencyId, TransactionType.Reserve);
                    var reservedTransactionSum = await reservedTransaction.SumAsync(s => s.Volume) * -1;

                    if (reservedTransactionSum >= item.SellVolume)
                    {
                        var transactions = new List <Transaction> {
                            new Transaction {
                                Volume          = item.SellVolume,
                                CreatedAt       = DateTime.UtcNow,
                                CurrencyId      = item.CurrencyId.ToUpper(),
                                TransactionType = TransactionType.Reserve,
                                UserId          = item.UserId
                            },
                            new Transaction {
                                Volume          = item.SellVolume * -1,
                                CreatedAt       = DateTime.UtcNow,
                                CurrencyId      = item.CurrencyId.ToUpper(),
                                TransactionType = TransactionType.Ask,
                                UserId          = item.UserId
                            },
                            new Transaction {
                                Volume          = item.BuyVolume,
                                CreatedAt       = DateTime.UtcNow,
                                CurrencyId      = item.BuyCurrencyId.ToUpper(),
                                TransactionType = TransactionType.Bid,
                                UserId          = item.UserId
                            }
                        };

                        _transactionRepository.AddRange(transactions);
                    }
                    else
                    {
                        return(false);
                    }
                }

                await _unitOfWork.SaveChangesAsync();

                await ReCreateReserves(operationData);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemple #3
0
        private void CallCsvReader(IFormFile file, ref List <string> errorMesages)
        {
            List <CsvTransactionModel> csvTransactionModels = csvTransactionReader.Read(file);

            if (csvTransactionModels.Count > 0)
            {
                foreach (CsvTransactionModel transaction in csvTransactionModels)
                {
                    errorMesages.AddRange(csvValidator.Validate(transaction));
                }

                if (errorMesages.Count == 0)
                {
                    IEnumerable <TransactionModel> transactionModels = csvTransactionModels.Select(s => s.MapToModel());
                    transactionRepository.AddRange(transactionModels);
                }
            }
        }
        public async Task <bool> Add(IEnumerable <Transaction> transactions)
        {
            await _transactionRepository.AddRange(transactions);

            return(true);
        }