// Embed relations in customer resource: reuse old relation if there is one and it hasn't changed private async Task EmbedRelations(Customer customer, Customer oldCustomer = null) { try { if (customer.Country != null) { if (oldCustomer != null && oldCustomer.Country != null && oldCustomer.Country.Id == customer.Country.Id) { customer.Country = oldCustomer.Country; } else { customer.Country = await _countryDataProvider.GetByIdAsync(int.Parse(customer.Country.Id)); } } if (customer.Language != null) { if (oldCustomer != null && oldCustomer.Language != null && oldCustomer.Language.Id == customer.Language.Id) { customer.Language = oldCustomer.Language; } else { customer.Language = await _langugageDataProvider.GetByIdAsync(int.Parse(customer.Language.Id)); } } if (customer.HonorificPrefix != null) { if (oldCustomer != null && oldCustomer.HonorificPrefix != null && oldCustomer.HonorificPrefix.Id == customer.HonorificPrefix.Id) { customer.HonorificPrefix = oldCustomer.HonorificPrefix; } else { customer.HonorificPrefix = await _honorificPrefixDataProvider.GetByIdAsync(customer.HonorificPrefix.Id); } } if (customer.Tags != null) { if (oldCustomer != null && oldCustomer.Tags != null && Tag.Equals(customer.Tags, oldCustomer.Tags)) { customer.Tags = oldCustomer.Tags; } else { customer.Tags = await Task.WhenAll(customer.Tags.Select(t => _tagDataProvider.GetByIdAsync(int.Parse(t.Id)))); } } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }
// Embed relations in contact resource: use old embedded value if there is one and the relation isn't updated or null private async Task EmbedRelations(Building building, Building oldBuilding = null) { try { if (building.Country != null) { if (oldBuilding != null && oldBuilding.Country != null && oldBuilding.Country.Id == building.Country.Id) { building.Country = oldBuilding.Country; } else { building.Country = await _countryDataProvider.GetByIdAsync(int.Parse(building.Country.Id)); } } if (building.Language != null) { if (oldBuilding != null && oldBuilding.Language != null && oldBuilding.Language.Id == building.Language.Id) { building.Language = oldBuilding.Language; } else { building.Language = await _langugageDataProvider.GetByIdAsync(int.Parse(building.Language.Id)); } } if (building.HonorificPrefix != null) { if (oldBuilding != null && oldBuilding.HonorificPrefix != null && oldBuilding.HonorificPrefix.Id == building.HonorificPrefix.Id) { building.HonorificPrefix = oldBuilding.HonorificPrefix; } else { building.HonorificPrefix = await _honorificPrefixDataProvider.GetByIdAsync(building.HonorificPrefix.Id); } } // Customer cannot be updated. Take customer of oldBuilding on update. if (oldBuilding != null) { building.Customer = oldBuilding.Customer; } else { building.Customer = await _customerDataProvider.GetByNumberAsync(building.Customer.Id); } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }