public async Task <Country> Create(Country country)
        {
            if (string.IsNullOrWhiteSpace(country.Name) || string.IsNullOrWhiteSpace(country.Population))
            {
                return(null);
            }

            var newCountry = new Country()
            {
                Name       = country.Name,
                Population = country.Population,
                Cities     = country.Cities
            };

            if (newCountry == null)
            {
                return(null);
            }

            await _db.Countries.AddAsync(newCountry);

            await _db.SaveChangesAsync();

            return(newCountry);
        }
Пример #2
0
        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);
        }
        public async Task <Person> Create(Person person, Guid?cityId)
        {
            if (string.IsNullOrWhiteSpace(person.FirstName) ||
                string.IsNullOrWhiteSpace(person.LastName) ||
                string.IsNullOrWhiteSpace(person.Email) ||
                string.IsNullOrWhiteSpace(person.Gender) ||
                string.IsNullOrWhiteSpace(person.PhoneNumber))
            {
                return(null);
            }

            City city = new City();

            city = null;

            // Checks if the user picked a city when creating a person. If the user did, then it'll try to
            // Find the chosen city, if the city doesn't exist however, it returns a null.
            if (cityId != null)
            {
                city = await _db.Cities.SingleOrDefaultAsync(x => x.Id == cityId);

                if (city == null)
                {
                    return(null);
                }
            }

            var newPerson = new Person()
            {
                FirstName   = person.FirstName,
                LastName    = person.LastName,
                Age         = person.Age,
                Email       = person.Email,
                PhoneNumber = person.PhoneNumber,
                Gender      = person.Gender,
                City        = city
            };

            if (newPerson == null)
            {
                return(null);
            }

            await _db.People.AddAsync(newPerson);

            await _db.SaveChangesAsync();

            return(newPerson);
        }