public ActionResult Create(CreateContestBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var loggedUserId = this.User.Identity.GetUserId();
                var newContest   = Mapper.Map <Contest>(model);
                newContest.StartDate = DateTime.Now;
                newContest.CreatorId = loggedUserId;

                if (model.RewardStrategy == RewardStrategy.One)
                {
                    newContest.NumberOfPrizes = 1;
                }

                var creator = this.Data.Users
                              .All()
                              .FirstOrDefault(u => u.Id == loggedUserId);

                this.Data.Contests.Add(newContest);
                newContest.Participants.Add(creator);
                this.Data.SaveChanges();

                return(this.RedirectToAction("Details", "Contest", new { newContest.Id }));
            }

            this.LoadCategories();

            return(this.View(model));
        }
示例#2
0
        public ActionResult CreateContest(CreateContestBindingModel model)
        {
            if (model == null)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = "Missing Data" }));
            }

            if (!this.ModelState.IsValid)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
            }

            try
            {
                int contestId = this._service.CreateContest(model, this.User.Identity.GetUserId());

                return(this.RedirectToAction("PreviewContest", new { id = contestId }));
            }
            catch (NotFoundException exception)
            {
                this.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(this.Json(new { ErrorMessage = exception.Message }));
            }
        }
示例#3
0
        public int CreateContest(CreateContestBindingModel model, string userId)
        {
            if (this.Data.RewardStrategies.Find(model.RewardStrategyId) == null)
            {
                throw new NotFoundException("Not existing reward strategy");
            }

            if (this.Data.ParticipationStrategies.Find(model.ParticipationStrategyId) == null)
            {
                throw new NotFoundException("Not existing participation strategy");
            }

            if (this.Data.VotingStrategies.Find(model.VotingStrategyId) == null)
            {
                throw new NotFoundException("Not existing voting strategy");
            }

            if (this.Data.DeadlineStrategies.Find(model.DeadlineStrategyId) == null)
            {
                throw new NotFoundException("Not existing deadline strategy");
            }

            var loggedUserId = userId;

            var contest = new Contest
            {
                Title                   = model.Title,
                Description             = model.Description,
                Status                  = ContestStatus.Active,
                RewardStrategyId        = model.RewardStrategyId,
                VotingStrategyId        = model.VotingStrategyId,
                ParticipationStrategyId = model.ParticipationStrategyId,
                DeadlineStrategyId      = model.DeadlineStrategyId,
                ParticipantsLimit       = model.ParticipantsLimit,
                TopNPlaces              = model.TopNPlaces,
                SubmissionDeadline      = model.SubmissionDeadline,
                IsOpenForSubmissions    = true,
                StartDate               = DateTime.Now,
                OrganizatorId           = loggedUserId,
            };

            this.Data.Contests.Add(contest);
            this.Data.SaveChanges();

            return(contest.Id);
        }
        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 Create(CreateContestBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var loggedUserId = this.User.Identity.GetUserId();
                var newContest = Mapper.Map<Contest>(model);
                newContest.StartDate = DateTime.Now;
                newContest.CreatorId = loggedUserId;

                if (model.RewardStrategy == RewardStrategy.One)
                {
                    newContest.NumberOfPrizes = 1;
                }

                var creator = this.Data.Users
                                    .All()
                                    .FirstOrDefault(u => u.Id == loggedUserId);

                this.Data.Contests.Add(newContest);
                newContest.Participants.Add(creator);
                this.Data.SaveChanges();

                return this.RedirectToAction("Details", "Contest", new { newContest.Id });
            }

            this.LoadCategories();

            return this.View(model);
        }
        public ActionResult CreateContest(CreateContestBindingModel newContest)
        {
            if (newContest.DeadlineStrategy == DeadlineStrategy.ByTime)
            {
                if (newContest.Deadline > DateTime.Now)
                {
                    if (ModelState.IsValid)
                    {

                        var currentUserId = User.Identity.GetUserId();
                        var currentUser = this.Data.Users.Find(currentUserId);
                        var contest = new Contest()
                        {
                            Title = newContest.Title,
                            Description = newContest.Description,
                            CreatedOn = newContest.CreatedOn,
                            OwnerId = currentUserId,
                            ParticipationStrategy = newContest.ParticipationStrategy,
                            NumberOfParticipants = newContest.NumberOfParticipants,
                            DeadlineStrategy = newContest.DeadlineStrategy,
                            Deadline = newContest.Deadline,
                            PrizeCount = newContest.PrizeCount,
                            PrizeValues = newContest.PrizeValues*2,
                            RewardStrategy = newContest.RewardStrategy,
                            VotingStrategy = newContest.VotingStrategy
                        };
                        currentUser.Coints = currentUser.Coints - contest.PrizeValues/2;
                        if (currentUser.Coints < 0)
                        {
                            this.TempData["message-create-contest-coints"] = "You do NOT have enough Coints!";
                        }
                        else
                        {
                            this.Data.Contests.Add(contest);
                            //currentUser.Contests.Add(contest);
                            this.Data.Contests.SaveChanges();
                            this.TempData["message-create-contest-success"] = "You successfully created new contest!";
                            return RedirectToAction("ContestDetails", "Contests", new {id = contest.Id});
                        }
                    }
                }
            }
            else if(newContest.NumberOfParticipants>0)
            {
                 var currentUserId = User.Identity.GetUserId();
                        var currentUser = this.Data.Users.Find(currentUserId);
                        var contest = new Contest()
                        {
                            Title = newContest.Title,
                            Description = newContest.Description,
                            CreatedOn = newContest.CreatedOn,
                            OwnerId = currentUserId,
                            ParticipationStrategy = newContest.ParticipationStrategy,
                            NumberOfParticipants = newContest.NumberOfParticipants,
                            DeadlineStrategy = newContest.DeadlineStrategy,
                            Deadline = newContest.Deadline,
                            PrizeCount = newContest.PrizeCount,
                            PrizeValues = newContest.PrizeValues*2,
                            RewardStrategy = newContest.RewardStrategy,
                            VotingStrategy = newContest.VotingStrategy
                        };
                        currentUser.Coints = currentUser.Coints - contest.PrizeValues/2;
                if (currentUser.Coints < 0)
                {
                    this.TempData["message-create-contest-coints"] = "You do NOT have enough Coints!";
                }
                else
                {
                    this.Data.Contests.Add(contest);
                    currentUser.Contests.Add(contest);
                    this.Data.Contests.SaveChanges();
                    this.TempData["message-create-contest-success"] = "You successfully created new contest!";
                    return RedirectToAction("ContestDetails", "Contests", new {id = contest.Id});
                }
            }

            return this.View(newContest);
        }
 private ActionResult AddInvalidContest()
 {
     var contest = new CreateContestBindingModel
     {
         VotingType = VotingType.Open,
         Description = "Description 1",
         Prizes = new HashSet<Prize>(),
         DeadlineType = DeadlineType.EndDate,
         ParticipationType = ParticipationType.Open,
         StartDate = DateTime.Now,
     };
     return this.contestsController.Create(contest);
 }
        private void AddContestWithJury()
        {
            var contest = new CreateContestBindingModel
            {
                Title = "Title 1",
                VotingType = VotingType.Closed,
                Description = "Description 1",
                Prizes = new HashSet<Prize>(),
                DeadlineType = DeadlineType.EndDate,
                ParticipationType = ParticipationType.Open,
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                ParticipationLimit = 1,
                Thumbnail = "Thumbnail",
            };

            this.contestsController.Create(contest);

            this.data.Contests.Find(0).Jury.Members.Add(
                    new User()
                    {
                        UserName = "******",
                        Id = "123"
                    }
                );

            this.data.SaveChanges();
        }
 private ActionResult AddContest()
 {
     var contest = new CreateContestBindingModel
     {
         Title = "Title 1",
         VotingType = VotingType.Open,
         Description = "Description 1",
         Prizes = new HashSet<Prize>(),
         DeadlineType = DeadlineType.EndDate,
         ParticipationType = ParticipationType.Open,
         StartDate = DateTime.Now,
         EndDate = DateTime.Now,
         ParticipationLimit = 1,
         Thumbnail = "Thumbnail",
     };
     return this.contestsController.Create(contest);
 }