예제 #1
0
파일: Edit.cshtml.cs 프로젝트: bonnina/OOP
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            string photoFileName = null;
            string notesFileName = null;

            if (PatientView.Photo != null)
            {
                photoFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(PatientView.Photo.FileName);
                var imagePath = Path.Combine(_environment.WebRootPath, "images", photoFileName);

                using (var fileStream = new FileStream(imagePath, FileMode.Create))
                {
                    await PatientView.Photo.CopyToAsync(fileStream);
                }
            }

            if (PatientView.Notes != null)
            {
                notesFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(PatientView.Notes.FileName);
                var notesPath = Path.Combine(_environment.WebRootPath, "files", notesFileName);

                using (var fileStream = new FileStream(notesPath, FileMode.Create))
                {
                    await PatientView.Notes.CopyToAsync(fileStream);
                }
            }

            var speciesName = PatientView.Species.ToLowerInvariant();
            var species     = _speciesList.SearchBy(speciesName);

            if (species == null)
            {
                _speciesList.Add(new Models.DbModels.Species
                {
                    Name = speciesName
                });

                species = _speciesList.SearchBy(speciesName);
            }

            var patient = new Patient
            {
                Id        = PatientView.Id,
                Name      = PatientView.Name,
                Species   = species,
                Age       = PatientView.Age,
                PhotoPath = photoFileName,
                NotesPath = notesFileName
            };

            try
            {
                _patientsList.Edit(patient);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PatientExists(PatientView.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }