public async Task <ClientPolicyDTO> AddClientPolicy(ClientPolicyDTO newClientPolicy) { ClientPolicy clientPolicy = null; List <int> idsToRemove = new List <int>(); using (var context = new EnsuranceDBEntities()) { var existingClientPolicy = context.ClientPolicies .Any(cp => cp.ClientId == newClientPolicy.ClientId && cp.PolicyId == newClientPolicy.PolicyId); if (!existingClientPolicy) { clientPolicy = new ClientPolicy { ClientId = newClientPolicy.ClientId, PolicyId = newClientPolicy.PolicyId, StartDate = DateTime.Now }; context.ClientPolicies.Add(clientPolicy); await context.SaveChangesAsync(); } else { throw new Exception("The client already has this policy."); } } return(newClientPolicy); }
public async Task <IHttpActionResult> Post([FromBody] ClientPolicyDTO newClientPolicy) { try { ClientPolicyDTO resultPolicy = await repository.AddClientPolicy(newClientPolicy); return(Content(HttpStatusCode.Created, resultPolicy)); } catch (Exception ex) { return(InternalServerError(ex)); } }