コード例 #1
0
        public async Task <IActionResult> CreateCountry([FromBody] Country country)
        {
            if (country == null)
            {
                throw new ArgumentNullException(nameof(country));
            }

            if (await _repo.CountryExistByName(country.Name))
            {
                ModelState.AddModelError("", $"Country {country.Name} already exists!");
                return(StatusCode(422, $"Country {country.Name} already exists!"));
            }


            if (!await _work.Add(country))
            {
                ModelState.AddModelError("", $"Something went wrong saving {country.Name}!!");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetCountry", new { countryId = country.Id }, country));
        }