public void UpdateClaim(MitchellClaim updater) { if (string.IsNullOrWhiteSpace(updater.ClaimNumber)) { throw new InvalidApiUsageException(ApiUsageError.ClaimNumberNotSpecified); } MitchellClaim existingClaim; if (_claims.TryGetValue(updater.ClaimNumber, out existingClaim)) { // Before updating the claim let's make sure we will not end up with an invalid claim. // We are going to create a clone, update the clone and only if everything looks fine then we'll // persist the clone. MitchellClaim newClaim = existingClaim.DeepClone(); newClaim.Update(updater); if (newClaim.ValidateRequiredFields() == false) { throw new InvalidApiUsageException(ApiUsageError.RequiredFieldNotSpecified); } // Now that we know everything is OK, persist the updated claim. _claims[updater.ClaimNumber] = newClaim; } else { // This method is used incorrectly. The client may attempt to update a claim that does not exist but // that attempt should not propagate to this level. // As far as this method is concerned, this is an invalid call. throw new InvalidApiUsageException(ApiUsageError.ItemNotFound); } }
public void AddClaim(MitchellClaim claim) { if (claim.ValidateRequiredFields() == false) { throw new InvalidApiUsageException(ApiUsageError.RequiredFieldNotSpecified); } if (_claims.ContainsKey(claim.ClaimNumber)) { throw new InvalidApiUsageException(ApiUsageError.ItemAlreadyExists); } if (claim.Vehicles.GroupBy(v => v.Vin).Any(g => g.Count() > 1)) { throw new InvalidApiUsageException(ApiUsageError.DuplicateVehicles); } _claims.Add(claim.ClaimNumber, claim); }