Ejemplo n.º 1
0
        public async Task <StepNavigation> Handle(GetStepNavigationByIdQuery request,
                                                  CancellationToken cancellationToken)
        {
            var step = await _readStepRepository.GetOneStepNavigationById(request.StepId);

            if (step is null)
            {
                throw new ApplicationException($"Step with id {request.StepId} not found");
            }
            return(step);
        }
        public async Task <string> Handle(CreateParticipationCommand request, CancellationToken cancellationToken)
        {
            var tournamentNavigation =
                await _readTournamentRepository.GetOneTournamentNavigationById(request.TournamentId);

            if (tournamentNavigation is null)
            {
                throw new NotFoundException(request.TournamentId.ToString(), "Tournament");
            }
            var stepNavigation = await _readStepRepository.GetOneStepNavigationById(request.StepId);

            if (stepNavigation is null)
            {
                throw new NotFoundException(request.StepId.ToString(), "Step");
            }
            var teamNavigation = await _readTeamRepository
                                 .GetOneTeamNavigationByIdAsync(request.TeamId);

            if (teamNavigation is null)
            {
                throw new NotFoundException(request.TeamId.ToString(), "Team");
            }
            if (await _readParticipationRepository.ParticipationExistsByTournamentStepTeamIds(request.TournamentId,
                                                                                              request.StepId, request.TeamId))
            {
                throw new ApplicationException(
                          $"Participation for team : {request.TeamId} on tournament : {request.TournamentId} on step : {request.StepId} already exists");
            }

            if (!await CanAddParticipation(request.TeamId, request.TournamentId, request.StepId))
            {
                throw new ApplicationException("Participation cannot be created, finish previous first");
            }

            var team = new TeamEntity(new TeamId(request.TeamId),
                                      teamNavigation.MembersIds.Select(memberId => new UserId(memberId)).ToList());
            var step = new StepEntity(new StepId(request.StepId),
                                      stepNavigation.TournamentsIds.Select(tournamentId => new TournamentId(tournamentId)).ToList());
            var tournament =
                new TournamentEntity(new TournamentId(request.TournamentId), tournamentNavigation.IsPublished);
            var participation = ParticipationAggregate.CreateNew(await _participationRepository.NextIdAsync(), team,
                                                                 tournament, step, _timeService.Now());

            await _participationRepository.SetAsync(participation);

            return(participation.Id.ToString());
        }
        public async Task <string> Handle(RunParticipationCommand request,
                                          CancellationToken cancellationToken)
        {
            var participation =
                await _participationsSessionsRepository.FindByIdAsync(new ParticipationId(request.ParticipationId));

            if (participation is null)
            {
                throw new NotFoundException(request.ParticipationId.ToString(), "ParticipationSession");
            }

            participation.ValidateProcessStart(_currentUserService.UserId);

            var step = await _readStepRepository.GetOneStepNavigationById(participation.StepEntity.Id.Value);

            if (step is null)
            {
                throw new NotFoundException(participation.StepEntity.Id.Value.ToString(), "ParticipationSessionStep");
            }

            var language = await _readProgrammingLanguageRepository.GetOneLanguageNavigationByIdAsync(step.LanguageId);

            if (language is null)
            {
                throw new NotFoundException(step.LanguageId.ToString(), "ParticipationSessionStepLanguage");
            }

            var tests = await _readTestRepository.GetAllTestNavigationByStepId(participation.StepEntity.Id.Value);

            var runTestDto = new RunParticipationTestsDto(
                participation.Id.Value,
                language.Name,
                step.HeaderCode,
                tests.Select(t => new RunParticipationTestsDto.Test(t.Id, t.Name, t.OutputValidator, t.InputGenerator))
                .ToList(),
                participation.Functions
                .Where(f => f.Order is not null)
                .Select(f => new RunParticipationTestsDto.Function(f.Id.Value, f.Code, f.Order !.Value))
                .ToList()
                );

            participation.StartProcess(_timeService.Now());
            _participationExecutionService.Dispatch(runTestDto);
            await _participationsSessionsRepository.SetAsync(participation);

            return(await Task.FromResult(request.ParticipationId.ToString()));
        }