Ejemplo n.º 1
0
        public IActionResult CreateCounty(int stateId,
                                          [FromBody] CountyForCreationDto county)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_myAppRepository.StateExists(stateId))
            {
                return(NotFound());
            }

            var finalCounty = _mapper.Map <Entities.County>(county);

            _myAppRepository.AddCountyForState(stateId, finalCounty);
            _myAppRepository.Save();

            var createdCountyToReturn = _mapper
                                        .Map <Models.CountyDto>(finalCounty);

            return(CreatedAtRoute(
                       nameof(GetCounty),
                       new { stateId, id = createdCountyToReturn.Id },
                       createdCountyToReturn));
        }