public static bool IsContestModelValid(Category category, ContestBindingModel model) { if (category == null) { return false; } if (model.ParticipantsNumberDeadline == null && model.DeadLine == null) { return false; } if (model.RewardType == RewardType.TopNWinners && model.WinnersNumber == null) { return false; } if (model.ParticipationType == ParticipationType.Closed && model.Participants == null) { return false; } if (model.VotingType == VotingType.Closed && model.Voters == null) { return false; } return true; }
public ActionResult Create(ContestBindingModel model) { if (this.ModelState != null && this.ModelState.IsValid) { Category category = this.ContestsData.Categories.Find(model.Category); bool isModelValid = CustomValidators.IsContestModelValid(category, model); if (!isModelValid) { this.AddToastMessage("Error", "Invalid data.", ToastType.Error); return View(model); } ICollection<User> voters = model.VotingType == VotingType.Closed ? this.GetUsers(model.Voters) : new HashSet<User>(); ICollection<User> participants = model.ParticipationType == ParticipationType.Closed ? this.GetUsers(model.Participants) : new HashSet<User>(); var contest = new Contest { Title = model.Title, Description = model.Description, OrganizatorId = this.UserProfile.Id, RewardType = model.RewardType, DeadlineType = model.DeadlineType, ParticipationType = model.ParticipationType, VotingType = model.VotingType, Voters = voters, WinnersCount = model.WinnersNumber, ParticipantsNumberDeadline = model.ParticipantsNumberDeadline, Participants = participants, DeadLine = model.DeadLine, CategoryId = model.Category }; if (model.Upload != null && model.Upload.ContentLength > 0) { var wallpaperPaths = Helpers.UploadImages.UploadImage(model.Upload, true); var wallpaperUrl = Dropbox.Download(wallpaperPaths[0]); var wallpaperThumbUrl = Dropbox.Download(wallpaperPaths[1], "Thumbnails"); contest.WallpaperPath = wallpaperPaths[0]; contest.WallpaperUrl = wallpaperUrl; contest.WallpaperThumbPath = wallpaperPaths[1]; contest.WallpaperThumbUrl = wallpaperThumbUrl; } else { contest.WallpaperUrl = AppKeys.WallpaperUrl; } this.ContestsData.Contests.Add(contest); this.ContestsData.SaveChanges(); this.AddToastMessage("Success", "Contest created.", ToastType.Success); return this.RedirectToAction("Details", "Users", routeValues: new { id = this.UserProfile.Id, area = "" }); } return this.View(); }
public ActionResult Create(ContestBindingModel model) { if (model != null && this.ModelState.IsValid) { var contest = Mapper.Map <Contest>(model); contest.DateCreated = DateTime.Now; contest.Creator = this.UserProfile; contest.NumberOfPrices = model.NumberOfPrices ?? 1; this.Data.Contests.Add(contest); this.Data.SaveChanges(); return(this.RedirectToAction("Details", new { id = contest.Id })); } return(this.View(model)); }
public ActionResult EditContest(int id, ContestBindingModel model) { var contest = this.Data.Contests.GetById(id); if (contest == null) { return this.HttpNotFound(); } contest.Title = model.Title; contest.Description = model.Description; contest.DateEnd = model.DateEnd; contest.MaximumParticipants = model.MaximumParticipants; this.Data.SaveChanges(); return this.RedirectToAction("Index", "HomeAdmin"); }
public ActionResult Create(ContestBindingModel model) { if (model != null && this.ModelState.IsValid) { var contest = Mapper.Map<Contest>(model); contest.DateCreated = DateTime.Now; contest.Creator = this.UserProfile; contest.NumberOfPrices = model.NumberOfPrices ?? 1; this.Data.Contests.Add(contest); this.Data.SaveChanges(); return this.RedirectToAction("Details", new { id = contest.Id }); } return this.View(model); }
public ActionResult EditContest(int id, ContestBindingModel model) { var contest = this.Data.Contests.GetById(id); if (contest == null) { return(this.HttpNotFound()); } contest.Title = model.Title; contest.Description = model.Description; contest.DateEnd = model.DateEnd; contest.MaximumParticipants = model.MaximumParticipants; this.Data.SaveChanges(); return(this.RedirectToAction("Index", "HomeAdmin")); }
public void CreateContest_WithInvalidData_ShouldReturnViewWithoutCreatingNewContest() { IList<Contest> contests = new List<Contest>(); var organizator = this.mocks .UserRepositoryMock .Object .All() .FirstOrDefault(); if (organizator == null) { Assert.Fail("Cannot perform test - no users available"); } this.mocks.ContestRepositoryMock.Setup(c => c.Add(It.IsAny<Contest>())) .Callback((Contest contest) => { contests.Add(contest); }); var mockContext = new Mock<IContestsData>(); mockContext.Setup(c => c.Contests) .Returns(this.mocks.ContestRepositoryMock.Object); mockContext.Setup(c => c.Categories) .Returns(this.mocks.CategoryRepositoryMock.Object); mockContext.Setup(c => c.Users) .Returns(this.mocks.UserRepositoryMock.Object); var mockIdProvider = new Mock<IUserIdProvider>(); mockIdProvider.Setup(ip => ip.GetUserId()) .Returns(organizator.Id); var contestController = new ContestController(mockContext.Object, mockIdProvider.Object); var newContest = new ContestBindingModel { Description = "Test contest", Category = -1 }; contestController.Create(newContest); mockContext.Verify(m => m.SaveChanges(), Times.Never); Assert.AreEqual(0, contests.Count); }
public ActionResult Edit(int id, ContestBindingModel model) { if (this.ModelState != null && this.ModelState.IsValid) { var contest = this.ContestsData.Contests.Find(id); //ICollection<User> voters = model.VotingType == VotingType.Close ? this.GetUsers(model.Voters) : new HashSet<User>(); //ICollection<User> participants = model.ParticipationType == ParticipationType.Close ? this.GetUsers(model.Participants) : new HashSet<User>(); contest.Title = model.Title; contest.Description = model.Description; contest.RewardType = model.RewardType; contest.DeadLine = model.DeadLine; contest.ParticipationType = model.ParticipationType; contest.VotingType = model.VotingType; //contest.Voters = voters; contest.WinnersCount = model.WinnersCount; contest.ParticipantsNumberDeadline = model.ParticipantsNumberDeadline; //contest.Participants = participants; contest.DeadLine = model.DeadLine; contest.CategoryId = model.Category; this.FillVoters(contest, model.Voters, model.VotingType); this.FillPacticipants(contest, model.Participants, model.ParticipationType); if (model.Upload != null && model.Upload.ContentLength > 0) { var wallpaperPaths = Helpers.UploadImages.UploadImage(model.Upload, true); var wallpaperUrl = Dropbox.Download(wallpaperPaths[0]); var wallpaperThumbUrl = Dropbox.Download(wallpaperPaths[1], "Thumbnails"); contest.WallpaperPath = wallpaperPaths[0]; contest.WallpaperUrl = wallpaperUrl; contest.WallpaperThumbPath = wallpaperPaths[1]; contest.WallpaperThumbUrl = wallpaperThumbUrl; } //this.ContestsData.Contests.Add(contest); this.ContestsData.SaveChanges(); this.AddToastMessage("Success", "Contest edited.", ToastType.Success); return this.RedirectToAction("Index", "Contests", routeValues: new { area = "Admin" }); } return this.View(model); }
public ActionResult Edit(int id, ContestBindingModel model) { var contest = this.Data.Contests.GetById(id); if (contest == null) { return(this.HttpNotFound()); } if (contest.CreatorId != this.UserProfile.Id) { return(new HttpUnauthorizedResult("Have to be contest owner to edit it.")); } contest.Title = model.Title; contest.Description = model.Description; contest.DateEnd = model.DateEnd; contest.MaximumParticipants = model.MaximumParticipants; this.Data.SaveChanges(); return(this.RedirectToAction("Details", new { id = contest.Id })); }
public ActionResult Edit(int id, ContestBindingModel model) { if (model != null && this.ModelState.IsValid) { var contest = this.ContestsData.Contests.Find(id); contest.Title = model.Title; contest.Description = model.Description; contest.RewardType = model.RewardType; contest.DeadLine = model.DeadLine; contest.ParticipationType = model.ParticipationType; contest.VotingType = model.VotingType; contest.WinnersCount = model.WinnersNumber; contest.ParticipantsNumberDeadline = model.ParticipantsNumberDeadline; contest.DeadLine = model.DeadLine; contest.CategoryId = model.Category; this.FillVoters(contest, model.Voters, model.VotingType); this.FillPacticipants(contest, model.Participants, model.ParticipationType); if (model.Upload != null && model.Upload.ContentLength > 0) { var wallpaperPaths = Helpers.UploadImages.UploadImage(model.Upload, true); var wallpaperUrl = Dropbox.Download(wallpaperPaths[0]); var wallpaperThumbUrl = Dropbox.Download(wallpaperPaths[1], "Thumbnails"); contest.WallpaperPath = wallpaperPaths[0]; contest.WallpaperUrl = wallpaperUrl; contest.WallpaperThumbPath = wallpaperPaths[1]; contest.WallpaperThumbUrl = wallpaperThumbUrl; } this.ContestsData.SaveChanges(); this.AddToastMessage("Success", "Contest edited.", ToastType.Success); return this.RedirectToAction("Details", "Users", routeValues: new {id = this.UserProfile.Id, area = ""}); } return this.View(model); }
public ActionResult Edit(int id, ContestBindingModel model) { var contest = this.Data.Contests.GetById(id); if (contest == null) { return this.HttpNotFound(); } if (contest.CreatorId != this.UserProfile.Id) { return new HttpUnauthorizedResult("Have to be contest owner to edit it."); } contest.Title = model.Title; contest.Description = model.Description; contest.DateEnd = model.DateEnd; contest.MaximumParticipants = model.MaximumParticipants; this.Data.SaveChanges(); return this.RedirectToAction("Details", new { id = contest.Id }); }
public void CreateContest_WithValidData_ShouldSuccessfullyAddTheContest() { IList<Contest> contests = new List<Contest>(); var organizator = this.mocks .UserRepositoryMock .Object .All() .FirstOrDefault(); if (organizator == null) { Assert.Fail("Cannot perform test - no users available"); } this.mocks.ContestRepositoryMock.Setup(c => c.Add(It.IsAny<Contest>())) .Callback((Contest contest) => { contest.Organizator = organizator; contests.Add(contest); }); var mockContext = new Mock<IContestsData>(); mockContext.Setup(c => c.Contests) .Returns(this.mocks.ContestRepositoryMock.Object); mockContext.Setup(c => c.Categories) .Returns(this.mocks.CategoryRepositoryMock.Object); mockContext.Setup(c => c.Users) .Returns(this.mocks.UserRepositoryMock.Object); var mockIdProvider = new Mock<IUserIdProvider>(); mockIdProvider.Setup(ip => ip.GetUserId()) .Returns(organizator.Id); var contestController = new ContestController(mockContext.Object, mockIdProvider.Object); string contestTitle = Guid.NewGuid().ToString(); var category = mockContext.Object .Categories .All() .FirstOrDefault(); if (category == null) { Assert.Fail("Cannot perform test - no categories available"); } var newContest = new ContestBindingModel { Title = contestTitle, Description = "Test contest", RewardType = RewardType.SingleWinner, VotingType = VotingType.Open, ParticipationType = ParticipationType.Open, DeadlineType = DeadlineType.ByParticipants, ParticipantsNumberDeadline = 5, Category = category.Id }; ActionResult response = contestController.Create(newContest); var contestFromRepo = contests.FirstOrDefault(c => c.Title == newContest.Title); if (contestFromRepo == null) { Assert.Fail(); } Assert.AreEqual(1, contests.Count); Assert.IsInstanceOfType(response, typeof(RedirectToRouteResult)); Assert.AreEqual(newContest.Description, contestFromRepo.Description); }