public async Task <IActionResult> CreateRegion(
            [FromBody] Models.RegionForCreation regionToCreate)
        {
            if (regionToCreate == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            regionToCreate.RegionId = await regionToCreate.GenerateNewRegionId(_regionRepository);

            var regionEntity = _mapper.Map <Regions>(regionToCreate);

            _regionRepository.AddRegion(regionEntity);

            await _regionRepository.SaveChanges();

            await _regionRepository.GetRegion(regionEntity.RegionId);

            return(Ok(CreatedAtRoute("GetRegion",
                                     new { regionId = regionEntity.RegionId },
                                     _mapper.Map <Models.Regions>(regionEntity))));
        }
Example #2
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));
     }
 }
Example #3
0
        /// <summary>
        /// Adds new country.
        /// </summary>
        /// <param name="countryModel">Country view model.</param>
        /// <returns></returns>
        public async Task AddCountry(CountryModel countryModel)
        {
            // Check if city already exists.
            var city = await cityRepository.GetCityByName(countryModel.Capital);

            if (city == null)
            {
                // City not found, add it and get Id.
                await cityRepository.AddCity(new City { Name = countryModel.Capital });

                city = await cityRepository.GetCityByName(countryModel.Capital);
            }

            // Check if region already exists.
            var region = await regionRepository.GetRegionByName(countryModel.Region);

            if (region == null)
            {
                // Region not found, add it and get Id.
                await regionRepository.AddRegion(new Region { Name = countryModel.Region });

                region = await regionRepository.GetRegionByName(countryModel.Region);
            }

            // Check if country with the provided code already exists.
            var countryExtracted = await countryRepository.GetCountryByCode(countryModel.Code);

            if (countryExtracted == null)
            {
                // Country by code not found, add new country.
                await countryRepository.AddCountry(new Country
                {
                    Name       = countryModel.Name,
                    Code       = countryModel.Code,
                    Area       = countryModel.Area,
                    Population = countryModel.Population,
                    RegionId   = region.Id,
                    CapitalId  = city.Id,
                });
            }
            else
            {
                // Update existing country.
                countryExtracted.Name       = countryModel.Name;
                countryExtracted.Code       = countryModel.Code;
                countryExtracted.Area       = countryModel.Area;
                countryExtracted.Population = countryModel.Population;
                countryExtracted.RegionId   = region.Id;
                countryExtracted.CapitalId  = city.Id;
                countryExtracted.Region     = region;
                countryExtracted.Capital    = city;

                await countryRepository.UpdateCountry(countryExtracted);
            }
        }
        public HttpResponseMessage Post([FromBody] Models.region mRegion)
        {
            try
            {
                if (string.IsNullOrEmpty(mRegion.region_name))
                {
                    var formatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "error", msg = "Region is Empty"
                    }, formatter));
                }
                if (mRegion.region_code.Length != 3)
                {
                    var formatter = RequestFormat.JsonFormaterString();
                    return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                        output = "error", msg = "Region code must be 3 character"
                    }, formatter));
                }
                else
                {
                    if (_regionRepository.CheckDuplicateRegions(mRegion))
                    {
                        var formatter = RequestFormat.JsonFormaterString();
                        return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                            output = "error", msg = "Region Already Exists"
                        }, formatter));
                    }
                    else
                    {
                        region insert_region = new region
                        {
                            region_name = mRegion.region_name,
                            region_code = mRegion.region_code,
                            is_active   = mRegion.is_active,
                            created_by  = mRegion.created_by,
                            updated_by  = mRegion.updated_by
                        };

                        _regionRepository.AddRegion(insert_region);
                        var formatter = RequestFormat.JsonFormaterString();
                        return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                            output = "success", msg = "Region save successfully"
                        }, formatter));
                    }
                }
            }
            catch (Exception ex)
            {
                var formatter = RequestFormat.JsonFormaterString();
                return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                    output = "error", msg = ex.ToString()
                }, formatter));
            }
        }
Example #5
0
        public Country SaveCountry(CountryDTO countryDTO)
        {
            Country country = new Country
            {
                Name       = countryDTO.Name,
                Code       = countryDTO.Code,
                Area       = countryDTO.Area,
                Population = countryDTO.Population,

                Capital = new City {
                    Name = countryDTO.Capital
                },
                Region = new Region {
                    Name = countryDTO.Region
                }
            };
            var capital = FindCityInDB(country.Capital.Name);

            if (capital == null)
            {
                capital = _cityRepository.AddCity(country.Capital);
            }
            country.CapitalId = capital.Id;
            country.Capital   = capital;

            var region = FindRegionInDB(country.Region.Name);

            if (region == null)
            {
                region = _regionRepository.AddRegion(country.Region);
            }
            country.RegionId = region.Id;
            country.Region   = region;

            var countryInDB = FindCountryInDB(country.Code);

            if (countryInDB == null)
            {
                return(_countryRepository.AddCountry(country));
            }
            else
            {
                return(_countryRepository.UpdateCountry(countryInDB));
            }
        }
Example #6
0
        public IActionResult CreateRegion([FromBody] Region region)
        {
            if (region == null)
            {
                return(BadRequest());
            }

            if (region.Name == string.Empty)
            {
                ModelState.AddModelError("Name", "The name of the Region shouldn't be empty");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var createdEmployee = _regionRepository.AddRegion(region);

            return(Created("region", region));
        }
 public void AddRegion(Region region)
 {
     regionRepo.AddRegion(region);
 }