Пример #1
0
 public IActionResult PutContinent(int id, [FromBody] ContinentInApi continentIn)
 {
     logger.LogInformation($"Put api/continent/ called");
     if (continentIn == null)
     {
         return(BadRequest());
     }
     try
     {
         if (continentManager.Find(id) == null)
         {
             Domain.Models.Continent continent        = ContinentMapper.ContinentInMapper(countryManager, continentIn);
             Domain.Models.Continent continentCreated = continentManager.AddContinent(continent);
             ContinentOutApi         continentOut     = ContinentMapper.ContinentOutMapper(hostUrl, continentCreated);
             return(CreatedAtAction(nameof(GetContinent), new { id = continentOut.Id }, continentOut));
         }
         Domain.Models.Continent continentUpdate = ContinentMapper.ContinentInMapper(countryManager, continentIn);
         continentUpdate.Id = id;
         continentManager.UpdateContinent(id, continentUpdate);
         return(new NoContentResult());
     }
     catch (Exception ex)
     {
         logger.LogError(ex.Message);
         return(BadRequest(ex.Message));
     }
 }
Пример #2
0
        public static Country CountryInMapper(ContinentManager continentManager, CountryInApi countryIn)
        {
            Country country = new Country();

            country.Name       = countryIn.Name;
            country.Population = countryIn.Population;
            country.Surface    = countryIn.Surface;
            country.Continent  = continentManager.Find(countryIn.ContinentId);
            return(country);
        }
Пример #3
0
 public ActionResult <CountryOutApi> GetCountry(int continentId, int countryId)
 {
     logger.LogInformation(countryId, $"Get api/continent/{continentId}/country/{countryId} called");
     try
     {
         if (continentManager.Find(continentId) != null && countryManager.Find(countryId) != null &&
             countryManager.Find(countryId).Continent.Id == continentId)
         {
             Domain.Models.Country country    = countryManager.Find(continentId, countryId);
             CountryOutApi         countryOut = CountryMapper.CountryOutMapper(hostUrl, country);
             return(Ok(countryOut));
         }
         else
         {
             logger.LogError($"Country with id{countryId} is not found");
             return(NotFound($"Country with id{countryId} is not found"));
         }
     }
     catch (Exception ex)
     {
         logger.LogError(ex.Message);
         return(NotFound(ex.Message));
     }
 }
Пример #4
0
        public void AddContinent_ShouldAddContinentAndUpdateCountries_IfContinentIsValid()
        {
            // Arrange
            Continent      continent1     = new Continent("Africa");
            Continent      continent1InDb = continentManager.AddContinent(continent1);
            Country        country1       = new Country("Afghanistan", 38928346, 652.860, continent1InDb);
            Country        country2       = new Country("Albania", 2877797, 27.400, continent1InDb);
            Country        country1InDb   = countryManager.AddCountry(country1);
            Country        country2nDb    = countryManager.AddCountry(country2);
            List <Country> countries      = new List <Country>();

            countries.Add(country1InDb); countries.Add(country2nDb);
            Continent continent2 = new Continent("Asia", countries);

            // Act
            continentManager.AddContinent(continent2);
            Continent continent2InDb = continentManager.Find("Asia");

            // Assert
            continent2InDb.Should().BeEquivalentTo(continent2, options => options.Excluding(o => o.Id));
        }