Пример #1
0
 public ActionResult Delete(int id)
 {
     try
     {
         var continent = ContinentManager.GetContinentById(id);
         if (continent != null)
         {
             if (CountryManager.GetContinentWithName(continent).Count == 0)
             {
                 ContinentManager.RemoveContinent(id);
                 return(NoContent());
             }
             else
             {
                 return(BadRequest("Continent still has countries!"));
             }
         }
         else
         {
             return(NotFound("Continent not found!"));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex));
     }
 }
Пример #2
0
 public ActionResult <SampleCity> DeleteCity(int id, int countryId, int cityId)
 {
     try
     {
         var continent = ContinentManager.GetContinentById(id);
         var country   = CountryManager.GetCountry(countryId);
         if (continent.Name == country.Continent.Name)
         {
             var city = CityManager.GetCityById(cityId);
             if (city == null)
             {
                 return(NotFound("City does not exist in this Country!"));
             }
             else
             {
                 CityManager.RemoveCityById(cityId);
                 return(NoContent());
             }
         }
         else
         {
             return(NotFound("Country does not exist in this Continent!"));
         }
     }
     catch (Exception ex)
     {
         throw new Exception("ERROR City Controller in the Delete request (" + ex + ")");
     }
 }
Пример #3
0
 public ActionResult <SampleCity> GetCity(int id, int countryId, int cityId)
 {
     try
     {
         var continent = ContinentManager.GetContinentById(id);
         var country   = CountryManager.GetCountry(countryId);
         if (continent.Name == country.Continent.Name)
         {
             var city = CityManager.GetCityById(cityId);
             if (city == null)
             {
                 return(NotFound("City does not exist in this County!"));
             }
             else
             {
                 return(new SampleCity {
                     ID = city.ID, Name = city.Name, Capital = city.IsCapital, Country = $"http://localhost:5001/api/Continent/{id}/Country/{countryId}", Population = city.Population
                 });
             }
         }
         else
         {
             return(NotFound("Country not found in Continent!"));
         }
     }
     catch (Exception ex)
     {
         throw new Exception("ERROR City Controller in the Get request (" + ex + ")");
     }
 }
Пример #4
0
 public ActionResult <SampleContinent> GetContinent(int id)
 {
     try
     {
         var continent   = ContinentManager.GetContinentById(id);
         var countryList = CountryManager.GetContinentWithName(continent).Select(x => $"http://localhost:3000/api/Continent/{continent.ID}/Country/" + x.ID.ToString()).ToList();
         return(new SampleContinent {
             ID = continent.ID.ToString(), Name = continent.Name, Population = continent.Population, Countries = countryList
         });
     }
     catch (Exception ex)
     {
         return(NotFound("Continent not found!"));
     }
 }
Пример #5
0
        public ActionResult <SampleContinent> Put(int id, [FromBody] SampleContinent sampleContinent)
        {
            try
            {
                var continent = ContinentManager.GetContinentById(id);

                if (ContinentManager.ContinentExists(new Continent(continent.Name)))
                {
                    continent.SetName(continent.Name);
                    ContinentManager.UpdateContinent(continent);
                    return(CreatedAtAction(nameof(GetContinent), new { id = continent.ID }, continent));
                }
                else
                {
                    return(BadRequest("Continent error"));
                }
            }
            catch (Exception ex)
            {
                return(NotFound("Continent not found!"));
            }
        }
Пример #6
0
 public ActionResult PostCity(int id, int countryId, [FromBody] SampleCity city)
 {
     try
     {
         var continent = ContinentManager.GetContinentById(id);
         var country   = CountryManager.GetCountry(countryId);
         if (continent.Name == country.Continent.Name)
         {
             var tmpCity = new City(city.Name, city.Population, country, city.Capital);
             CityManager.Add(tmpCity);
             return(CreatedAtAction(nameof(GetCity), new { id = continent.ID, countryId = country.ID, cityId = tmpCity.ID }, tmpCity));
         }
         else
         {
             return(BadRequest("Country not found in Continent!"));
         }
     }
     catch (Exception ex)
     {
         throw new Exception("ERROR City Controller in the Post request (" + ex + ")");
     }
 }