public async Task <ProblemEditDto> UpdateProblemAsync(int id, ProblemEditDto dto) { await EnsureProblemExists(id); await ValidateProblemEditDtoAsync(dto); var problem = await Context.Problems.FindAsync(id); problem.ContestId = dto.ContestId.GetValueOrDefault(); problem.Title = dto.Title; problem.Type = dto.Type; problem.Description = dto.Description; problem.InputFormat = dto.InputFormat; problem.OutputFormat = dto.OutputFormat; problem.FootNote = dto.FootNote; problem.TimeLimit = dto.TimeLimit.GetValueOrDefault(); problem.MemoryLimit = dto.MemoryLimit.GetValueOrDefault(); problem.HasSpecialJudge = dto.HasSpecialJudge; problem.SpecialJudgeProgram = dto.HasSpecialJudge ? dto.SpecialJudgeProgram : null; problem.HasHacking = false; problem.SampleCases = dto.SampleCases; Context.Problems.Update(problem); await Context.SaveChangesAsync(); await _statistics.InvalidStatisticsAsync(problem.Id); await LogInformation($"UpdateProblem Id={problem.Id} Contest={problem.ContestId} Title={problem.Id} " + $"HasSpecialJudge={problem.HasSpecialJudge} HasHacking={problem.HasHacking}"); return(new ProblemEditDto(problem)); }
public async Task <ProblemEditDto> CreateProblemAsync(ProblemEditDto dto) { await ValidateProblemEditDtoAsync(dto); var problem = new Problem { ContestId = dto.ContestId.GetValueOrDefault(), Title = dto.Title, Description = dto.Description, InputFormat = dto.InputFormat, OutputFormat = dto.OutputFormat, FootNote = dto.FootNote, TimeLimit = dto.TimeLimit.GetValueOrDefault(), MemoryLimit = dto.MemoryLimit.GetValueOrDefault(), HasSpecialJudge = dto.HasSpecialJudge, SpecialJudgeProgram = dto.HasSpecialJudge ? dto.SpecialJudgeProgram : null, HasHacking = false, SampleCases = dto.SampleCases, TestCases = new List <TestCase>() }; await Context.Problems.AddAsync(problem); await Context.SaveChangesAsync(); await LogInformation($"CreateProblem Id={problem.Id} Contest={problem.ContestId} Title={problem.Id} " + $"HasSpecialJudge={problem.HasSpecialJudge} HasHacking={problem.HasHacking}"); return(new ProblemEditDto(problem)); }
public async Task <ActionResult <ProblemEditDto> > CreateProblem(ProblemEditDto dto) { try { return(Created(nameof(ViewProblem), await _service.CreateProblemAsync(dto))); } catch (ValidationException e) { return(BadRequest(e.Message)); } }
private async Task ValidateProblemEditDtoAsync(ProblemEditDto dto) { var contest = await Context.Contests.FindAsync(dto.ContestId.GetValueOrDefault()); if (contest == null) { throw new ValidationException("Invalid contest ID."); } if (string.IsNullOrEmpty(dto.Title)) { throw new ValidationException("Invalid problem title."); } if (string.IsNullOrEmpty(dto.Description)) { throw new ValidationException("Invalid problem description."); } if (dto.Type == ProblemType.Ordinary) { if (string.IsNullOrEmpty(dto.InputFormat) || string.IsNullOrEmpty(dto.OutputFormat)) { throw new ValidationException("Invalid problem input or output format."); } if (!dto.TimeLimit.HasValue) { throw new ValidationException("Time limit cannot be null."); } if (!dto.MemoryLimit.HasValue) { throw new ValidationException("Memory limit cannot be null."); } if (dto.HasSpecialJudge && dto.SpecialJudgeProgram == null) { throw new ValidationException("Special judge program cannot be null."); } if (dto.HasHacking) { throw new NotImplementedException("Hacking and Special Judge is not implemented."); } if (dto.SampleCases.Count == 0) { throw new ValidationException("At lease one sample case is required."); } } }
public async Task <ActionResult <ProblemEditDto> > UpdateProblem(int id, ProblemEditDto dto) { try { return(Ok(await _service.UpdateProblemAsync(id, dto))); } catch (NotFoundException e) { return(NotFound(e.Message)); } catch (ValidationException e) { return(BadRequest(e.Message)); } }