public bool ToggleUserInCompetition(long competitionID, int userID) { UserInCompetition userInCompetition = _context.UserInCompetitions.FirstOrDefault(o => o.CompetitionID == competitionID && o.UserID == userID); if (userInCompetition == null) { throw new ArgumentException(string.Format("Unable to retrieve UserInCompetition object: competitionID: {0}, userID: {1}", competitionID, userID)); } userInCompetition.IsActive = (userInCompetition.IsActive ? false : true); bool isActive = userInCompetition.IsActive; //HACK the NotMapped fields that are required will throw entity validation errors userInCompetition.FullName = "x"; userInCompetition.Email = "x"; try { _context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var eve in ex.EntityValidationErrors) { Console.WriteLine(eve.Entry.Entity.GetType().Name); Console.WriteLine(eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine(ve.PropertyName); Console.WriteLine(ve.ErrorMessage); } } Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Elmah.Error(ex)); } return(isActive); }
public void AddUserInCompetition(UserInCompetition userInCompetition, string allUsersPassword) { //see if the user is already registered. var userProfile = _context.UserProfiles.FirstOrDefault(o => !string.IsNullOrEmpty(o.Email) && o.Email.ToUpper() == userInCompetition.Email.ToUpper()); if (userProfile != null) { //user is already registered. ignore the FullName passed in. //see if the user is already in the competition var existingUserInCompetition = _context.UserInCompetitions.FirstOrDefault(o => o.CompetitionID == userInCompetition.CompetitionID && o.UserID == userProfile.UserId); if (existingUserInCompetition != null) { throw new RepositoryException(string.Format("The user {0} with email address {1} is already in this competition.", userInCompetition.FullName, userInCompetition.Email)); } } else { try { using (AccountRepository accountRepository = new AccountRepository(_context)) { accountRepository.CreateUser(userInCompetition.FullName, userInCompetition.Email); } } catch (MembershipCreateUserException e) { throw new RepositoryException(string.Format("Error creating user {0}, email {1}: {2}", userInCompetition.FullName, userInCompetition.Email, e.StatusCode)); } userProfile = _context.UserProfiles.FirstOrDefault(o => !string.IsNullOrEmpty(o.Email) && o.Email.ToUpper() == userInCompetition.Email.ToUpper()); if (userProfile == null) { throw new ApplicationException(string.Format("Unable to retrieve just-added user profile for user name {0}, email [1}", userInCompetition.FullName, userInCompetition.Email)); } } userInCompetition.DateInvited = DateTime.Now; userInCompetition.IsActive = true; userInCompetition.UserID = userProfile.UserId; //HACK Competition property is instantiated to an object with ID of 0 because we have UserInCompetition.Competition.Name in hidden input on view. //if left that way EF tries to create a new Competition record using the parameters passed in for the UserInCompetition! userInCompetition.Competition = null; //HACK NotMapped Required fields will throw validation error even though are not in database userInCompetition.FullName = userProfile.FullName; userInCompetition.Email = userProfile.Email; _context.UserInCompetitions.Add(userInCompetition); _context.SaveChanges(); }
public ActionResult AddUserInCompetition(long competitionID) { UserInCompetition userInCompetition = new UserInCompetition() { CompetitionID = competitionID }; using (CompetitionRepository repository = new CompetitionRepository()) { userInCompetition.Competition = repository.GetCompetition(competitionID); } return(View(userInCompetition)); }
public ActionResult AddUserInCompetition(UserInCompetition userInCompetition) { try { //HACK the competition property is set NULL in AddUserInCompetition. //this is needed to send email Competition competition = null; if (userInCompetition.SendEmail) { competition = userInCompetition.Competition; } using (CompetitionRepository repository = new CompetitionRepository()) { repository.AddUserInCompetition(userInCompetition, AccountRepository.AllUsersPassword); } if (userInCompetition.SendEmail) { userInCompetition.Competition = competition; using (var msg = new SendMail().UserInCompetitionAdded(userInCompetition, true)) { //msg.SendAsync(userState: userInCompetition.Email); msg.Send(); //msg.Dispose(); } } TempData[ControllerHelpers.PURR] = new Purr() { Title = "Success", Message = "The User was successfully added to the Competition." }; //entire page gets refreshed when this view calls parent.location.reload. //TODO: put the users table on a partial view and update it via a JSON call. return(View("../Shared/ClosePopup")); } catch (RepositoryException ex) { //clear model errors for DateInvited ModelState.Clear(); ModelState.AddModelError("AddUserInCompetition", ex.Message); } return(View(userInCompetition)); }
public virtual MvcMailMessage UserInCompetitionAdded(UserInCompetition userInCompetition, bool isAsync = false) { //this is in Views/SendMail/_Layout.cshtml ViewBag.Title = "User Added to Competition"; if (isAsync) { var smtpClientWrapper = new SmtpClientWrapper(); smtpClientWrapper.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(SmtpClientWrapper_SendCompleted); } //allows us to use strongly-typed model in Views/SendMail/UserInCompetitionAdded.cshtml ViewData.Model = userInCompetition; return(Populate(x => { x.Subject = "User Added To Competition"; x.ViewName = "UserInCompetitionAdded"; x.To.Add(userInCompetition.Email); })); }