public async Task AssigningAPolicyToAClientMustCallThroughToTheRepository() { var clientId = Guid.NewGuid(); var policyId = Guid.NewGuid(); var client = new Client(Guid.NewGuid(), "Louise"); var policy = new InsurancePolicy(Guid.NewGuid(), "123", "123", new Dictionary <InsuranceCoverage, float>(), DateTime.Now, 10, 10, RiskLevel.Low); repoMock .Setup(mock => mock.GetPolicyAsync(policyId)) .ReturnsAsync(policy); repoMock .Setup(mock => mock.GetClientAsync(clientId)) .ReturnsAsync(client); repoMock .Setup(mock => mock.AddClientAssignmentAsync(policyId, clientId)) .Returns(Task.CompletedTask) .Verifiable(); await service.AssignPolicyToClientAsync(policyId, clientId); repoMock.Verify(); }
public async Task <IActionResult> UpdateClientPolicies([FromRoute] string clientIdString, [FromBody] string[] policyIdStrings) { if (string.IsNullOrEmpty(clientIdString) || policyIdStrings.Any(string.IsNullOrEmpty)) { return(BadRequest()); } Guid clientId; Guid[] desiredPolicyIds; try { clientId = Guid.Parse(clientIdString); desiredPolicyIds = policyIdStrings.Select(Guid.Parse).ToArray(); } catch (FormatException) { return(BadRequest()); } Client client; try { client = await insuranceService.GetClientAsync(clientId); } catch (ResourceNotFoundException) { return(NotFound()); } var actualPolicyIds = client.AssignedPolicies.Select(policy => policy.Id); var idsToAdd = desiredPolicyIds.Except(actualPolicyIds); var idsToRemove = actualPolicyIds.Except(desiredPolicyIds); foreach (var idToAdd in idsToAdd) { await insuranceService.AssignPolicyToClientAsync(idToAdd, clientId); } foreach (var idToRemove in idsToRemove) { await insuranceService.RemovePolicyFromClientAsync(idToRemove, clientId); } client = await insuranceService.GetClientAsync(clientId); var response = new ClientResponse(client); return(Ok(response)); }