Пример #1
0
        public async Task <IActionResult> RegisterCitizenAsPatient(RegisterCitizen registerCitizen)
        {
            ApplicationUser Citizen_with_max_ID = citizenRepository.GetMaxCitizen();

            if (ModelState.IsValid)
            {
                //copy data from registerviewmodel to identityuser
                var user = new ApplicationUser
                {
                    //CitizenId = 0,
                    CitizenId   = Citizen_with_max_ID.CitizenId + 1,
                    UserName    = registerCitizen.Email,
                    Email       = registerCitizen.Email,
                    Name        = registerCitizen.Name,
                    Gender      = registerCitizen.Gender,
                    Mobile      = registerCitizen.Mobile,
                    IsCitizen   = true,
                    BirthDate   = DateTime.Now,
                    Bloodgroup  = registerCitizen.Bloodgroup,
                    Height      = registerCitizen.Height,
                    Weight      = registerCitizen.Weight,
                    CheckupData = registerCitizen.CheckupData,
                    State       = registerCitizen.State,
                    City        = registerCitizen.City,
                    Address     = registerCitizen.Address,
                };

                CitizenHospital citizenHospital = new CitizenHospital();

                citizenHospital.Hospital = userManager.Users
                                           .Where(user => user.IsHospital == true)
                                           .Where(us => us.HospitalId == registerCitizen.Hospital_id_select)
                                           .FirstOrDefault();

                citizenHospital.CitizenId = Citizen_with_max_ID.CitizenId + 1;
                using (var transaction = context.Database.BeginTransaction())
                {
                    context.CitizenHospitals.Add(citizenHospital);
                    context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.CitizenHospitals ON;");
                    context.SaveChanges();
                    context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.CitizenHospitals OFF;");
                    transaction.Commit();
                }

                var result = await userManager.CreateAsync(user, registerCitizen.Password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, "Citizen");

                    var     max_patient = patientRepository.GetMaxPatient();
                    Patient patient     = new Patient();
                    patient.PatientId = max_patient.PatientId + 1;
                    patient.CitizenId = user.CitizenId;
                    patient.Hospital  = userManager.Users
                                        .Where(user => user.IsHospital == true)
                                        .Where(us => us.HospitalId == registerCitizen.Hospital_id_select)
                                        .FirstOrDefault();

                    using (var transaction = context.Database.BeginTransaction())
                    {
                        context.Patients.Add(patient);
                        context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Patients ON;");
                        context.SaveChanges();
                        context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Patients OFF;");
                        transaction.Commit();
                    }

                    TempData["SuccessMessage"] = "Citizen Registered & Hospitalized Successfully!";
                    return(RedirectToAction("Index", "Patient"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            return(View(registerCitizen));
        }