private void SynchronizeFunctions(ParticipationAggregate aggregate, Participation participation)
        {
            var removedFunctions = GetRemovedFunctions(aggregate, participation);

            removedFunctions.ForEach(function => function.IsDeleted = true);
            var currentFunctions = GetCurrentFunctions(participation);
            var newFunctions     = GetNewFunctions(aggregate, currentFunctions);

            currentFunctions.ForEach(currentFunction =>
            {
                var function          = aggregate.Functions.First(f => f.Id.Value == currentFunction.Id);
                currentFunction.Code  = function.Code;
                currentFunction.Order = function.Order;
                var userFunction      =
                    currentFunction.UserFunctions.FirstOrDefault(uF => uF.UserId == function.UserId.Value);
                if (userFunction is null)
                {
                    currentFunction.UserFunctions.Add(new UserFunction
                    {
                        UserId = function.UserId.Value, LastModificationDate = function.LastModificationDate
                    });
                }
                else
                {
                    userFunction.LastModificationDate = function.LastModificationDate;
                }
            });
            newFunctions.ForEach(function => participation.Functions.Add(function));
        }
 public void DeleteParticipationDirectory(ParticipationAggregate participation)
 {
     if (DeleteParticipationDirectoryWithoutDeleteFileFlag(participation, out var templateDirectoryPath))
     {
         WriteDeleteFlagFile(templateDirectoryPath);
     }
 }
        public async Task <ParticipationId> SetAsync(ParticipationAggregate aggregate)
        {
            var participation = await ToModel(aggregate);

            _context.Participations.Upsert(participation);
            await _context.SaveChangesAsync();

            return(new ParticipationId(participation.Id));
        }
Beispiel #4
0
 public Task CleanParticipationExecution(ParticipationAggregate participation)
 {
     Logger.LogInformation(
         "Template deletion started for participation : {ParticipationId} on language : {Language} ",
         participation.Id, participation.Language);
     _directoryService.DeleteParticipationDirectory(participation);
     Logger.LogInformation(
         "Template deletion ended for participation : {ParticipationId} on language : {Language} ",
         participation.Id, participation.Language);
     return(Task.CompletedTask);
 }
Beispiel #5
0
        public async Task PrepareParticipationExecution(ParticipationAggregate participation)
        {
            Logger.LogInformation(
                "Template preparation started for participation : {ParticipationId} on language : {Language} ",
                participation.Id, participation.Language);
            await _directoryService.GetTemplateDirectoryByParticipation(participation).ToTask();

            Logger.LogInformation(
                "Template preparation ended for participation : {ParticipationId} on language : {Language} ",
                participation.Id, participation.Language);
        }
 private static ParticipationAggregate ToAggregate(Participation participation)
 {
     return(ParticipationAggregate.Restore(
                new ParticipationId(participation.Id),
                ToTeamEntity(participation.Team),
                ToTournamentEntity(participation.Tournament),
                ToStepEntity(participation.Step),
                participation.StartDate,
                participation.EndDate,
                participation.CalculatedScore,
                participation.Functions.Where(f => !f.IsDeleted).Select(ToFunctionEntity).ToList()
                ));
 }
Beispiel #7
0
 private static ParticipationSessionAggregate ToAggregate(ParticipationAggregate participation,
                                                          IList <TestNavigation> tests) =>
 ParticipationSessionAggregate.Restore(
     participation.Id,
     ToTeamSessionEntity(participation.Team),
     participation.TournamentEntity,
     ToStepSessionEntity(participation.StepEntity, tests),
     participation.StartDate,
     participation.EndDate,
     participation.CalculatedScore,
     false,
     participation.Functions.ToList()
     );
Beispiel #8
0
 public void WriteParticipation(ParticipationAggregate participation)
 {
     Logger.LogInformation(
         "Tests writing started for participation : {ParticipationId} on language : {Language} ",
         participation.Id, participation.Language);
     TemplateDirectoryPath = _directoryService.GetTemplateDirectoryByParticipation(participation);
     FileStream.WriteLine(participation.GetExecutableCode());
     _fileStream?.Close();
     _fileStream = null;
     Logger.LogInformation(
         "Tests writing ended for participation : {ParticipationId} on language : {Language} ",
         participation.Id, participation.Language);
 }
        private bool DeleteParticipationDirectoryWithoutDeleteFileFlag(ParticipationAggregate participation,
                                                                       out string templateDirectoryPath)
        {
            var dir = GetTemplateExtractPathByParticipation(participation);

            templateDirectoryPath = dir;
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
                return(true);
            }

            return(false);
        }
        private async Task <Participation> ToModel(ParticipationAggregate aggregate)
        {
            var participation = await GetModel(aggregate.Id.Value);

            SynchronizeFunctions(aggregate, participation);
            participation.Step       = _context.Steps.First(s => !s.IsDeleted && s.Id == aggregate.StepEntity.Id.Value);
            participation.Tournament =
                _context.Tournaments.First(t => !t.IsDeleted && t.Id == aggregate.TournamentEntity.Id.Value);
            participation.Team            = _context.Teams.First(t => !t.IsDeleted && t.Id == aggregate.Team.Id.Value);
            participation.CalculatedScore = aggregate.CalculatedScore;
            participation.EndDate         = aggregate.EndDate;
            participation.StartDate       = aggregate.StartDate;
            return(participation);
        }
 private List <Function> GetNewFunctions(ParticipationAggregate aggregate, List <Function> currentFunctions)
 {
     return(aggregate.Functions
            .Where(function => currentFunctions.All(f => f.Id != function.Id.Value))
            .Select(function => new Function
     {
         Id = function.Id.Value, Code = function.Code, Order = function.Order,
         UserFunctions = new List <UserFunction>
         {
             new()
             {
                 FunctionId = function.Id.Value, UserId = function.UserId.Value,
                 LastModificationDate = function.LastModificationDate
             }
         }
     })
        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());
        }
Beispiel #13
0
 protected virtual Process GetProcess(ParticipationAggregate partipation)
 {
     return(new()
     {
         StartInfo =
         {
             FileName               = ProcessName,
             RedirectStandardInput  = true,
             RedirectStandardOutput = true,
             RedirectStandardError  = true,
             StandardErrorEncoding  = Encoding.UTF8,
             CreateNoWindow         = false,
             UseShellExecute        = false,
             Arguments              = ProcessArguments,
             WorkingDirectory       = TemplateDirectoryPath.FullName
         },
         EnableRaisingEvents = true
     });
        public FileInfo?GetTemplateDirectoryByParticipation(ParticipationAggregate participation)
        {
            var zipPath    = GetZipPathByParticipation(participation);
            var extractDir = GetTemplateExtractPathByParticipation(participation);

            if (Directory.Exists(extractDir))
            {
                return(new FileInfo(extractDir));
            }

            ClearDeleteFileFlag(extractDir);
            ZipFile.ExtractToDirectory(zipPath, extractDir);
            if (ClearDeleteFileFlag(extractDir))
            {
                DeleteParticipationDirectoryWithoutDeleteFileFlag(participation, out var templatePath);
                return(null);
            }

            return(new FileInfo(extractDir));
        }
Beispiel #15
0
 protected virtual Task PreExecuteParticipation(ParticipationAggregate participation)
 {
     return(Task.CompletedTask);
 }
 private string GetTemplateExtractPathByParticipation(ParticipationAggregate participation)
 {
     return(Path.GetFullPath(Path.Join(TemplateExtractParentDirectoryPath, participation.Id.Value.ToString())));
 }
Beispiel #17
0
 public async Task WriteAndExecuteParticipation(ParticipationAggregate participation)
 {
     WriteParticipation(participation);
     await ExecuteParticipation(participation);
 }
 private string GetZipPathByParticipation(ParticipationAggregate participation)
 {
     return(Path.GetFullPath(Path.Join(TemplateZipParentDirectoryPath,
                                       GetTemplateSettingsByLanguage(participation.Language).Name)));
 }