Beispiel #1
0
        public async Task <Address> EnsureAsync(Address address)
        {
            var existingAddress = await ExistsAsync(address);

            if (existingAddress != null)
            {
                return(existingAddress);
            }

            var country = await _countryRepository.FindByNameAsync(address.Country.Name);

            var ZIP = await _zipCodeRepository.FindByCodeAsync(address.ZIP.Code);

            var street = await _streetRepository.FindByNameAsync(address.Street.Name);

            if (country == null)
            {
                _countryRepository.Add(address.Country);
                await _countryRepository.SaveAll();
            }

            if (ZIP == null)
            {
                _zipCodeRepository.Add(address.ZIP);
                await _zipCodeRepository.SaveAll();
            }

            if (street == null)
            {
                _streetRepository.Add(address.Street);
                await _streetRepository.SaveAll();
            }

            address.Country = await _countryRepository.FindByNameAsync(address.Country.Name);

            address.ZIP = await _zipCodeRepository.FindByCodeAsync(address.ZIP.Code);

            address.Street = await _streetRepository.FindByNameAsync(address.Street.Name);

            Add(address);
            await SaveAll();

            return(address);
        }