public ActionResult <SampleCountry> Put(int id, int con_id, [FromBody] SampleCountry con)
        {
            try
            {
                var tempCon = ContinentManager.Get(id);
                var temp    = CountryManager.Get(con_id);

                if (con.Continent == null || con.Continent == "")
                {
                    con.Continent = id.ToString();
                }

                if (tempCon.Name == temp.Continent.Name)
                {
                    logger.LogInformation("CountryController : Put => " + DateTime.Now);
                    CountryManager.Update(temp, con.Name, con.Population, con.Surface, ContinentManager.Get(Int32.Parse(con.Continent)));
                    return(Ok());
                }
                else
                {
                    return(NotFound("Country not found in Continent"));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Пример #2
0
 public ActionResult <SampleCity> Get(int id, int country_id, int city_id)
 {
     try
     {
         var tempCon = ContinentManager.Get(id);
         var tempCou = CountryManager.Get(country_id);
         if (tempCon.Name == tempCou.Continent.Name)
         {
             var temp = CityManager.Get(city_id);
             if (temp == null)
             {
                 return(NotFound("City not found in Country"));
             }
             else
             {
                 logger.LogInformation("CityController : Get => " + DateTime.Now);
                 return(new SampleCity {
                     ID = temp.ID, name = temp.Name, Capital = temp.IsCapital, Country = $"https://localhost:5001/api/Continent/{id}/Country/{country_id}", Population = temp.Population
                 });
             }
         }
         else
         {
             return(NotFound("Country not found in Continent"));
         }
     }catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
 public ActionResult <SampleCountry> Get(int id, int con_id)
 {
     try
     {
         var tempCon = ContinentManager.Get(id);
         var temp    = CountryManager.Get(con_id);
         if (tempCon.Name == temp.Continent.Name)
         {
             logger.LogInformation("CountryController : Get => " + DateTime.Now);
             return(new SampleCountry
             {
                 ID = temp.ID,
                 Name = temp.Name,
                 Continent = $"https://localhost:5001/api/Continent/" + temp.Continent.ID,
                 Population = temp.Population,
                 Surface = temp.Surface,
                 Cities = temp.Cities.Select(k => $"https://localhost:5001/api/Continent/{temp.Continent.ID}/Country/{temp.ID}/City/" + k.ID.ToString()).ToList(),
                 Rivers = temp.Rivers.Select(x => $"https://localhost:5001/api/River/" + x.ID.ToString()).ToList()
             });
         }
         else
         {
             return(NotFound("Country not found in Continent"));
         }
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
        public ActionResult Delete(int id, int con_id)
        {
            try
            {
                var tempCon = ContinentManager.Get(id);
                var temp    = CountryManager.Get(con_id);

                if (tempCon.Name == temp.Continent.Name)
                {
                    if (temp.Cities.Count == 0 || temp.Cities == null)
                    {
                        logger.LogInformation("CountryController : Delete => " + DateTime.Now);
                        CountryManager.Remove(con_id);
                        return(NoContent());
                    }
                    else
                    {
                        return(BadRequest("Country still has Cities"));
                    }
                }
                else
                {
                    return(NotFound("Country not found in Continent"));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Пример #5
0
 public ActionResult <SampleCity> Delete(int id, int country_id, int city_id)
 {
     try
     {
         var tempCon = ContinentManager.Get(id);
         var tempCou = CountryManager.Get(country_id);
         if (tempCon.Name == tempCou.Continent.Name)
         {
             var temp = CityManager.Get(city_id);
             if (temp == null)
             {
                 return(NotFound("City not found in Country"));
             }
             else
             {
                 logger.LogInformation("CityController : Delete => " + DateTime.Now);
                 CityManager.Remove(city_id);
                 return(NoContent());
             }
         }
         else
         {
             return(NotFound("Country not found in Continent"));
         }
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Пример #6
0
 public ActionResult Post(int id, int country_id, [FromBody] SampleCity con)
 {
     try
     {
         var tempCon = ContinentManager.Get(id);
         var tempCou = CountryManager.Get(country_id);
         if (tempCon.Name == tempCou.Continent.Name)
         {
             var temp = new City(con.name, con.Population, tempCou, con.Capital);
             if (CityManager.CalculatePopulationCheck(temp))
             {
                 CityManager.Add(temp);
                 logger.LogInformation("CityController : Post => " + DateTime.Now);
                 return(CreatedAtAction(nameof(Get), new { id = tempCon.ID, country_id = tempCou.ID, city_id = temp.ID }, temp));
             }
             else
             {
                 return(BadRequest("Too much population for Country"));
             }
         }
         else
         {
             return(NotFound("Country not found in Continent"));
         }
     }catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
 public ActionResult Delete(int id)
 {
     try
     {
         var temp = ContinentManager.Get(id);
         if (temp != null)
         {
             if (CountryManager.GetByContinentName(temp).Count == 0)
             {
                 logger.LogInformation("ContinentController : Delete => " + DateTime.Now);
                 ContinentManager.Remove(id);
                 return(NoContent());
             }
             else
             {
                 return(BadRequest("Continent still has Countries"));
             }
         }
         else
         {
             return(NotFound("Continent not found"));
         }
     }
     catch (Exception e)
     {
         return(BadRequest(e));
     }
 }
        public void Continent_Manager_Test()
        {
            ContinentManager temp = new ContinentManager(new UnitOfWork(new DataContext("test")));

            temp.RemoveAll();
            Continent con = new Continent("test-continent");

            Assert.AreEqual(0, con.ID);
            temp.Add(con);
            temp.Add(new Continent("test-continent1"));

            List <Continent> continents = temp.GetAll();
            Continent        continent1 = temp.Get(continents[0].ID);
            Continent        continent2 = temp.Get(continents[1].ID);

            Assert.AreEqual(2, continents.Count);
            Assert.AreEqual("test-continent", continent1.Name);
            Assert.AreEqual("test-continent1", continent2.Name);

            temp.Remove(continent1.ID);

            continents = temp.GetAll();
            continent1 = temp.Get(continents[0].ID);

            Assert.AreEqual(1, continents.Count);
            Assert.AreEqual("test-continent1", continent1.Name);

            continent1.SetName("test");

            temp.Update(continent1);

            continents = temp.GetAll();
            continent1 = temp.Get(continents[0].ID);
            Assert.AreEqual("test", continent1.Name);
            temp.RemoveAll();
        }
 public ActionResult <SampleContinent> Get(int id)
 {
     try
     {
         var temp     = ContinentManager.Get(id);
         var tempList = CountryManager.GetByContinentName(temp).Select(s => $"https://localhost:5001/api/Continent/{temp.ID}/Country/" + s.ID.ToString()).ToList();
         logger.LogInformation("ContinentController : Get => " + DateTime.Now);
         return(new SampleContinent {
             ID = temp.ID.ToString(), Name = temp.Name, Population = temp.Population, Countries = tempList
         });
     }
     catch (Exception e)
     {
         return(NotFound("Continent doesn't exist"));
     }
 }
 public ActionResult <List <SampleCountry> > GetAll(int id)
 {
     try
     {
         var temp = ContinentManager.Get(id);
         logger.LogInformation("CountryController : GetAll => " + DateTime.Now);
         return(CountryManager.GetByContinentName(temp).Select(s => new SampleCountry
         {
             ID = s.ID,
             Name = s.Name,
             Population = s.Population,
             Continent = $"https://localhost:5001/api/Continent/" + temp.ID,
             Surface = s.Surface,
             Cities = s.Cities.Select(k => $"https://localhost:5001/api/Continent/{temp.ID}/Country/{s.ID}/City/" + k.ID.ToString()).ToList(),
             Rivers = s.Rivers.Select(x => $"https://localhost:5001/api/River/" + x.ID.ToString()).ToList()
         }).ToList());
     }catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Пример #11
0
 public ActionResult <SampleCity> Put(int id, int country_id, int city_id, [FromBody] SampleCity con)
 {
     try
     {
         var tempCon       = ContinentManager.Get(id);
         var tempCou       = CountryManager.Get(country_id);
         var getNewCountry = CountryManager.Get(Int32.Parse(con.Country));
         if (tempCon.Name == tempCou.Continent.Name)
         {
             var temp = CityManager.Get(city_id);
             if (temp == null)
             {
                 return(NotFound("City not found in Country"));
             }
             else
             {
                 if (getNewCountry != null)
                 {
                     CityManager.Update(temp, con.name, con.Population, con.Capital, getNewCountry);
                     logger.LogInformation("CityController : Put => " + DateTime.Now);
                     return(Ok());
                 }
                 else
                 {
                     return(NotFound("Country input not found"));
                 }
             }
         }
         else
         {
             return(NotFound("Country not found in Continent"));
         }
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
        public ActionResult <SampleContinent> Put(int id, [FromBody] SampleContinent con)
        {
            try
            {
                var temp = ContinentManager.Get(id);

                if (ContinentManager.IfExist(new Continent(con.Name)))
                {
                    logger.LogInformation("ContinentController : Put => " + DateTime.Now);
                    temp.SetName(con.Name);
                    ContinentManager.Update(temp);
                    return(CreatedAtAction(nameof(Get), new { id = temp.ID }, temp));
                }
                else
                {
                    return(BadRequest("Continent already Exists"));
                }
            }
            catch (Exception e)
            {
                return(NotFound("Continent doesn't exist"));
            }
        }
 public ActionResult <SampleCountry> Post(int id, [FromBody] SampleCountry country)
 {
     try
     {
         CountryManager = new CountryManager(new UnitOfWork(new DataContext("test")));
         var temp = new Country(country.Name, country.Population, country.Surface, ContinentManager.Get(id));
         CountryManager.Add(temp);
         logger.LogInformation("CountryController : Post => " + DateTime.Now);
         return(CreatedAtAction(nameof(Get), new { id = temp.Continent.ID, con_id = temp.ID }, temp));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }