Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Username, Email = Input.Email
                };

                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    if (user.Equals("Admin"))
                    {
                        var admin = new Admin
                        {
                            admin_email    = Input.Email,
                            admin_fname    = Input.admin_fname,
                            admin_lname    = Input.admin_lname,
                            admin_mname    = Input.admin_mname,
                            admin_password = Input.Password,
                            admin_phone    = Input.admin_phone,
                            admin_username = Input.Username
                        };
                        _context.Admin.Add(admin);
                        await _context.SaveChangesAsync();

                        await _userManager.AddToRoleAsync(user, "Admin");
                    }
                    if (!User.IsInRole("Assistant"))
                    {
                        var role = Input.Role;
                        if (!await _roleManager.RoleExistsAsync(role))
                        {
                            await _roleManager.CreateAsync(new IdentityRole(role));
                        }
                        if (role.Equals("Doctor"))
                        {
                            var doctor = new Doctor
                            {
                                dr_password   = Input.Password,
                                dr_email      = Input.Email,
                                dr_firstname  = Input.dr_firstname,
                                dr_middlename = Input.dr_middlename,
                                dr_lastname   = Input.dr_lastname,
                                dr_geneder    = Input.dr_gender,
                                dr_username   = Input.Username,
                                dr_phone      = Input.dr_phone,
                                dr_speciality = Input.dr_speciality,
                                dr_time       = Input.dr_time,
                                dr_address    = Input.dr_address,
                                dr_about      = Input.dr_about,
                                Id            = user.Id
                            };
                            _context.Doctor.Add(doctor);
                            await _context.SaveChangesAsync();

                            await _userManager.AddToRoleAsync(user, role);
                        }
                        else if (role == "Assistant")
                        {
                            var doctor1 = _context.Doctor.Where(e => e.dr_username == Input.doc_ref).Single();

                            var assistant = new Assistant
                            {
                                as_password = Input.Password,
                                as_email    = Input.Email,
                                as_username = Input.Username,
                                as_fname    = Input.as_fname,
                                as_mname    = Input.as_mname,
                                as_lname    = Input.as_lname,
                                doctor      = doctor1,
                                ins_doc     = doctor1.Id,
                                Id          = user.Id
                            };
                            _context.Assistant.Add(assistant);
                            await _context.SaveChangesAsync();

                            await _userManager.AddToRoleAsync(user, role);
                        }
                        else if (role == "InsuranceCompany")
                        {
                            var insurancecompany = new InsuranceCompany
                            {
                                ins_password = Input.Password,
                                ins_email    = Input.Email,
                                ins_username = Input.Username,
                                ins_name     = Input.ins_name,
                                ins_phone    = Input.ins_phone,
                                ins_address  = Input.ins_address,
                                ins_fax      = Input.ins_fax,
                                Id           = user.Id
                            };
                            _context.InsuranceCompany.Add(insurancecompany);
                            await _context.SaveChangesAsync();

                            await _userManager.AddToRoleAsync(user, role);
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                    else//role = assistant
                    {
                        var role = "Patient";
                        if (!await _roleManager.RoleExistsAsync(role))
                        {
                            await _roleManager.CreateAsync(new IdentityRole(role));
                        }
                        await _userManager.AddToRoleAsync(user, role);

                        var ins = _context.InsuranceCompany.Where(e => e.ins_name == Input.pat_ins_ref).Single();

                        var patient = new Patient
                        {
                            pat_password   = Input.Password,
                            pat_email      = Input.Email,
                            pat_username   = Input.Username,
                            pat_firstname  = Input.pat_firstname,
                            pat_middlename = Input.pat_middlename,
                            pat_lastname   = Input.pat_lastname,
                            pat_phone      = Input.pat_phone,
                            pat_address    = Input.pat_address,
                            pat_blood_type = Input.pat_blood,
                            //pat_birthday = Input.pat_birthday,
                            //InsuranceCompany = ins,
                            ins_ref = ins.Id,
                            Id      = user.Id
                        };
                        _context.Patient.Add(patient);
                        await _context.SaveChangesAsync();


                        return(RedirectToAction("Index", "Home"));
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            ViewData["doc_ref"]     = new SelectList(_context.Doctor, "dr_username", "dr_username");
            ViewData["pat_ins_ref"] = new SelectList(_context.InsuranceCompany, "ins_name", "ins_name");

            // If we got this far, something failed, redisplay form
            return(Page());
        }