Пример #1
0
        public IHttpActionResult ContestCreate([FromBody] ContestCreationFormData data)
        {
            if (!user_service.IsAuthorizedToCreateContest())
            {
                return(Unauthorized());
            }

            if (RequestUtility.IsPreFlightRequest(Request))
            {
                return(Ok());
            }

            var validation_oresult = data.Validate();

            if (!validation_oresult.IsValid)
            {
                return(new BadHttpRequest(validation_oresult.ErrorMessages));
            }

            try{
                // todo replace 1 with current user id
                contest_repository.CreateContest(1, data);
            }
            catch (Exception e) {
                return(InternalServerError(e));
            }
            return(Ok());
        }
Пример #2
0
        public void UpdateContest(int contest_id, ContestCreationFormData data)
        {
            List <ContestProblem> contest_problems = new List <ContestProblem>();

            int order = 0;

            foreach (int problem_id in data.Problems)
            {
                var problem = context.Problems.Find(problem_id);

                if (problem == null)
                {
                    throw new InvalidOperationException("One or more specified problem IDs were not found");
                }

                contest_problems.Add(new ContestProblem()
                {
                    Problem = problem,
                    Order   = order,
                });

                order++;
            }

            // delete existing contest problems
            foreach (var old_problem in context.ContestProblems.Where(x => x.Contest.Id == contest_id))
            {
                context.ContestProblems.Remove(old_problem);
            }

            Contest contest = context.Contests.Find(contest_id);

            if (contest == null)
            {
                throw new ObjectNotFoundException("Contest with specified ID not found");
            }

            contest.Title       = data.Title;
            contest.Description = data.Description;
            contest.StartDate   = data.StartDate;
            contest.EndDate     = data.EndDate;
            contest.Problems    = contest_problems;
            contest.IsPublic    = data.IsPublic;
            contest.Password    = data.Password;

            context.SaveChanges();
        }
Пример #3
0
        public void CreateContest(int user_id, ContestCreationFormData data)
        {
            List <ContestProblem> contest_problems = new List <ContestProblem>();
            var creator = context.Users.Find(user_id);

            if (creator == null)
            {
                throw new ObjectNotFoundException("User with specified not found");
            }

            int order = 0;

            foreach (int problem_id in data.Problems)
            {
                var problem = context.Problems.Find(problem_id);

                if (problem == null)
                {
                    throw new InvalidOperationException("One or more specified problem IDs were not found");
                }

                contest_problems.Add(new ContestProblem()
                {
                    Problem = problem,
                    Order   = order,
                });

                order++;
            }

            Contest contest = new Contest()
            {
                Title       = data.Title,
                Description = data.Description,
                StartDate   = data.StartDate,
                EndDate     = data.EndDate,
                Problems    = contest_problems,
                Creator     = creator,
                IsPublic    = data.IsPublic,
                Password    = data.Password,
            };

            context.Contests.Add(contest);
            context.SaveChanges();
        }