/// <inheritdoc /> public async Task <Unit> Handle(UpdateStubCommand request, CancellationToken cancellationToken) { var validationResults = _stubModelValidator.ValidateStubModel(request.Stub).ToArray(); if (validationResults.Any()) { throw new ValidationException(validationResults); } // Check that the stub is not read-only. const string exceptionFormat = "Stub with ID '{0}' is read-only; it can not be updated through the API."; var oldStub = await _stubContext.GetStubAsync(request.StubId); if (oldStub?.Metadata?.ReadOnly == true) { throw new ValidationException(string.Format(exceptionFormat, request.StubId)); } var newStub = await _stubContext.GetStubAsync(request.Stub.Id); if (newStub?.Metadata?.ReadOnly == true) { throw new ValidationException(string.Format(exceptionFormat, request.Stub.Id)); } await _stubContext.DeleteStubAsync(request.StubId); await _stubContext.DeleteStubAsync(request.Stub.Id); await _stubContext.AddStubAsync(request.Stub); return(Unit.Value); }
/// <inheritdoc /> public async Task <FullStubModel> Handle(AddStubCommand request, CancellationToken cancellationToken) { var validationResults = _stubModelValidator.ValidateStubModel(request.Stub).ToArray(); if (validationResults.Any()) { throw new ValidationException(validationResults); } // Delete stub with same ID. await _stubContext.DeleteStubAsync(request.Stub.Id); return(await _stubContext.AddStubAsync(request.Stub)); }
private void ParseAndValidateStubs(IEnumerable <StubModel> stubs) { foreach (var stub in stubs) { if (string.IsNullOrWhiteSpace(stub.Id)) { // If no ID is set, calculate a unique ID based on the stub contents. stub.EnsureStubId(); } var results = _stubModelValidator.ValidateStubModel(stub).ToArray(); if (results.Any()) { results = results.Select(r => $"- {r}").ToArray(); _logger.LogWarning( $"Validation warnings encountered for stub '{stub.Id}':\n{string.Join("\n", results)}"); } } }
/// <inheritdoc /> public async Task <IEnumerable <FullStubModel> > Handle( AddStubsCommand request, CancellationToken cancellationToken) { if (request.Stubs == null || !request.Stubs.Any()) { throw new ArgumentException("No stubs posted."); } // Validate posted stubs. var stubsToAdd = request.Stubs.ToArray(); var validationResults = stubsToAdd .SelectMany(s => { var validation = _stubModelValidator.ValidateStubModel(s); return(!string.IsNullOrWhiteSpace(s.Id) ? validation.Select(v => $"{s.Id}: {v}") : validation); }) .ToArray(); if (validationResults.Any()) { throw new ValidationException(validationResults); } // Validate that none of the posted stubs have the same ID. var duplicateIds = stubsToAdd .Select(s => s.Id) .GroupBy(id => id, StringComparer.OrdinalIgnoreCase) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToArray(); if (duplicateIds.Any()) { throw new ArgumentException( $"The following stub IDs are posted more than once: {string.Join(", ", duplicateIds)}"); } // Validated that no stubs with the same ID exist in readonly stub sources. var stubsFromReadonlySource = await _stubContext.GetStubsFromReadOnlySourcesAsync(); var duplicateStubs = stubsFromReadonlySource.Where(r => stubsToAdd.Any(s => string.Equals(s.Id, r.Stub.Id, StringComparison.OrdinalIgnoreCase))).ToArray(); if (duplicateStubs.Any()) { throw new ValidationException(duplicateStubs.Select(s => $"Stub with ID already exists: {s.Stub.Id}")); } // Add the stubs. var result = new List <FullStubModel>(); foreach (var stub in stubsToAdd) { // First, delete existing stub with same ID. await _stubContext.DeleteStubAsync(stub.Id); result.Add(await _stubContext.AddStubAsync(stub)); } return(result); }