public IActionResult CreateCountry([FromBody] Country countryToCreate)
        {
            if (countryToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            //check if country with this name already exists
            var countryExists = _countryRepository.GetCountries().Any(c =>
                                                                      c.Name.Trim().ToUpper().Equals(countryToCreate.Name.Trim().ToUpper()));

            if (countryExists)
            {
                ModelState.AddModelError("", $"Country {countryToCreate.Name} already Exists");
                return(StatusCode(422, ModelState));
            }

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

            if (!_countryRepository.CreateCountry(countryToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong saving {countryToCreate.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetCountry", new { countryId = countryToCreate.Id }, countryToCreate));
        }
        public IActionResult CreateCountry([FromBody] Country CountryToCreate)
        {
            if (CountryToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            var Country = _countryRepository.GetCountries().Where(c => c.Name.Trim().ToUpper() == CountryToCreate.Name.Trim().ToUpper()).FirstOrDefault();

            if (Country != null)
            {
                ModelState.AddModelError("", $"Country {CountryToCreate.Name} already exists");
                // return StatusCode(422, ModelState); // User can use the error type and create a new custom message from the error type
                return(StatusCode(422, $"Country {CountryToCreate.Name} already exists")); // Custom message for the user
            }

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

            if (!_countryRepository.CreateCountry(CountryToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong saving {CountryToCreate.Name}");
                return(StatusCode(500, ModelState));
            }
            // return RedirectToAction("GetCountries");
            return(CreatedAtRoute("GetCountry", new { countryId = CountryToCreate.Id }, CountryToCreate));
        }
        public IActionResult CreateCountry([FromBody] Country countryToCreate) //[FromBody] means all the info comes from the body of the post request.
        {
            if (countryToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            var country = _countryRepository.GetCountries().Where(c => c.Name.Trim().ToUpper() == countryToCreate.Name.Trim().ToUpper()).FirstOrDefault(); //check if the country already exists or not.

            if (country != null)
            {
                ModelState.AddModelError("", $"Country {countryToCreate.Name} already exists!");
                return(StatusCode(422, ModelState));
            }

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


            if (!_countryRepository.CreateCountry(countryToCreate)) // here we try to add the new country to the DB.
            {
                // if the create was not success.
                ModelState.AddModelError("", $"Something want wrong saving {countryToCreate.Name} ");
                return(StatusCode(500, ModelState));
            }
            // if the create success.

            return(CreatedAtRoute("GetCountry", new { countryId = countryToCreate.Id }, countryToCreate)); // return the info of the new country. call GetCountry() in line 55.
        }
Esempio n. 4
0
        public IActionResult CreateCountry([FromBody] Country countryToCreate)
        {
            if (countryToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            var country = _countryRepository.GetCountries().Where(c => c.Name.Trim().ToUpper() == countryToCreate.Name.Trim().ToUpper()).FirstOrDefault();

            if (country != null)
            {
                ModelState.AddModelError("", $"Country {countryToCreate.Name} already exists.");
                return(StatusCode(422, ModelState));
            }

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

            if (!_countryRepository.CreateCountry(countryToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong saving {countryToCreate.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetCountry", new { countryId = countryToCreate.Id }, countryToCreate));
        }
        public IActionResult CreateCountry([FromBody] Country countryToCreate)
        {
            if (countryToCreate == null)
            {
                return(BadRequest(ModelState));
            }
            var country = _countryRepository.GetCountries().Where(b => b.Name.Trim().ToUpper() == countryToCreate.Name.Trim().ToUpper()).FirstOrDefault();

            if (country != null)
            {
                ModelState.AddModelError("", $"Country {countryToCreate.Name} already exists");
                return(StatusCode(422, ModelState)); // status code 422 - the object is unprocessable
                // return StatusCode(422, $"Country {countryToCreate.Name} already exists");
            }

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

            if (!_countryRepository.CreateCountry(countryToCreate))
            {
                ModelState.AddModelError("", $"Sonething went wrong saving {countryToCreate.Name}");
                return(StatusCode(500, ModelState)); //Server error
            }
            //After all went Ok, we want to route back to the source i.e. we will display the obejct which we wanted to save.
            return(CreatedAtRoute("GetCountry", new { countryId = countryToCreate.Id }, countryToCreate));
        }
        public IActionResult CreateCountry([FromBody] Country createCountry) //FromBody indicates the object is coming from body/form
        {
            if (createCountry == null)
            {
                return(BadRequest(ModelState));
            }

            //to avoid duplication entry ofthe country Name
            var country = countryRepository.GetCountries().Where(c => c.Name.Trim().ToUpper() == createCountry.Name.Trim().ToUpper()).FirstOrDefault();

            if (country != null)
            {
                ModelState.AddModelError("", "Country you are trying to enter already exists");
                return(StatusCode(422, $"Country {country.Name} you are trying to enter already exists"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!countryRepository.CreateCountry(createCountry))
            {
                ModelState.AddModelError("", "Something Went wrong");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetCountry", new { countryId = createCountry.Id }, createCountry));
        }
Esempio n. 7
0
        public IActionResult CreateCountry([FromBody] CountryDto countryDto)
        {
            if (countryDto == null)
            {
                return(BadRequest());
            }
            if (countryRepo.CountryExists(countryDto.CountryName))
            {
                ModelState.AddModelError("", "Naziv drzave vec postoji!");
                return(StatusCode(404, ModelState));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var countryObj = mapper.Map <Country>(countryDto);

            if (!countryRepo.CreateCountry(countryObj))
            {
                ModelState.AddModelError("", $"Doslo je do greske u spasavanju {countryObj.CountryName}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetCountry", new { id = countryObj.CountryId }, countryObj));
        }
Esempio n. 8
0
        public CountryCreatingConfirmation CreateCountry(Country country)
        {
            CountryCreatingConfirmation createdCountry = _countryRepository.CreateCountry(country);

            _countryRepository.SaveChanges();
            return(createdCountry);
        }
 public IActionResult Create(Country Country)
 {
     if (ModelState.IsValid)
     {
         Country.CreatedBy = _admin.Fullname;
         _CountryRepository.CreateCountry(Country);
         return(RedirectToAction("index"));
     }
     return(View(Country));
 }
Esempio n. 10
0
        public IActionResult CreateCountry(CountryCreateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            _countryRepository.CreateCountry(request);
            return(Ok());
        }
        public void OnPost()
        {
            var    countryName = Request.Form["countryName"];
            var    capitalName = Request.Form["capitalName"];
            string path        = "/images/Flags/" + file.FileName;

            using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
            {
                file.CopyTo(fileStream);
            }

            _countryRepository.CreateCountry(new Country {
                CountryName = countryName,
                CapitalName = capitalName,
                Flag        = file.FileName,
                MainLetter  = countryName.ToString()[0]
            });
        }
Esempio n. 12
0
 public IActionResult CreateCountry([FromBody] Country country)
 {
     if (country == null || !ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (_countryRepository.IsDuplicateCountryName(country.Id, country.Name))
     {
         ModelState.AddModelError("", $"Country {country.Name} already exists");
         return(StatusCode(422, $"Country {country.Name} already exists"));
     }
     if (!_countryRepository.CreateCountry(country))
     {
         ModelState.AddModelError("", $"Something went wrong savig {country.Name}");
         return(StatusCode(500, ModelState));
     }
     return(CreatedAtRoute("GetCountry", new { countryId = country.Id }, country));
 }
Esempio n. 13
0
        // The Country object will come from the "body" of the post request
        public IActionResult CreateCountry([FromBody] Country countryToCreate)
        {
            // Id will be automatically added by the Database

            // Check if country is null
            if (countryToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            // Check if another country with that same name already exists (Duplicate Name)
            var country = _countryRepository.GetCountries()
                          .Where(c => c.Name.Trim().ToUpper() == countryToCreate.Name.Trim().ToUpper()).FirstOrDefault();

            // If it does,
            if (country != null)
            {
                // return a custom error message after adding the found country to the ModelState
                ModelState.AddModelError("", $"Country {country.Name} already exists");
                // return an Unprocessable Entity
                // ModelState is the response I see in Postman when the Status returns 422. It could be whatever I want
                // Returning ModelState is good for Debugging
                return(StatusCode(422, ModelState));
            }

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

            // Check if the country creation in the Database was successfull
            if (!_countryRepository.CreateCountry(countryToCreate))
            {
                // If the function returns null, add another error
                ModelState.AddModelError("", $"Something went wrong saving {countryToCreate.Name}");
                return(StatusCode(500, ModelState));
            }

            // If we reach that point, the country was successfully created, so we can redirect to that new country's Route
            // Pass in the countryId as a parameter to the Route (country is now saved in the database so has an id)
            // Pass in the actual country object as well
            return(CreatedAtRoute("GetCountry", new { countryId = countryToCreate.Id }, countryToCreate));
        }
        public async Task <IActionResult> PostMtnCountry([FromBody] Country mtnCountry)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var response = await _countyRepository.CreateCountry(mtnCountry);


                return(new JsonResult(response));
            }
            catch (Exception ex)
            {
                var response = new CountryResponse {
                    status = false, message = "Error occured on page", countryList = null
                };
                return(new JsonResult(response));
            }
        }
Esempio n. 15
0
        public IActionResult CreateCountry([FromBody] Country countryToCreate)
        {
            if (countryToCreate == null)
            {
                return(BadRequest(ModelState));
            }
            var country = _countryRepository.GetCountries().Where(c => c.Name == countryToCreate.Name).FirstOrDefault();

            if (country != null)
            {
                ModelState.AddModelError("", $"Country with name {countryToCreate.Name} already exist.");
                return(StatusCode(222, ModelState));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_countryRepository.CreateCountry(countryToCreate))
            {
                ModelState.AddModelError("", $"Error occur while saving country {countryToCreate.Name}.");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetCountry", new { CountryId = countryToCreate.Id }, countryToCreate));
        }
Esempio n. 16
0
        public IActionResult CreateCountry([FromBody] Country countrytoCreate)
        {
            if (countrytoCreate == null)
            {
                return(BadRequest());
            }

            if (_countryRepository.isDuplicateCountryName(countrytoCreate.Id, countrytoCreate.Name))
            {
                return(BadRequest(ModelState));
            }

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

            if (!_countryRepository.CreateCountry(countrytoCreate))
            {
                return(BadRequest(ModelState));
            }

            return(CreatedAtRoute("GetCountry", new { countryId = countrytoCreate.Id }, countrytoCreate));
        }
        public IActionResult CreateCountry([FromBody] Country country)
        {
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }
            var existingCountry = _countryRepository.GetCountries()
                                  .Where(c => c.Name.ToLower() == country.Name.ToLower())
                                  .FirstOrDefault();

            if (existingCountry != null)
            {
                ModelState.AddModelError("", $"country name {country.Name} already exists");
                return(StatusCode(422, ModelState));
            }

            if (!_countryRepository.CreateCountry(country))
            {
                ModelState.AddModelError("", $"country{country.Name} fail to create");
                return(StatusCode(422, ModelState));
            }

            return(CreatedAtRoute("GetCountry", new { CountryId = country.CountryId }, country));
        }
Esempio n. 18
0
 public int CreateCountry(Country country)
 {
     return(_countryRepo.CreateCountry(country));
 }