public async Task <IActionResult> Create([Bind("ID,Name")] Students students) { if (ModelState.IsValid) { students.Email = _userManager.GetUserName(User); _context.Add(students); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(students)); }
public async Task <IActionResult> Create([Bind("ID,SeatDate,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15 ")] SeatBooking seatBooking) { //Show data for the next 4 weeks ViewData["CheckFullSession"] = _sessions.GetSingleWeekStats(seatBooking.SeatDate); if (ModelState.IsValid) { //Save locally for sending to calendar in GenerateCalendarEvents _dbCallsSessionDataDTO.AllSeatBookings.Add(seatBooking); //get the user email seatBooking.StudentEmail = _userManager.GetUserName(User); //get the user name var name = _context.Students.Where(s => s.Email == seatBooking.StudentEmail).Select(s => s.Name).FirstOrDefault(); //add to the booking seatBooking.Name = name.ToString(); // get session with the same date and same email // UPDATE them if they exist //var matchSeatBooking = _context.SeatBooking.FirstOrDefault(s => s.SeatDate == seatBooking.SeatDate && s.StudentEmail == _userManager.GetUserName(User)); //if (matchSeatBooking != null) //{ // //just need the id of the saved entry // seatBooking.ID = matchSeatBooking.ID; // matchSeatBooking.ID = 10000; //need to get rid of the other id throws an error // //update the existing record // _context.Update(seatBooking); //} //else //{//create a new record // _context.Add(seatBooking); //} _context.Add(seatBooking); await _context.SaveChangesAsync(); //delete out old session booking to keep db small await DeleteOldSessionBookings(); return(RedirectToAction(nameof(Index))); } return(View(seatBooking)); }
/// <summary> /// Add users to the local DB NOTE this is mostly for Google, as it doesn't use the Register, it just logs in. /// </summary> /// <param name="Email">there will always be an email from the login</param> /// <param name="Name">There will only be a name if they login with Google. </param> /// <returns></returns> public void AddUserToStudentDB(string Email, string Name) { //check if there is a Name in the Students table string studentEmail = Email; // _userManager.GetUserName(_userManager.GetUserName(User)); string studentName = Name; //Check if they in the DB var student = _context.Students.FirstOrDefault(m => m.Email == studentEmail); //No entry if (student == null) { //No match lets add it in if we have the Student name from Google Students myStudent = new Students { Name = studentName, Email = studentEmail }; _context.Add(myStudent); _context.SaveChangesAsync(); } }