Beispiel #1
0
        public bool AddRewards(int id, CreateRewardsBindingModel model, string userId)
        {
            var contest = this.Data.Contests.Find(id);

            if (contest == null)
            {
                throw new NotFoundException("Contest not found");
            }

            if (contest.Status != ContestStatus.Active)
            {
                throw new BadRequestException("Cannot add reward to inactive contest.");
            }

            for (int i = 0; i < model.Name.Length; i++)
            {
                if (model.Place[i] < 1 || (contest.TopNPlaces != null && model.Place[i] > contest.TopNPlaces))
                {
                    throw new BadRequestException("Reward for unknown place");
                }

                contest.Rewards.Add(new Reward()
                {
                    ContestId   = contest.Id,
                    Name        = model.Name[i],
                    Description = model.Description[i],
                    Place       = model.Place[i],
                    ImageUrl    = model.ImageUrl[i]
                });
            }

            this.Data.SaveChanges();

            return(true);
        }
Beispiel #2
0
        public ActionResult AddRewards(int id, CreateRewardsBindingModel model)
        {
            if (model == null)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = "Missing data" }));
            }

            try
            {
                this._service.AddRewards(id, model, this.User.Identity.GetUserId());

                return(this.RedirectToAction("PreviewContest", new { id = id }));
            }
            catch (NotFoundException exception)
            {
                return(this.HttpNotFound(exception.Message));
            }
            catch (BadRequestException exception)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = exception.Message }));
            }
        }