public ActionResult DeleteUsersFromSession(UsersEnrolledBySessionViewModel userEnrolledVM) { var filteredCollectionsOfUsersToDelete = userEnrolledVM.Enrollments.Where(x => x.isSelected == true); bool usersDeleted = false; using (WSADDbContext context = new WSADDbContext()) { foreach (var userItems in filteredCollectionsOfUsersToDelete) { SessionCart dtoToDelete = context.SessionCartItems.FirstOrDefault(row => row.UserId == userItems.User.UserId && row.SessionId == userEnrolledVM.SessionId); if (dtoToDelete != null) { context.SessionCartItems.Remove(dtoToDelete); usersDeleted = true; } } context.SaveChanges(); } if (usersDeleted) { TempData["UsersRemoved"] = "Users Removed!"; return(RedirectToAction("GetEnrolledUsers", new { Id = userEnrolledVM.SessionId })); } return(RedirectToAction("GetEnrolledUsers", new { Id = userEnrolledVM.SessionId })); }
public ActionResult EditUser(EditUserAdminViewModel editUserAdminViewModel) { bool passwordChanged = false; bool emailChanged = false; if (!ModelState.IsValid) { return(View(editUserAdminViewModel)); } //admin resets the password if (!string.IsNullOrWhiteSpace(editUserAdminViewModel.UserPassword)) { if (!editUserAdminViewModel.UserPassword.Equals(editUserAdminViewModel.UserConfirmPassword)) { ModelState.AddModelError("", "Passwords must match"); return(View(editUserAdminViewModel)); } else { passwordChanged = true; } } using (WSADDbContext context = new WSADDbContext()) { User userDTO = context.Users.Find(editUserAdminViewModel.UserId); if (userDTO == null) { return(Content("Inavid User ID")); } if (!userDTO.UserEmailAddress.Equals(editUserAdminViewModel.UserEmailAddress)) { userDTO.UserEmailAddress = editUserAdminViewModel.UserEmailAddress; emailChanged = true; } userDTO.UserFirstName = editUserAdminViewModel.UserFirstName; userDTO.UserLastName = editUserAdminViewModel.UserLastName; userDTO.UserEmailAddress = editUserAdminViewModel.UserEmailAddress; userDTO.UserCompany = editUserAdminViewModel.UserCompany; userDTO.UserIsActive = editUserAdminViewModel.UserIsActive; userDTO.UserIsAdmin = editUserAdminViewModel.UserIsAdmin; userDTO.UserDateModified = DateTime.Now; if (passwordChanged) { userDTO.UserPassword = editUserAdminViewModel.UserPassword; } context.SaveChanges(); } if (emailChanged || passwordChanged) { TempData["AdminChange"] = "User Credentials Changed"; } return(View(editUserAdminViewModel)); }
public ActionResult Delete(List <ManageSessionViewModel> collectionOfSessionsToDelete) { //Filter collection of Sessions, and seperate the isSelected items only var fileteredCollectionOfSessionsToDelete = collectionOfSessionsToDelete.Where(x => x.isSelected == true); //Get reference to the database using (WSADDbContext context = new WSADDbContext()) { //iterate and delete each object foreach (var vmItems in fileteredCollectionOfSessionsToDelete) { var dtoToDelete = context.Sessions.FirstOrDefault(row => row.SessionId == vmItems.SessionId); context.Sessions.Remove(dtoToDelete); //delete the rows in the sessionCart table (null pointer prevented) on user cart GET var sessionCartDtoToDelete = context.SessionCartItems.FirstOrDefault(row => row.SessionId == vmItems.SessionId); if (sessionCartDtoToDelete != null) { context.SessionCartItems.Remove(sessionCartDtoToDelete); } } context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Edit(EditSessionViewModel editSessionVM) { if (!ModelState.IsValid) { return(View(editSessionVM)); } using (WSADDbContext context = new WSADDbContext()) { Session sessionDTO = context.Sessions.Find(editSessionVM.SessionId); if (sessionDTO == null) { return(Content("Inavid Session ID")); } sessionDTO.SessionTitle = editSessionVM.SessionTitle; sessionDTO.SessionDescription = editSessionVM.SessionDescription; sessionDTO.SessionPresenter = editSessionVM.SessionPresenter; sessionDTO.SessionAddress = editSessionVM.SessionAddress; sessionDTO.SessionRoom = editSessionVM.SessionRoom; sessionDTO.SessionSeatsAvailable = editSessionVM.SessionSeatsAvailable; context.SaveChanges(); } // Toast to show the session edited return(View()); }
public ActionResult AddToSession(List <SessionViewModel> sessions) { //Verify that sessionToAdd is not null if (sessions == null) { return(RedirectToAction("Index")); } //Capture Sessions to Add (filter by isSelected) sessions = sessions.Where(p => p.isSelected).ToList(); //If there are no sessions to add, then redirect to sessionCart index if (!(sessions.Count > 0)) { return(RedirectToAction("Index")); } //get user from user.identity.name string emailAddress = User.Identity.Name; //get user from the database -- we need their user id using (WSADDbContext context = new WSADDbContext()) { //Get user info //Get user id from DB int userId = context.Users .Where(row => row.UserEmailAddress == emailAddress) .Select(row => row.UserId) .FirstOrDefault(); foreach (SessionViewModel sessionVM in sessions) { //check to see if the user, session combo already exists, then we say already enrolled //does not make sense to do it here, since we do not have quantity //create session cart dto SessionCart sessionCartDTO = new SessionCart() { //add product id and user id to dto UserId = userId, SessionId = sessionVM.SessionId }; //add dto to db conetext context.SessionCartItems.Add(sessionCartDTO); } context.SaveChanges(); } //redirect to shopping cart index return(RedirectToAction("Index")); }
/// <summary> /// /// </summary> /// <param name="sessionId"></param> /// <param name="userId"></param> /// <returns></returns> public ActionResult AddUserToSession(int sessionId, int userId) { bool userAdded = false; //Check for valid session and user id's if (sessionId <= 0 || userId <= 0) { return(this.HttpNotFound("Invalid Input Parameters")); } using (WSADDbContext context = new WSADDbContext()) { Session sessionDTO = context.Sessions.FirstOrDefault(x => x.SessionId == sessionId); User userDTO = context.Users.FirstOrDefault(x => x.UserId == userId); if (sessionDTO == null || userDTO == null) { return(this.HttpNotFound("Invalid Input Parameters")); } //Check to see if the user+session Combination already exists, if not add it SessionCart sessionCartDto = context.SessionCartItems.FirstOrDefault(row => sessionDTO.SessionId == sessionId && row.UserId == userDTO.UserId); //if (sessionCartDto == null) //{ SessionCart sessionCartItemToAdd = new SessionCart() { UserId = userId, SessionId = sessionId }; context.SessionCartItems.Add(sessionCartItemToAdd); userAdded = true; //} #region MyRegion //} //else //{ // //if (!userAdded) // //{ // // TempData["NoDuplicates"] = "User Already Registered to this session"; // // return RedirectToAction("GetEnrolledUsers", new { sessionId }); // //} //} #endregion context.SaveChanges(); } return(RedirectToAction("GetEnrolledUsers", new { Id = sessionId })); }
public ActionResult Create(CreateUserViewModel newUser) { //Check required fields if (!ModelState.IsValid) { return(View(newUser)); } //Check Password & PasswordConfirm if (!newUser.Password.Equals(newUser.PasswordConfirm)) { ModelState.AddModelError("", "Password does not match Password Confirm"); return(View(newUser)); } //Create DbContext instance using (WSADDbContext context = new WSADDbContext()) { //Check username is not a duplicate if (context.Users.Any(row => row.Username.Equals(newUser.Username))) { ModelState.AddModelError("", "Username '" + newUser.Username + "' already exists. Try Again"); newUser.Username = ""; return(View(newUser)); } //Create User DTO User newUserDTO = new Models.Data.User() { FirstName = newUser.FirstName, LastName = newUser.LastName, EmailAddress = newUser.EmailAddress, IsActive = true, IsAdmin = false, Username = newUser.Username, Password = newUser.Password, DateCreated = DateTime.Now, DateModified = DateTime.Now, Gender = newUser.Gender }; //Add to DbContext newUserDTO = context.Users.Add(newUserDTO); //Save changes context.SaveChanges(); } //Redirect to login return(RedirectToAction("login")); }
public ActionResult Delete(List <ManageUserViewModel> collectionOfUserVM) { var vmItemsToDelete = collectionOfUserVM.Where(x => x.IsSelected == true); using (WSADDbContext context = new WSADDbContext()) { foreach (var vmItems in vmItemsToDelete) { var dtoToDelete = context.Users.FirstOrDefault(row => row.Id == vmItems.Id); context.Users.Remove(dtoToDelete); } context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Delete(List <SessionSignupViewModel> sessionsToAdd) { var vmItemsToDelete = sessionsToAdd.Where(x => x.IsSelected == true); using (WSADDbContext context = new WSADDbContext()) { foreach (var vmItems in vmItemsToDelete) { var dtoToDelete = context.SessionSignup.FirstOrDefault(row => row.Id == vmItems.Id); context.SessionSignup.Remove(dtoToDelete); } context.SaveChanges(); } return(RedirectToAction("Index")); }
// TODO This method needs to be changed to delete sessions, another page will handle adding sessions // TODO similar to adding users. This will be an admin only method as will this entire class. // TODO collectionOfSessionVM is not picking up items from checkbox-- index looks good must be below public ActionResult DeleteSessions(List <ManageSessionViewModel> newSession) { // filter collectionofsessions to find the selected items only var vmItemsToDelete = newSession.Where(x => x.IsSelected == true); using (WSADDbContext context = new WSADDbContext()) { foreach (var vmItems in vmItemsToDelete) { var dtoToDelete = context.Sessions.FirstOrDefault(row => row.Id == vmItems.Id); context.Sessions.Remove(dtoToDelete); } context.SaveChanges(); } return(RedirectToAction("index")); }
public ActionResult Enroll(List <EnrollSessionViewModel> collectionOfSessionsVM) { //Filter collectionOfUsers to find only Selected Items var vmItemsToEnroll = collectionOfSessionsVM.Where(x => x.IsSelected == true); //Get logged in user string username = User.Identity.Name; User userDTO; //Do Enroll using (WSADDbContext context = new WSADDbContext()) { //Get user db info an Id userDTO = context.Users.FirstOrDefault(row => row.Username == username); int userId = userDTO.Id; //Loop through ViewModel Items to update foreach (var vmItem in vmItemsToEnroll) { UserSession userSessionDTO = new UserSession(); var sessionDTO = context.Sessions.FirstOrDefault(row => row.Id == vmItem.Id); userSessionDTO.User_Id = userId; userSessionDTO.Session_Id = vmItem.Id; //Check that session hasn't already been enrolled by user if (context.UserSessions.Any(row => row.User_Id.Equals(userId)) && context.UserSessions.Any(row => row.Session_Id.Equals(vmItem.Id))) { TempData["error"] = "Error: You are already enrolled in this course."; return(RedirectToAction("Index")); } else { context.UserSessions.Add(userSessionDTO); } } context.SaveChanges(); } return(RedirectToAction("Index", "Schedule")); }
public ActionResult Delete(List <SessionCartViewModel> collectionOfCartItems) { //Filter collection of Sessions to find the selected items only var cartItemsToDelete = collectionOfCartItems.Where(x => x.isSelected == true); //Get reference to the database using (WSADDbContext context = new WSADDbContext()) { //iterate and delete each object foreach (var sessionItem in cartItemsToDelete) { var dtoToDelete = context.SessionCartItems.FirstOrDefault(row => row.SessionCartId == sessionItem.SessionCartId); context.SessionCartItems.Remove(dtoToDelete); } context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Create(CreateUserViewModel newUser) { if (!ModelState.IsValid) { return(View(newUser)); } if (!newUser.Password.Equals(newUser.PasswordConfirm)) { ModelState.AddModelError("", "Password does not match password confirm."); return(View(newUser)); } using (WSADDbContext context = new WSADDbContext()) { if (context.Users.Any(row => row.Username.Equals(newUser.Username))) { ModelState.AddModelError("", "Username '" + newUser.Username + "' already exists. Try again."); newUser.Username = ""; return(View(newUser)); } User newUserDTO = new Models.Data.User() { FirstName = newUser.FirstName, LastName = newUser.LastName, EmailAddress = newUser.EmailAddress, IsActive = true, IsAdmin = false, Username = newUser.Username, Password = newUser.Password, DateCreated = DateTime.Now, DateModified = DateTime.Now, Gender = newUser.Gender }; newUserDTO = context.Users.Add(newUserDTO); context.SaveChanges(); } return(RedirectToAction("login")); }
public void saveCorrespondence(ContactEmailViewModel contactMessage) { //Create DBContext instance using (WSADDbContext context = new WSADDbContext()) { //Create correspondenceDTO Correspondence correspondenceDTO = new Correspondence() { Name = contactMessage.Name, Email = contactMessage.Email, Message = contactMessage.Message }; //Add to DbContext correspondenceDTO = context.Correspondences.Add(correspondenceDTO); //Save changes context.SaveChanges(); } }
public ActionResult Delete(List <ManageSessionViewModel> collectionOfSessionsVM) { //Filter collectionOfSessions to find only Selected Items var vmItemsToDelete = collectionOfSessionsVM.Where(x => x.IsSelected == true); //Do delete using (WSADDbContext context = new WSADDbContext()) { //Loop through ViewModel Items to delete foreach (var vmItem in vmItemsToDelete) { var dtoToDelete = context.Sessions.FirstOrDefault(row => row.Id == vmItem.Id); context.Sessions.Remove(dtoToDelete); } context.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Create(CreateSessionViewModel newSession) { using (WSADDbContext context = new WSADDbContext()) { Session newSessionDTO = new WSAD_App1.Models.Data.Session() { Title = newSession.Title, Description = newSession.Description, Presenter = newSession.Presenter, Room = newSession.Room, Time = newSession.Time, Occupancy = newSession.Occupancy }; newSessionDTO = context.Sessions.Add(newSessionDTO); context.SaveChanges(); } return(RedirectToAction("index")); }
public ActionResult Create(CreateSessionViewModel newSession) { //Check required fields if (!ModelState.IsValid) { return(View(newSession)); } //Create DbContext using (WSADDbContext context = new WSADDbContext()) { //Check for duplicate sessions if (context.Sessions.Any(row => row.Course.Equals(newSession.Course))) { ModelState.AddModelError("", "Session '" + newSession.Course + "' already exists. Try Again"); newSession.Course = ""; return(View(newSession)); } //Create Session DTO Session newSessionDTO = new WSAD_App1.Models.Data.Session() { Course = newSession.Course, Instructor = newSession.Instructor, MeetingDate = newSession.MeetingDate, MeetingTime = newSession.MeetingTime, Description = newSession.Description }; //Add to DbContext newSessionDTO = context.Sessions.Add(newSessionDTO); //Save changes context.SaveChanges(); } //Redirect to login return(RedirectToAction("Index")); }
public ActionResult Edit(EditSessionViewModel editSessionVM) { //validate Model if (!ModelState.IsValid) { return(View(editSessionVM)); } //get our user from DB using (WSADDbContext context = new WSADDbContext()) { Session sessionDTO = context.Sessions.Find(editSessionVM.Id); if (sessionDTO == null) { return(Content("Invalid user Id")); } //set update values from viewmodel sessionDTO.Title = editSessionVM.Title; sessionDTO.Description = editSessionVM.Description; sessionDTO.Presenter = editSessionVM.Presenter; sessionDTO.Time = editSessionVM.Time; sessionDTO.Room = editSessionVM.Room; sessionDTO.Occupancy = editSessionVM.Occupancy; //save changes context.SaveChanges(); } return(RedirectToAction("index")); }
public ActionResult Delete(List <EnrolledSessionViewModel> collectionOfSessionsVM) { //Get sessions to delete var vmItemsToDelete = collectionOfSessionsVM.Where(x => x.IsSelected == true); //Do delete using (WSADDbContext context = new WSADDbContext()) { //Loop through ViewModel Items to delete foreach (var vmItem in vmItemsToDelete) { var dtoToDelete = context.UserSessions.FirstOrDefault(row => row.Session_Id == vmItem.Session_Id); context.UserSessions.Remove(dtoToDelete); } context.SaveChanges(); } TempData["success"] = "Successfully unenrolled from course(s)"; return(RedirectToAction("Index")); }
public ActionResult CreateSession(CreateSessionViewModel newSession) { bool sessionCreated = false; //admin has the right to populate this, and commit changes to the database if (newSession == null) { ModelState.AddModelError("", "No Message Provided"); return(View()); } //form verification if (string.IsNullOrWhiteSpace(newSession.SessionTitle) || string.IsNullOrWhiteSpace(newSession.SessionDescription) || string.IsNullOrWhiteSpace(newSession.SessionPresenter) || string.IsNullOrWhiteSpace(newSession.SessionAddress) || string.IsNullOrWhiteSpace(newSession.SessionRoom)) { ModelState.AddModelError("", "All Fields Required!"); return(View()); } //Seats Verification if (newSession.SessionSeatsAvailable == null || newSession.SessionSeatsAvailable < 1) { ModelState.AddModelError("", "Session Cannot Be Added"); return(View()); } //TODO: Loop through all the user emails and notify about new session //store the session into the database using (WSADDbContext context = new WSADDbContext()) { //Check to see if a session already exists if (context.Sessions.Any(row => row.SessionTitle.Equals(newSession.SessionTitle, StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("", "A Session with the Title " + newSession.SessionTitle + " Already Exists"); newSession.SessionTitle = " "; return(View(newSession)); } Session newSessionDto = new Session() { SessionTitle = newSession.SessionTitle, SessionDescription = newSession.SessionDescription, SessionPresenter = newSession.SessionPresenter, SessionAddress = newSession.SessionAddress, SessionRoom = newSession.SessionRoom, SessionSeatsAvailable = newSession.SessionSeatsAvailable, SessionDateCreated = DateTime.Now, SessionDateModified = DateTime.Now }; newSessionDto = context.Sessions.Add(newSessionDto); //commit changes to the database try { context.SaveChanges(); sessionCreated = true; } catch (DbException d) { return(Content(d.Message)); } } //toast - session created sucessfully if (sessionCreated) { TempData["SessionSuccess"] = "Session Created Successfully"; return(View()); } return(View()); }
public ActionResult Edit(EditViewModel editVM) { //Variables bool needsPasswordReset = false; bool usernameHasChanged = false; //validate Model if (!ModelState.IsValid) { return(View(editVM)); } //check for password change? if (!string.IsNullOrWhiteSpace(editVM.Password)) { //compare password and passwordconfirm if (editVM.Password != editVM.PasswordConfirm) { ModelState.AddModelError("", "Password and password confirm must match."); return(View(editVM)); } else { needsPasswordReset = true; } } //get our user from DB using (WSADDbContext context = new WSADDbContext()) { User userDTO = context.Users.Find(editVM.Id); if (userDTO == null) { return(Content("Invalid user Id")); } //check for username change if (userDTO.Username != editVM.UserName) { userDTO.Username = editVM.UserName; usernameHasChanged = true; } //set update values from viewmodel userDTO.FirstName = editVM.FirstName; userDTO.LastName = editVM.LastName; userDTO.DateModified = DateTime.Now; userDTO.EmailAddress = editVM.EmailAddress; userDTO.Gender = editVM.Gender; if (needsPasswordReset) { userDTO.Password = editVM.Password; } //save changes context.SaveChanges(); } if (usernameHasChanged || needsPasswordReset) { TempData["LogoutMessage"] = "After a username or password change. Please log in with the new credentials."; return(RedirectToAction("Logout")); } return(RedirectToAction("UserProfile")); }
public ActionResult Index(ContactUsViewModel newContact) { bool contactStatus = false; ViewBag.ContactReason = new SelectList(new[] { "Request Admin Account", "Forgot Password", "General Inquiry" }); if (newContact == null) { ModelState.AddModelError("", "No Message Provided"); return(View()); } if (string.IsNullOrWhiteSpace(newContact.ContactName) || string.IsNullOrWhiteSpace(newContact.ContactEmail) || string.IsNullOrWhiteSpace(newContact.ContactReason) || string.IsNullOrWhiteSpace(newContact.ContactDetail)) { ModelState.AddModelError("", "All Fields Required!"); return(View()); } MailMessage email = new MailMessage(); email.To.Add("*****@*****.**"); email.From = new MailAddress(newContact.ContactEmail); email.Subject = "Email Inquiry"; email.Body = string.Format("Name: {0} \r\nMessage {1} \r\nEmail {2}", newContact.ContactName, newContact.ContactDetail, newContact.ContactEmail); email.IsBodyHtml = false; SmtpClient client = new SmtpClient(); client.Host = "mail.twc.com"; try { client.Send(email); } catch (SmtpException) { throw new SmtpException(); } using (WSADDbContext context = new WSADDbContext()) { Contact newContactDto = new Contact() { ContactName = newContact.ContactName, ContactEmail = newContact.ContactEmail, ContactReason = newContact.ContactReason, ContactDetail = newContact.ContactDetail }; newContactDto = context.Contacts.Add(newContactDto); /* * if (newContact.ContactReason.Equals("Request Admin Account")) * { * //send email to the user saying account access granted * //need to access the user account table and change the bit field of the user * //need to access the edit user view model first * } * else if (newContact.ContactReason.Equals("Forgot Password")) * { * //send the password as an email to the user * //need to query for the password * } * else * { * //general inquiry * * } */ try { context.SaveChanges(); contactStatus = true; } catch (DbUpdateException d) { Console.WriteLine(d.Message); } } if (contactStatus) { TempData["contact"] = "Thank You- We will get in touch via Email"; //redirect to the login page return(View()); } return(View()); }
public ActionResult Edit(EditViewModel editVM) { bool passwordChanged = false; bool emailChanged = false; if (!ModelState.IsValid) { return(View(editVM)); } if (!string.IsNullOrWhiteSpace(editVM.UserPassword)) // we assume user changes password { if (!editVM.UserPassword.Equals(editVM.UserConfirmPassword)) { ModelState.AddModelError("", "Passwords must match"); return(View(editVM)); } else { passwordChanged = true; } } using (WSADDbContext context = new WSADDbContext()) { User userDTO = context.Users.Find(editVM.UserId); if (userDTO == null) { return(Content("Inavid User ID")); } if (!userDTO.UserEmailAddress.Equals(editVM.UserEmailAddress)) { userDTO.UserEmailAddress = editVM.UserEmailAddress; emailChanged = true; } userDTO.UserFirstName = editVM.UserFirstName; userDTO.UserLastName = editVM.UserLastName; userDTO.UserEmailAddress = editVM.UserEmailAddress; userDTO.UserCompany = editVM.UserCompany; userDTO.UserDateModified = DateTime.Now; if (passwordChanged) { userDTO.UserPassword = editVM.UserPassword; } context.SaveChanges(); } if (emailChanged || passwordChanged) { TempData["LogoutMessage"] = "Username & Password CHANGED. [Please login with updated credentials]"; return(RedirectToAction("Logout")); } else { return(RedirectToAction("UserProfile")); } }
public ActionResult Create(CreateUserViewModel newUser) { bool accountCreated = false; //ensures all the [Required] data is populated if (!ModelState.IsValid) { return(View(newUser)); } //Tally the password and the confirm password if (!newUser.UserPassword.Equals(newUser.UserPasswordConfirm)) { ModelState.AddModelError("", "Passwords do not match!"); return(View(newUser)); } using (WSADDbContext context = new WSADDbContext()) { //Check to see if the email id already exists in the database if (context.Users.Any(row => row.UserEmailAddress.Equals(newUser.UserEmailAddress))) { ModelState.AddModelError("", "An Account With " + newUser.UserEmailAddress + " Already Exists"); newUser.UserEmailAddress = " "; return(View(newUser)); } //User DTO User newUserDto = new User() { //viewModel.property = newUser.FirstName UserFirstName = newUser.UserFirstName, UserLastName = newUser.UserLastName, UserEmailAddress = newUser.UserEmailAddress, UserPassword = newUser.UserPassword, UserCompany = newUser.UserCompany, UserIsActive = true, UserIsAdmin = false, UserDateCreated = DateTime.Now, UserDateModified = DateTime.Now }; //add it to the db context newUserDto = context.Users.Add(newUserDto); //Save & commit changes to db try { context.SaveChanges(); accountCreated = true; } catch (DbUpdateException d) { Console.WriteLine(d.Message); } } if (accountCreated) { TempData["AccountCreatedMessage"] = "Account Created Sucessfully"; //redirect to the login page return(RedirectToAction("login")); } return(View(newUser)); }
public ActionResult AddToSessionOrder(List <SessionViewModel> sessionsToAdd) { //verify that sessionsToAdd is not null if (sessionsToAdd == null) { return(RedirectToAction("Index")); } //capture sessions to add (filter by isSelected) sessionsToAdd = sessionsToAdd.Where(p => p.IsSelected).ToList(); //IF there are no sessions to add, redirect to shopping cart index if (sessionsToAdd.Count <= 0) { return(RedirectToAction("Index")); } //get user from User.Identity.Name string username = User.Identity.Name; using (WSADDbContext context = new WSADDbContext()) { //get user from db, need their user Id int userId = context.Users .Where(row => row.Username == username) .Select(row => row.Id) .FirstOrDefault(); foreach (SessionViewModel sVM in sessionsToAdd) { //does this session/user combo exist if (context.SessionSignup.Any(row => row.UserId == userId && row.SessionId == sVM.Id)) { //update quantity SessionSignup existingSessionSignupDTO = context.SessionSignup.FirstOrDefault(row => row.UserId == userId && row.SessionId == sVM.Id); existingSessionSignupDTO.Quantity++; } else { //create a Session checkout DTO SessionSignup sessionDTO = new SessionSignup() { //add the session id and user id and quantity to the dto UserId = userId, SessionId = sVM.Id, Quantity = 1 }; //add the dto to the dbcontext context.SessionSignup.Add(sessionDTO); } } //save the db context context.SaveChanges(); } //redirect to session list index return(RedirectToAction("Index")); }