public IActionResult EditPatient(PatientRecordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var patient = mapper.Map <PatientViewModel, Patient>(model.Patient);

                try
                {
                    repository.UpdatePatient(patient);
                }
                catch (Exception ex)
                {
                    logger.Log(LogLevel.Error, $"Failed to update patient: {ex}");
                }
                try
                {
                    repository.SaveAll();
                }
                catch (Exception ex)
                {
                    logger.Log(LogLevel.Error, $"Failed to save updated patient to db: {ex}");
                }

                var updatedPatient = repository.GetPatientById(patient.Id);
            }
            else
            {
                TempData["FormError"] = "Input data not valid";
            }

            return(RedirectToAction("Record"));
        }
Example #2
0
        public async Task <IActionResult> AddPatient(AdminViewModel model, int doctorId)
        {
            if (ModelState.IsValid)
            {
                var patient = mapper.Map <PatientViewModel, Patient>(model.Patient);

                var doctor = repository.GetDoctorById(doctorId);
                if (doctor != null)
                {
                    patient.Doctor = doctor;
                }

                patient.Record = new Record()
                {
                    Appointments = new List <Appointment>()
                };

                var user = new PMAUser()
                {
                    FirstName   = model.Patient.FirstName,
                    LastName    = model.Patient.LastName,
                    Email       = model.Patient.EmailAddress,
                    PhoneNumber = model.Patient.PhoneNumber,
                    UserName    = model.Patient.FirstName.Substring(0, 1) + model.Patient.LastName
                };

                //To Do: Add random generation of password, then after successful creation
                //send email to patient informing them of their login info.

                var result = await userManager.CreateAsync(user, "TestP@ss!1");

                if (!result.Succeeded)
                {
                    logger.Log(LogLevel.Error, $"Failed to create user account. {result.Errors.ToString()}");
                }
                else
                {
                    await userManager.AddToRoleAsync(user, "Patient");

                    patient.User = user;
                }

                try
                {
                    repository.AddPatient(patient);
                }
                catch (Exception ex)
                {
                    logger.Log(LogLevel.Error, $"Failed to add patient to db: {ex}");
                }

                try
                {
                    repository.SaveAll();
                }
                catch (Exception ex)
                {
                    logger.Log(LogLevel.Error, $"Failed to save added patient to db: {ex}");
                }

                return(RedirectToAction("PatientInfo", new { id = patient.Id }));
            }


            TempData["FormError"] = "Patient data is invalid";
            return(View(model));
        }