Пример #1
0
        public async Task <ContestEditDto> UpdateContestAsync(int id, ContestEditDto dto)
        {
            await EnsureContestExistsAsync(id);
            await ValidateContestEditDtoAsync(dto);

            var contest = await Context.Contests.FindAsync(id);

            contest.Title                = dto.Title;
            contest.Description          = dto.Description;
            contest.IsPublic             = dto.IsPublic.GetValueOrDefault();
            contest.Mode                 = dto.Mode.GetValueOrDefault();
            contest.BeginTime            = dto.BeginTime;
            contest.EndTime              = dto.EndTime;
            contest.HasScoreBonus        = dto.HasScoreBonus.GetValueOrDefault();
            contest.ScoreBonusTime       = dto.ScoreBonusTime;
            contest.ScoreBonusPercentage = dto.ScoreBonusPercentage;
            contest.HasScoreDecay        = dto.HasScoreDecay.GetValueOrDefault();
            contest.IsScoreDecayLinear   = dto.IsScoreDecayLinear;
            contest.ScoreDecayTime       = dto.ScoreDecayTime;
            contest.ScoreDecayPercentage = dto.ScoreDecayPercentage;
            Context.Contests.Update(contest);
            await Context.SaveChangesAsync();

            await Context.Entry(contest).Collection(c => c.Problems).LoadAsync();

            foreach (var problem in contest.Problems)
            {
                await _problemStatisticsService.InvalidStatisticsAsync(problem.Id);
            }

            await LogInformation($"UpdateContest Id={contest.Id} Title={contest.Title} " +
                                 $"IsPublic={contest.IsPublic} Mode={contest.Mode}");

            return(new ContestEditDto(contest));
        }
Пример #2
0
        public async Task <ContestEditDto> CreateContestAsync(ContestEditDto dto)
        {
            await ValidateContestEditDtoAsync(dto);

            var contest = new Contest
            {
                Title                = dto.Title,
                Description          = dto.Description,
                IsPublic             = dto.IsPublic.GetValueOrDefault(),
                Mode                 = dto.Mode.GetValueOrDefault(),
                BeginTime            = dto.BeginTime,
                EndTime              = dto.EndTime,
                HasScoreBonus        = dto.HasScoreBonus.GetValueOrDefault(),
                ScoreBonusTime       = dto.ScoreBonusTime,
                ScoreBonusPercentage = dto.ScoreBonusPercentage,
                HasScoreDecay        = dto.HasScoreDecay.GetValueOrDefault(),
                IsScoreDecayLinear   = dto.IsScoreDecayLinear,
                ScoreDecayTime       = dto.ScoreDecayTime,
                ScoreDecayPercentage = dto.ScoreDecayPercentage
            };
            await Context.Contests.AddAsync(contest);

            await Context.SaveChangesAsync();

            await LogInformation($"UpdateContest Id={contest.Id} Title={contest.Title} " +
                                 $"IsPublic={contest.IsPublic} Mode={contest.Mode}");

            return(new ContestEditDto(contest));
        }
Пример #3
0
 public async Task <ActionResult <ContestEditDto> > CreateContest(ContestEditDto dto)
 {
     try
     {
         return(Created(nameof(ViewContest), await _service.CreateContestAsync(dto)));
     }
     catch (ValidationException e)
     {
         return(BadRequest(e.Message));
     }
 }
Пример #4
0
 public async Task <ActionResult <ContestEditDto> > UpdateContest(int id, ContestEditDto dto)
 {
     try
     {
         return(Ok(await _service.UpdateContestAsync(id, dto)));
     }
     catch (NotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (ValidationException e)
     {
         return(BadRequest(e.Message));
     }
 }
Пример #5
0
        private Task ValidateContestEditDtoAsync(ContestEditDto dto)
        {
            if (string.IsNullOrEmpty(dto.Title))
            {
                throw new ValidationException("Title cannot be empty.");
            }

            if (!Enum.IsDefined(typeof(ContestMode), dto.Mode.GetValueOrDefault()))
            {
                throw new ValidationException("Invalid contest mode.");
            }

            if (dto.BeginTime >= dto.EndTime)
            {
                throw new ValidationException("Invalid begin and end time.");
            }

            if (dto.HasScoreBonus.GetValueOrDefault(false))
            {
                if (!dto.ScoreBonusTime.HasValue)
                {
                    throw new ValidationException("Score bonus time cannot be null.");
                }
                else if (dto.ScoreBonusTime.Value < dto.BeginTime)
                {
                    throw new ValidationException("Score bonus time cannot be before contest start time.");
                }
                else if (dto.ScoreBonusTime.Value > dto.EndTime)
                {
                    throw new ValidationException("Score bonus time cannot be after contest end time.");
                }

                switch (dto.ScoreBonusPercentage)
                {
                case null:
                    throw new ValidationException("Score bonus percentage cannot be null.");

                case < 100:
                    throw new ValidationException("Score bonus percentage cannot be less than 100.");
                }
            }

            if (dto.HasScoreDecay.GetValueOrDefault(false))
            {
                if (!dto.ScoreDecayTime.HasValue)
                {
                    throw new ValidationException("Score decay time cannot be null.");
                }
                else if (dto.ScoreDecayTime.Value < dto.BeginTime)
                {
                    throw new ValidationException("Score decay time cannot be before contest start time.");
                }
                else if (dto.ScoreDecayTime.Value > dto.EndTime)
                {
                    throw new ValidationException("Score decay time cannot be after contest end time.");
                }

                if (!dto.IsScoreDecayLinear.HasValue)
                {
                    throw new ValidationException("Score decay linear cannot be null.");
                }
                switch (dto.ScoreDecayPercentage)
                {
                case null:
                    throw new ValidationException("Score decay percentage cannot be null.");

                case < 0:
                    throw new ValidationException("Score decay percentage cannot be negative.");

                case > 100:
                    throw new ValidationException("Score decay percentage cannot be more than 100.");
                }
            }

            return(Task.CompletedTask);
        }