コード例 #1
0
        public IActionResult CreateDirector([FromBody] Director createDirector)
        {
            if (createDirector == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //As Director entity model also has reference to Moview & DIrector object due to entity relationship, need to populate these both objects

            if (!countryRepository.CountryExists(createDirector.Country.Id)) //Id comes from View as hidden input when user selects Country from e.g. dropdown
            {
                ModelState.AddModelError("", "Country doesn't exists");
            }
            if (!ModelState.IsValid)
            {
                return(StatusCode(404, ModelState));
            }
            createDirector.Country = countryRepository.GetCountry(createDirector.Country.Id); //Id comes from View as hidden input when user selects Critic from e.g. dropdown

            if (!directorRepository.CreateDirector(createDirector))
            {
                ModelState.AddModelError("", "Something went wrong, Please try again");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetDirector", new { directorID = createDirector.Id }, createDirector));
        }