Esempio n. 1
0
        public async Task <IActionResult> ImportRecordsFromCsv(IFormFile csvFile)
        {
            try
            {
                if (!_csvFileValidator.Validate(csvFile))
                {
                    return(BadRequest("Please upload valid csv file."));
                }

                var fileName = _temporaryFileNameGenerator.GenerateFileName();

                var tmpFileDirectory = Path.Combine(_environment.WebRootPath, _csvImportSettings.TemporaryFilesFolder);

                if (!Directory.Exists(tmpFileDirectory))
                {
                    Directory.CreateDirectory(tmpFileDirectory);
                }

                var fullTmpCsvFilePath = Path.Combine(tmpFileDirectory, $"{fileName}.csv");


                using (var stream = new FileStream(fullTmpCsvFilePath, FileMode.Create))
                {
                    await csvFile.CopyToAsync(stream);
                }

                await _salesRecordsService.ImportRecordsFromCsvFile(fullTmpCsvFilePath);

                if (System.IO.File.Exists(fullTmpCsvFilePath))
                {
                    System.IO.File.Delete(fullTmpCsvFilePath);
                }

                return(Ok());
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Unexpected exception in method {nameof(ImportRecordsFromCsv)}.");
                throw;
            }
        }
Esempio n. 2
0
 public async Task ImportRecordsFromCsvFile_WhenFilePathIsNull_ShouldThrowException()
 {
     await _salesRecordsService.ImportRecordsFromCsvFile(null);
 }