public string AddTown(string townName, string countryName) { StringBuilder sb = new StringBuilder(); Country country = countryService.GetCountryByName(countryName); if (country != null) { sb.AppendLine(countryService.AddCountry(countryName)); country = countryService.GetCountryByName(countryName); } Town town = this.GetTownByName(townName, countryName); if (town == null) { town = new Town() { Name = townName, CountryId = country.Id, }; dbContext.Towns.Add(town); dbContext.SaveChanges(); sb.AppendLine($">> Town {townName} is added!"); return(sb.ToString().TrimEnd()); } else { return($">> Town {townName} already exists!"); } }
public string AddAddress(string addressName, string townName, string countryName) { StringBuilder sb = new StringBuilder(); Country country = countryService.GetCountryByName(countryName); if (country == null) { sb.AppendLine(countryService.AddCountry(countryName)); country = countryService.GetCountryByName(countryName); } Town town = townsService.GetTownByName(townName, countryName); if (town == null) { sb.AppendLine(townsService.AddTown(townName, countryName)); town = townsService.GetTownByName(townName, countryName); } Address address = GetAddressByName(addressName, townName, countryName); if (address == null) { address = new Address() { Name = addressName, Town = town }; dbContext.Addresses.Add(address); dbContext.SaveChanges(); sb.AppendLine($">> Address {addressName} is added!"); return(sb.ToString().TrimEnd()); } else { return($">> Address {addressName} already exist!"); } }