public ActionResult CreatePrize(CreatePrizeBindingModel model) { if (model == null) { ModelState.AddModelError("", "Incorect enered parameter"); } if (!ModelState.IsValid) { ModelState.AddModelError("", "Incorect enered parameter"); } Prize prize = new Prize() { ContestId = model.ContestId, Description = model.Description, Name = model.Name, ForPlace = model.Place }; this.Data.Prizes.Add(prize); this.Data.SaveChanges(); return Json("Success", JsonRequestBehavior.AllowGet); }
public ActionResult Create(CreateContestBindingModel model) { if (!this.ModelState.IsValid) { return this.View(model); } var loggedUserId = this.User.Identity.GetUserId(); string thumbnailImageData = ""; try { var image = PictureUtills.CreateImageFromBase64(model.Thumbnail); var thumbnail = PictureUtills.CreateThumbnailFromImage(image, 316); thumbnailImageData = PictureUtills.ConvertImageToBase64(thumbnail); } catch (Exception ex) { thumbnailImageData = model.Thumbnail; } var contest = new Contest() { Title = model.Title, Description = model.Description, StartDate = model.StartDate, EndDate = model.EndDate, OwnerId = loggedUserId, VotingType = model.VotingType, ParticipationType = model.ParticipationType, DeadlineType = model.DeadlineType, Picture = model.Thumbnail, Thumbnail = thumbnailImageData, Status = model.StartDate < DateTime.Now ? ContestStatus.Active : ContestStatus.Inactive, }; if (contest.DeadlineType == DeadlineType.ParticipationLimit) { contest.ParticipationLimit = model.ParticipationLimit; } this.Data.Contests.Add(contest); this.Data.SaveChanges(); if (model.VotingType == VotingType.Closed) { contest.Jury = new VotingCommittee(); contest.Jury.ContestId = contest.Id; } foreach (var prize in model.Prizes) { var dbPrize = new Prize() { Name = prize.Name, Description = prize.Description, ContestId = contest.Id, }; contest.Prizes.Add(dbPrize); } this.Data.SaveChanges(); var members = this.Data.Users.All().ToList(); foreach (var member in members) { var notification = new Notification() { RecipientId = member.Id, Content = string.Format( Messages.NewContest, contest.Title, contest.StartDate), CreatedOn = DateTime.Now, IsRead = false, }; this.Data.Notifications.Add(notification); } this.Data.SaveChanges(); var hubContext = GlobalHost.ConnectionManager.GetHubContext<ContestsHub>(); hubContext.Clients.All.receiveMessage(contest.Id); return this.RedirectToAction("Contests", "Me"); }
public ActionResult CreatePrizes(CreatePrizesBindingModel prizesModel, int contestId, int numOfWinnersRequired, int leftForAdding) { if (prizesModel != null && this.ModelState.IsValid) { var prize = new Prize() { ContestId = contestId, PrizeName = prizesModel.PrizeName, Position = leftForAdding }; this.Data.Prizes.Add(prize); this.Data.SaveChanges(); } int numOfWinnersInDb = this.Data.Prizes.All().Where(p => p.ContestId == contestId).Count(); if (numOfWinnersInDb < numOfWinnersRequired) { //If all winners prizes are'nt added to DB leftForAdding = numOfWinnersRequired - numOfWinnersInDb; this.ViewBag.leftForAdding = leftForAdding; return this.RedirectToAction("CreatePrizes", "Contest", new { contestId = contestId, numOfWinnersRequired = numOfWinnersRequired, leftForAdding = leftForAdding }); } else { //If all winners prizes are added to DB var currentContest = this.Data.Contests.All().FirstOrDefault(c => c.Id == contestId); currentContest.Flag = Flag.Active; this.Data.SaveChanges(); var html = HttpContext.Server.HtmlEncode(currentContest.Name); SendContestCreatedNotification(string.Format("New contest \"{0}\" has been created.", html)); return this.RedirectToAction("Details", "Contest", new { id = contestId }); } }