public async Task <IActionResult> CreateDoctor(DoctorCreationBindingModel doctorCreationBindingModel) { ViewBag.Gender = genders.Select(g => new SelectListItem { Text = g, Value = g });; //Create 'Doctor' Role if it doesn't exist string RoleString = "Doctor"; var role = await _roleManager.RoleExistsAsync(RoleString); if (!role) { await _roleManager.CreateAsync(new IdentityRole(RoleString)); } Doctor doctor = null; //Validate Model if (ModelState.IsValid) { var user = CreateUser(doctorCreationBindingModel); //create user and assign role if successful var result = await _userManager.CreateAsync(user, doctorCreationBindingModel.Password); if (result.Succeeded) { //Fill role related attributes doctor = new Doctor() { Specialty = doctorCreationBindingModel.Specialty, OpenTime = doctorCreationBindingModel.OpenTime, CloseTime = doctorCreationBindingModel.CloseTime, Id = user.Id }; doctor.User = user; _context.Add(doctor); _context.SaveChanges(); await _userManager.AddToRoleAsync(user, RoleString); } else { return(Content("Failed to add User")); } if (result.Succeeded) { await _context.SaveChangesAsync(); return(RedirectToAction("Doctors")); } return(Content("Failed to add Doctor")); } return(View("./Doctors/Create", doctor)); }
public async Task <IActionResult> EditDoctor(string id, DoctorCreationBindingModel doctorCreationBindingModel) { if (id != doctorCreationBindingModel.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var doctor = _context.Doctors .Where(d => d.Id.Equals(id)) .Include(d => d.User) .Single(); doctor.Specialty = doctorCreationBindingModel.Specialty; doctor.OpenTime = doctorCreationBindingModel.OpenTime; doctor.CloseTime = doctorCreationBindingModel.CloseTime; MapUser(doctor.User, doctorCreationBindingModel); _context.Update(doctor); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DoctorExists(doctorCreationBindingModel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Doctors")); } return(View("./Doctors/Edit", doctorCreationBindingModel)); }