public async Task <CityWithCountryVM> FindCity(Guid id) { if (id == null || string.IsNullOrWhiteSpace(id.ToString())) { return(null); } var city = await _db.Cities .Include(x => x.Country) .Include(x => x.People) .SingleOrDefaultAsync(x => x.Id == id); if (city == null) { return(null); } CityWithCountryVM cityVM = new CityWithCountryVM { CountryId = city.Country?.Id, CountryName = city.Country?.Name, People = city.People ?? null }; city.People = null; city.Country = null; cityVM.City = city; return(cityVM); }
public async Task <List <CityWithCountryVM> > AllCitiesWithCountry() { var cities = await _db.Cities .Include(x => x.People) .Include(x => x.Country) .ToListAsync(); if (cities == null || cities.Count == 0) { return(null); } List <CityWithCountryVM> citiesVM = new List <CityWithCountryVM>(); foreach (var item in cities) { CityWithCountryVM city = new CityWithCountryVM() { CountryId = item.Country?.Id, CountryName = item.Country?.Name ?? "Stateless", People = item.People ?? null }; item.People = null; item.Country = null; city.City = item; citiesVM.Add(city); } return(citiesVM); }
private List <CityWithCountryVM> TwoValidCitiesWithCountries() { var city = new CityWithCountryVM { City = OneValidCity(), }; return(new List <CityWithCountryVM>() { new CityWithCountryVM { City = city.City, CountryId = city.CountryId, CountryName = city.CountryName, People = city.People }, new CityWithCountryVM { City = new City { Name = "TestTown", Population = "12 345", }, CountryId = city.CountryId, CountryName = city.CountryName, People = city.People } }); }
public async Task <CityWithCountryVM> Create(City city, Guid?countryId) { if (string.IsNullOrWhiteSpace(city.Name) || string.IsNullOrWhiteSpace(city.Population)) { return(null); } Country country = new Country(); country = null; if (countryId != null) { country = await _db.Countries.SingleOrDefaultAsync(x => x.Id == countryId); if (country == null) { return(null); } } var newCity = new City() { Name = city.Name, Population = city.Population, People = city.People, Country = country }; var cityVM = new CityWithCountryVM() { City = newCity, CountryId = country.Id, CountryName = country.Name, }; if (cityVM == null) { return(null); } await _db.Cities.AddAsync(cityVM.City); await _db.SaveChangesAsync(); cityVM.City.Country = null; return(cityVM); }