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> BookConfirmed(string slotID) { if (SlotExists(Int32.Parse(slotID))) { var slot = await _context.Slot.FindAsync(Int32.Parse(slotID)); slot.StudentID = _userManager.GetUserId(User); if (_context.Slot.Any(x => x.StudentID == slot.StudentID && x.StartTime.Date == slot.StartTime.Date)) { Console.WriteLine("Student has already made a booking on this day"); return(RedirectToAction(nameof(Index))); } _context.Update(slot); await _context.SaveChangesAsync(); } else { Console.WriteLine("Slot does not exist"); } return(RedirectToAction(nameof(Index))); }
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)); }
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> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new User { UserName = Input.Email, Email = Input.Email }; var id = Input.Email.Substring(0, Input.Email.IndexOf('@')); if (id.StartsWith('e')) { if (await _context.Staff.FindAsync(id) == null) { await _context.Staff.AddAsync(new Staff { StaffID = id, Email = Input.Email, Name = Input.Name }); } user.StaffID = id; } else if (id.StartsWith('s')) { if (await _context.Student.FindAsync(id) == null) { await _context.Student.AddAsync(new Student { StudentID = id, Email = Input.Email, Name = Input.Name }); } user.StudentID = id; } else { throw new Exception(); } await _context.SaveChangesAsync(); var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, id.StartsWith('e')?Constants.StaffRole : id.StartsWith('s')?Constants.StudentRole : throw new Exception()); _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> BookSlot(string roomid, DateTime starttime, [Bind("RoomID,StartTime,StaffID,StudentID")] Slot slot) { var studentid = HttpContext.User.Identity.Name.Substring(0, 8); if (roomid != slot.RoomID) { return(NotFound()); } if (_context.Slot.Where(x => x.StartTime.Date == starttime.Date && x.StudentID == studentid).Count() == 1) { ViewData["ErrorMessage"] = new string ("Failed to book slot. You already have a slot booked for this date."); return(View(slot)); } slot.StudentID = studentid; if (ModelState.IsValid) { try { _context.Update(slot); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SlotExists(slot.RoomID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } await _context.SaveChangesAsync(); return(View(slot)); }