Example #1
0
        public async Task <string> Handle(SetStepTestsCommand request, CancellationToken cancellationToken)
        {
            var step = await _stepEditionRepository.FindByIdAsync(new StepId(request.StepId));

            if (step is null)
            {
                throw new NotFoundException(request.StepId.ToString(), "Step");
            }
            var tests = request.Tests
                        .Where(t => t.Id is not null)
                        .Select(t =>
                                new TestEntity(new TestId(t.Id !.Value), t.Name, t.OutputValidator, t.InputGenerator, t.Score))
                        .ToList();
            var newTests = request.Tests.Where(t => t.Id is null);

            tests.ForEach(existingTest =>
            {
                if (!step.Tests.Contains(existingTest))
                {
                    throw new NotFoundException(existingTest.Id.ToString(), "Test");
                }
            });
            foreach (var newTest in newTests)
            {
                tests.Add(new TestEntity(await _stepEditionRepository.GetNextTestIdAsync(), newTest.Name,
                                         newTest.OutputValidator,
                                         newTest.InputGenerator, newTest.Score));
            }
            step.SetTests(tests);
            return((await _stepEditionRepository.SetAsync(step)).ToString());
        }
        public async Task <string> Handle(AddTestCommand request, CancellationToken cancellationToken)
        {
            var step = await _stepEditionRepository.FindByIdAsync(new StepId(request.StepId));

            if (step is null)
            {
                throw new NotFoundException(request.StepId.ToString(), nameof(StepEditionAggregate));
            }
            step.ValidateEdition();
            var newTest = new TestEntity(await _stepEditionRepository.GetNextTestIdAsync(), request.Name,
                                         request.OutputValidator,
                                         request.InputGenerator, request.Score);

            step.AddTest(newTest);
            await _stepEditionRepository.SetAsync(step);

            return(newTest.Id.ToString());
        }