Example #1
0
 public IActionResult AddCountry(CountryViewModel countryVM)
 {
     if (ModelState.IsValid)
     {
         City   cityDB;
         Region regionDB;
         if (_cityRepository.CheckExistCity(countryVM.CapitalName) == "EXIST")
         {
             cityDB = _cityRepository.GetCityByName(countryVM.CapitalName);
         }
         else
         {
             _cityRepository.AddCity(new City {
                 Name = countryVM.CapitalName
             });
             cityDB = _cityRepository.GetCityByName(countryVM.CapitalName);
         }
         if (_regionRepository.CheckExistRegion(countryVM.RegionName) == "EXIST")
         {
             regionDB = _regionRepository.GetRegionByName(countryVM.RegionName);
         }
         else
         {
             _regionRepository.AddRegion(new Region {
                 Name = countryVM.RegionName
             });
             regionDB = _regionRepository.GetRegionByName(countryVM.RegionName);
         }
         if (_countryRepository.CheckExistCountry(countryVM.Country.CountryCode) == "NOT EXIST")
         {
             _countryRepository.AddCountry(countryVM.Country, cityDB.Name, regionDB.Name);
             TempData["successMessage"] = string.Format($"{countryVM.Country.Name}" +
                                                        " has been successfully added to the database.");
             return(RedirectToAction("Index"));
         }
         else
         {
             _countryRepository.UpdateCountry(countryVM.Country, cityDB.Name, regionDB.Name);
             TempData["successMessage"] = string.Format($"{countryVM.Country.Name} has been successfully updated.");
             return(RedirectToAction("Index"));
         }
     }
     else
     {
         return(View(countryVM));
     }
 }