public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; if (Input.Email.StartsWith('e')) { user.StaffID = Input.Email.Substring(0, 6); var staff = new Staff(); staff.StaffID = user.StaffID; staff.Email = Input.Email; staff.Name = Input.Name; _context.Add(staff); await _context.SaveChangesAsync(); } if (Input.Email.StartsWith('s')) { user.StudentID = Input.Email.Substring(0, 8); var student = new Student(); student.StudentID = user.StudentID; student.Email = Input.Email; student.Name = Input.Name; _context.Add(student); await _context.SaveChangesAsync(); } var result = await _userManager.CreateAsync(user, Input.Password); await _userManager.AddToRoleAsync(user, user.UserName.StartsWith('e')?Constants.StaffRole : user.UserName.StartsWith('s')?Constants.StudentRole : throw new Exception()); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); await _signInManager.SignInAsync(user, false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> Create(string RoomName, [Bind("RoomID,StartTime,StaffID,StudentID")] Slot slot) { //find the correct room id from the room name. slot.RoomID = _context.Room.FirstOrDefault(x => x.RoomName == RoomName).RoomID; //set the slot ID to the correct primary keys. slot.StaffID = FindPrimaryKeyFromSchoolID(slot.StaffID); if (slot.StudentID != null) { slot.StudentID = FindPrimaryKeyFromSchoolID(slot.StudentID); } if (!SlotCreationBusinessRules(slot)) { return(Create()); } if (ModelState.IsValid) { _context.Add(slot); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(Create()); }
public async Task <IActionResult> Create([Bind("RoomID,StartTime,StaffID,StudentID")] Slot slot) { ViewData["RoomID"] = new SelectList(_context.Room, "RoomID", "RoomID", slot.RoomID); ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffID", slot.StaffID); ViewData["StudentID"] = new SelectList(_context.Student, "StudentID", "StudentID", slot.StudentID); if (_context.Slot.Where(x => x.RoomID == slot.RoomID && x.StartTime.Date == slot.StartTime.Date).Count() == 2) { ViewData["DateMessage"] = new string($"Room {slot.RoomID} has exceeded the max amount of bookings for the selected date."); return(View(slot)); } if (_context.Slot.Where(x => x.StartTime.Date == slot.StartTime.Date && x.StaffID == slot.StaffID).Count() == 4) { ViewData["StaffMessage"] = new string($"You have exceeded the max amount of bookings for the selected date."); return(View(slot)); } if (!ValidTime(slot.StartTime)) { ViewData["DateMessage"] = new string($"Invalid time selected."); return(View(slot)); } if (ModelState.IsValid) { _context.Add(slot); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(slot)); }