public async Task <IActionResult> Post(IFormFile upload) { var parserToUse = _fileParsers.FirstOrDefault(p => p.CanParseFile(upload.FileName)); if (parserToUse is null) { var validExtenstions = _fileParsers.Select(p => p.Extension); return(ValidationProblem($"File format not supported. Valid formats: {string.Join(", ", validExtenstions)}")); } var(Result, Errors) = parserToUse.ParseContent(upload.OpenReadStream()); if (Errors.Any()) { foreach (var e in Errors) { ModelState.AddModelError("ParsingError", e); } return(ValidationProblem(ModelState)); } await _repository.AddAsync(Result); await _repository.SaveChangesAsync(); return(Ok()); }
public async Task SuccesfullyWriteValidCsvInput() { var content = @"""Invoice0000001"",""1,000.00"",""USD"",""20/02/2019 12:33:16"",""Approved"" ""Invoice0000002"",""300.00"",""USD"",""21/02/2019 02:04:59"",""Failed"" ""Invoice0000003"",""40.00"",""EUR"",""01/02/2020 02:04:59"",""Finished"" "; IEnumerable <PaymentTransaction> transaction = default; await _repoMock.AddAsync(Arg.Do <IEnumerable <PaymentTransaction> >(q => transaction = q)); var fileContent = CreateFileContent(content, "test.csv"); var response = await _client.PostAsync("/uploads", fileContent); response.IsSuccessStatusCode.Should().BeTrue(); await _repoMock.Received().SaveChangesAsync(); transaction.Should().BeEquivalentTo(new PaymentTransaction { Id = "Invoice0000001", Amount = 1000m, Currency = "USD", TransactionDate = new DateTime(2019, 2, 20, 12, 33, 16), Status = PaymentTransactionStatus.A }, new PaymentTransaction { Id = "Invoice0000002", Amount = 300m, Currency = "USD", TransactionDate = new DateTime(2019, 2, 21, 2, 4, 59), Status = PaymentTransactionStatus.R }, new PaymentTransaction { Id = "Invoice0000003", Amount = 40m, Currency = "EUR", TransactionDate = new DateTime(2020, 2, 1, 2, 4, 59), Status = PaymentTransactionStatus.D }); }