Esempio n. 1
0
        public IActionResult CreateCountry([FromBody] Country countryToCareate)
        {
            if (countryToCareate == null)
            {
                return(BadRequest(ModelState));
            }

            var country = _countryRepo.GetCountries().Where(c => c.Name.Trim().ToUpper() == countryToCareate.Name.Trim().ToUpper()).Count();

            if (country > 0)
            {
                ModelState.AddModelError("", $"Country {countryToCareate.Name} is aleardy exists");
                return(StatusCode(422, ModelState));
            }

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

            if (!_countryRepo.CreateCaountry(countryToCareate))
            {
                ModelState.AddModelError("", $"Somthing went wrong during save {countryToCareate.Name}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetCountry", new { countryId = countryToCareate.Id }, countryToCareate));
        }