public async Task AddTestInputOutputExample(Guid testId, AddTestInputOutputExampleBindingModel model) { var test = _testsRepository.Read <TestReadModel>().Single(t => t.Id == testId); if (model.IsDifficult) { model.Name = $"Difficult {test.IoExamples.Count(ioe => ioe.Name.StartsWith("Difficult")) + 1}"; } if (string.IsNullOrEmpty(model.Name)) { throw new ToolsException("An example's name cannot be empty."); } var exampleWithSameName = _testIoExamplesRepository.Read <TestIoExampleReadModel>().SingleOrDefault(e => e.TestId == testId && e.Name == model.Name); if (exampleWithSameName != null) { throw new ToolsException("An example with the same name already exists"); } var exampleWithSameInputAndOutput = _testIoExamplesRepository.Read <TestIoExampleReadModel>() .SingleOrDefault(e => e.TestId == testId && e.Input == model.Input && e.Output == model.Output); if (exampleWithSameInputAndOutput != null) { throw new ToolsException("An identical example already exists."); } var newExample = new TestInputOutputExample { Id = Guid.NewGuid(), TestId = testId, Name = model.Name, Input = model.Input, Output = model.Output }; await _testIoExamplesRepository.CreateAsync(newExample); }
public async Task AddTestInputOutputExampleAsync([FromRoute] Guid testId, [FromBody] AddTestInputOutputExampleBindingModel model) { await _testsService.AddTestInputOutputExample(testId, model); }