// DISPLAY OPEN FIELDS TO EDIT (IF ADMIN) public IActionResult Edit(int?id) { Staff staff = _context.Staff.Find(id); StaffLink staffLink = _context.StaffLink.Find(id); StaffCreateViewModel model = GetStaffCreateViewModel(); model.Staff = staff; model.Staff.StaffLink = staffLink; return(View(model)); }
public IActionResult Edit(int id, Staff staff) { try // save staff to db { // entities to update Staff staffToUpdate = _context.Staff.Find(id); StaffLink staffLinkToUpdate = _context.StaffLink.Find(id); // assign staff name & email to entities marked for update staffToUpdate.Name = staff.Name; staffToUpdate.Email = staff.Email; /* * I've intentionally used both Fluent and standard LINQ * syntax below to show equivalent db querying techniques * (options are good, right?) */ // assign StaffLink RoleId staffLinkToUpdate.RoleId = _context.Role .Where(r => r.Title == staff.StaffLink.Role.Title) .Select(rId => rId.Id) .Single(); // assign StaffLink StaffTypeId staffLinkToUpdate.StaffTypeId = ( from sType in _context.StaffType where sType.Type == staff.StaffLink.StaffType.Type select sType.Id).FirstOrDefault(); // update db with changes _context.UpdateRange(staffToUpdate, staffLinkToUpdate); _context.SaveChanges(); return(RedirectToAction("Index") .WithSuccess("Successful Update", staff.Name + " has been updated")); } catch (Exception) { return(RedirectToAction("Index") .WithDanger("Update Not Successful", "Something went wrong. Try again.")); throw; } }
public string Delete(int id) { try { Staff staff = _context.Staff.Where(s => s.Id == id).Single(); StaffLink staffLink = _context.StaffLink.Where(sl => sl.StaffId == id).Single(); // deletes selected staff & associated staffLink record _context.RemoveRange(staff, staffLink); _context.SaveChanges(); return(staff.Name + " has successfully been removed from the team"); } catch (Exception) { throw; } }
public async Task <IActionResult> Login() { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Total")); } using (StaffController staffController = new StaffController(UserManager, SignInManager, _certitrackContext)) { try { Staff staff = await UserManager.FindByEmailAsync("*****@*****.**").ConfigureAwait(false); if (staff == null) { Role role = await RoleManager.FindByNameAsync("Admin").ConfigureAwait(false); StaffType staffType = await _certitrackContext.StaffType.FirstOrDefaultAsync().ConfigureAwait(false); if (role == null) { role = new Role { Title = "Admin", Description = "Can create, edit, delete anyone and anything. Basically, God mode.", Name = "Admin" }; await RoleManager.CreateAsync(role).ConfigureAwait(false); } if (staffType == null) { staffType = new StaffType { Type = "Head Therapist" }; await _certitrackContext.StaffType.AddAsync(staffType).ConfigureAwait(false); await _certitrackContext.SaveChangesAsync().ConfigureAwait(false); } StaffLink staffLink = new StaffLink { Role = role, StaffType = staffType }; staff = new Staff { UserName = "******", Email = "*****@*****.**", Name = "Admin", Password = "******", StaffLink = staffLink }; if (staff == null) { return(View()); } await staffController.Create(staff).ConfigureAwait(false); IdentityResult result = await UserManager.AddToRoleAsync(staff, "Admin").ConfigureAwait(false); // output result to console - debugging Console.WriteLine("UserManager.AddToRoleAsync - Result: " + result); ViewBag.Message = "DB Seed Successful - Staff: '" + staff.Name + "' was created"; } } catch (DbUpdateConcurrencyException e) { ViewBag.Message = e.Message; } } return(View()); }