Ejemplo n.º 1
0
        /// <summary>
        /// Adds city to db if not exist and
        /// updates if required city is found
        /// </summary>
        /// <param name="city">City to add</param>
        /// <returns></returns>
        public async Task <int> AddCityToDbIfNotExist(CityDto city)
        {
            var cityDto = await _context.Cities.FirstOrDefaultAsync(c => c.CityName == city.CityName);

            if (cityDto == null)
            {
                await _context.AddAsync(city);
                await SaveChangesAsync();

                return(city.Id);
            }
            else
            {
                return(cityDto.Id);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds region to db if not exist and
        /// updates if required region is found
        /// </summary>
        /// <param name="region">Region to add</param>
        /// <returns></returns>
        public async Task <int> AddRegionToDbIfNotExist(RegionDto region)
        {
            var regionDto = await _context.Regions.FirstOrDefaultAsync(r => r.RegionName == region.RegionName);

            if (regionDto == null)
            {
                await _context.AddAsync(region);
                await SaveChangesAsync();

                return(region.Id);
            }
            else
            {
                return(regionDto.Id);
            }
        }
        /// <summary>
        /// Adds country to db if its code not exist and
        /// updates if required country is found
        /// </summary>
        /// <param name="country">Country to add</param>
        /// <returns></returns>
        public async Task <int> AddCountryToDbIfNotExistAsync(CountryDto country)
        {
            var countryDto = await _context.Countries.FirstOrDefaultAsync(c => c.CountryCode == country.CountryCode);

            if (countryDto == null)
            {
                await _context.AddAsync(country);
                await SaveChangesAsync();

                return(country.Id);
            }
            else
            {
                countryDto.CountryName = country.CountryName;
                countryDto.Capital     = country.Capital;
                countryDto.Area        = country.Area;
                countryDto.Population  = country.Population;
                countryDto.Region      = country.Region;

                await SaveChangesAsync();

                return(countryDto.Id);
            }
        }