Example #1
0
        public void MapperFunction_ToDContinent_FunctionalityTest()
        {
            string    continentName = "Continent";
            Continent continent     = new Continent(continentName);

            string  countryName       = "Country";
            int     countryPopulation = 14000;
            float   countrySurface    = 7500.50f;
            Country country           = new Country(countryPopulation, countryName, countrySurface, continent);

            string  countryName2       = "Country 2";
            int     countryPopulation2 = 18000;
            float   countrySurface2    = 7500.50f;
            Country country2           = new Country(countryPopulation2, countryName2, countrySurface2, continent);

            continent.AddCountry(country);
            continent.AddCountry(country2);

            DContinent mappedDContinent = Mapper.ToDContinent(continent);

            mappedDContinent.Name.Should().Be(continentName);
            mappedDContinent.Population.Should().Be(countryPopulation + countryPopulation2);

            mappedDContinent.Countries.Count.Should().Be(2);
            mappedDContinent.Countries[0].Name.Should().Be(countryName);
            mappedDContinent.Countries[0].Surface.Should().Be(countrySurface);
            mappedDContinent.Countries[0].Population.Should().Be(countryPopulation);
            mappedDContinent.Countries[1].Name.Should().Be(countryName2);
            mappedDContinent.Countries[1].Surface.Should().Be(countrySurface2);
            mappedDContinent.Countries[1].Population.Should().Be(countryPopulation2);
        }
        public void AddContinent(Continent continent)
        {
            DContinent dContinent = Mapper.FromContinentToDContinent(continent);

            if (context.Continents.Any(c => c.Name == continent.Name))
            {
                throw new Exception($"Continent: {continent.Name} already in database.");
            }
            context.Continents.Add(dContinent);
        }
        public void UpdateContinent(Continent continent, int continentId)
        {
            if (!context.Continents.Any(c => c.Id == continentId))
            {
                throw new Exception($"Continent with id: {continentId} not in database.");
            }
            DContinent continentToUpdate = context.Continents
                                           .Include(continent => continent.Countries)
                                           .Single(c => c.Id == continentId);

            continentToUpdate.Name = continent.Name;
        }
        public bool isInCountry(int continentId, int countryId)
        {
            if (!context.Continents.Any(c => c.Id == continentId))
            {
                throw new Exception($"Continent with id: {continentId} not in DB.");
            }
            DContinent continent = context.Continents
                                   .Include(continent => continent.Countries)
                                   .AsNoTracking()
                                   .Single(c => c.Id == continentId);

            return(continent.Countries.Any(c => c.Id == countryId));
        }
Example #5
0
        public void AddCountry(int continentId, string countryName, int countryPopulation, float countrySurface)
        {
            DContinent dContinent = context.Continents.Include(c => c.Countries).Single(c => c.Id == continentId);

            Continent continent = Mapper.ToContinent(dContinent);

            continent.AddCountry(new Country(countryPopulation, countryName, countrySurface, continent));

            DCountry dCountry = Mapper.ToDCountry(continent.Countries.Last());

            dContinent.Countries.Add(dCountry);
            dContinent.Population = continent.Population;
        }
        public Continent GetContinentByName(string name)
        {
            if (!context.Continents.Any(c => c.Name == name))
            {
                throw new Exception($"Continent: {name} not in DB.");
            }
            DContinent dContinent = context.Continents
                                    .AsNoTracking()
                                    .Include(continent => continent.Countries)
                                    .AsNoTracking()
                                    .Single(c => c.Name == name);

            return(Mapper.FromDContinentToContinent(dContinent));
        }
        public Continent GetContinent(int id)
        {
            if (!context.Continents.Any(c => c.Id == id))
            {
                throw new Exception($"Continent with id: {id} not in DB.");
            }
            DContinent dContinent = context.Continents
                                    .AsNoTracking()
                                    .Include(continent => continent.Countries)
                                    .AsNoTracking()
                                    .Single(c => c.Id == id);

            return(Mapper.FromDContinentToContinent(dContinent));
        }
        public void DeleteCountry(int continentId, int countryId)
        {
            if (!context.Continents.Any(c => c.Id == continentId))
            {
                throw new Exception($"Continent with id: {continentId} not in DB.");
            }
            DContinent continent = context.Continents
                                   .Include(continent => continent.Countries)
                                   .Single(c => c.Id == continentId);
            DCountry country = continent.Countries.SingleOrDefault(c => c.Id == countryId);

            if (country == null)
            {
                throw new Exception($"country with id: {countryId} not in DB.");
            }
            continent.Countries.Remove(country);
        }
        public Country GetCountry(int continentId, int countryId)
        {
            if (!context.Continents.Any(c => c.Id == continentId))
            {
                throw new Exception($"Continent with id: {continentId} not in DB.");
            }
            DContinent continent = context.Continents
                                   .Include(continent => continent.Countries)
                                   .AsNoTracking()
                                   .Single(c => c.Id == continentId);
            DCountry country = continent.Countries.SingleOrDefault(c => c.Id == countryId);

            if (country == null)
            {
                throw new Exception($"Country with id: {countryId} not in DB.");
            }
            return(Mapper.FromDCountryToCountry(country));
        }
        public void AddCountry(int continentId, Country country)
        {
            if (!context.Continents.Any(c => c.Id == continentId))
            {
                throw new Exception($"Continent with id: {continentId} not in DB.");
            }
            DContinent continent = context.Continents
                                   .Include(continent => continent.Countries)
                                   .Single(c => c.Id == continentId);

            if (continent.Countries.Any(c => c.Name == country.Name))
            {
                throw new Exception($"Country: {country.Name} already in DB.");
            }
            DCountry toAdd = Mapper.FromCountryToDCountry(country);

            if (toAdd.Id == 0)
            {
                toAdd.Id = continent.Countries.Count;
            }
            continent.Countries.Add(toAdd);
        }
        public void UpdateCountry(int continentId, int countryId, Country country)
        {
            if (!context.Continents.Any(c => c.Id == continentId))
            {
                throw new Exception($"Continent with id: {continentId} not in DB.");
            }
            DContinent continent = context.Continents
                                   .Include(continent => continent.Countries)
                                   .Single(c => c.Id == continentId);
            DCountry countryToUpdate = continent.Countries.SingleOrDefault(c => c.Id == countryId);

            if (countryToUpdate == null)
            {
                throw new Exception($"country with id: {countryId} not in DB.");
            }
            if (continent.Countries.Any(c => c.Id == country.Id))
            {
                throw new Exception($"Country with id: {country.Id} already in DB.");
            }
            countryToUpdate.Id         = (int)country.Id;
            countryToUpdate.Population = country.Population;
            countryToUpdate.Surface    = country.Surface;
            countryToUpdate.Name       = country.Name;
        }