Exemplo n.º 1
0
        public async Task <IActionResult> UploadPatientsAsync()
        {
            var file = Request.Form.Files[0];

            await using (var fileContentStream = new MemoryStream())
            {
                await file.CopyToAsync(fileContentStream).ConfigureAwait(false);

                // Parse CSV file to a list of models
                var allPatients = _patientService.ParsePatients(fileContentStream.ToArray());

                // Validate input
                var validationResult = await _patientsCsvValidator.ValidateAsync(allPatients);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult));
                }

                var existingPatients = await _patientService.ExistingPatientsAsync(allPatients).ConfigureAwait(false);

                var newPatients = allPatients.AsQueryable().Except(existingPatients).ToList();

                await _patientService.CreatePatientsAsync(newPatients).ConfigureAwait(false);

                await _patientService.UpdatePatientsAsync(existingPatients).ConfigureAwait(false);
            }
            return(Ok());
        }