public ActionResult EditCompetition(long competitionID) { Competition competition = null; if (competitionID > 0) { using (CompetitionRepository repository = new CompetitionRepository()) { competition = repository.GetCompetition(competitionID); if (competition == null) { throw new ApplicationException(string.Format("Unable to retrieve Competition object for competitionID: ", competitionID)); } competition.UseDefaultImage = competition.IsUsingDefaultImage(); competition.CompetitionProgressBars = ControllerHelpers.GetCompetitionProgressBars(repository, competitionID); ViewBag.AllowEdit = (IsUserAdmin || competition.CreatorUserID == UserID); } } else { competition = new Competition() { IsActive = true }; ViewBag.AllowEdit = true; } //if another method redirected to here show the purr message if (TempData[ControllerHelpers.PURR] != null) { ViewBag.Purr = TempData[ControllerHelpers.PURR]; TempData[ControllerHelpers.PURR] = null; } return(View(competition)); }
public ActionResult EditCompetition(Competition competition, HttpPostedFileBase CustomImage) { if (!IsUserAdmin && competition.CompetitionID > 0 && competition.CreatorUserID != UserID) { throw new ApplicationException(string.Format("Possible security breach, User {0} attempted to edit Competition {1}.", UserID, competition.CompetitionID)); } if (!competition.UseDefaultImage && CustomImage != null) { string fileName = Guid.NewGuid().ToString() + Path.GetExtension(CustomImage.FileName); string filePath = string.Format(@"{0}\{1}\{2}", System.Web.HttpContext.Current.Request.PhysicalApplicationPath, ControllerHelpers.CustomImageFolder, fileName); if (ControllerHelpers.SaveImage(CustomImage.InputStream, filePath)) { competition.ImageSrc = fileName; } } else if (competition.UseDefaultImage && !string.IsNullOrEmpty(competition.ImageSrc)) { string filePath = string.Format(@"{0}\{1}\{2}", System.Web.HttpContext.Current.Request.PhysicalApplicationPath, ControllerHelpers.CustomImageFolder, competition.ImageSrc); try { if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } catch { } competition.ImageSrc = null; } using (CompetitionRepository repository = new CompetitionRepository()) { if (competition.CompetitionID <= 0) { competition.CreatedDate = DateTime.Now; competition.CreatorUserID = UserID; } repository.SaveCompetition(competition); } //the ClosePopup closes EditCompetition.cshtml and causes Index.cshtml to refresh, //which in turn calls the Index action. //cannot just set ViewBag because it is lost on redirect. //TempData survives one redirect, store it there. TempData[ControllerHelpers.PURR] = new Purr() { Title = "Success", Message = "Competition was saved successfully." }; return(View("../Shared/ClosePopup")); }
public ActionResult Index(SweatyTShirt sweatyTShirt) { int userID = UserID; sweatyTShirt.UserID = userID; using (CompetitionRepository competitionRepository = new CompetitionRepository()) { if (sweatyTShirt.IsSave) { sweatyTShirt.CreatedDate = DateTime.Now; competitionRepository.AddSweatyTShirt(sweatyTShirt); ViewBag.Purr = new Purr() { Title = "Success", Message = "Sweaty-T-Shirt was successfully added." }; } sweatyTShirt.Competitions = competitionRepository .GetUserInCompetitionsForUser(userID) .Where(o => o.IsActive) .Select(o => o.Competition).ToList(); if (sweatyTShirt.Competitions.Count > 0) { if (sweatyTShirt.CompetitionID > 0) { sweatyTShirt.Competition = sweatyTShirt.Competitions.FirstOrDefault(o => o.CompetitionID == sweatyTShirt.CompetitionID); } else { sweatyTShirt.Competition = sweatyTShirt.Competitions[0]; sweatyTShirt.CompetitionID = sweatyTShirt.Competition.CompetitionID; } if (sweatyTShirt.Competition == null) { throw new ApplicationException(string.Format("Unable to retrieve Competition object for sweatyTShirt.CompetitionID {0}", sweatyTShirt.CompetitionID)); } sweatyTShirt.Competition.CompetitionProgressBars = ControllerHelpers.GetCompetitionProgressBars(competitionRepository, sweatyTShirt.CompetitionID); if (sweatyTShirt.IsSave) { /* emails are sent in separate thread, see global.asax.cs*/ if (sweatyTShirt.PostToFacebook) { FacebookRepository.PostToFacebook(sweatyTShirt); } } } } if (TempData[ControllerHelpers.PURR] != null) { ViewBag.Purr = TempData[ControllerHelpers.PURR]; TempData[ControllerHelpers.PURR] = null; } sweatyTShirt.IsSave = false; sweatyTShirt.Description = null; //will get client side validation errors because manually adding model to view, need to clear them. ModelState.Clear(); return(View(sweatyTShirt)); }