Exemplo n.º 1
0
        public async Task <IActionResult> CreateState([FromBody] StateDetailsDto stateResource)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var country = await _countryRepo.GetCountry(stateResource.CountryId);

                if (country == null)
                {
                    return(NotFound());
                }

                if (_stateRepo.CheckStateCode(stateResource.StateCode))
                {
                    ModelState.AddModelError("Error", "The State with this Code already exists.");
                    return(BadRequest(ModelState));
                }

                var state = _mapper.Map <StateDetailsDto, State>(stateResource);
                state.Deleted   = false;
                state.Protected = false;
                country.States.Add(state);
                await _unitOfWork.CompleteAsync();

                state = await _stateRepo.GetState(state.Id);

                var result = _mapper.Map <State, StateDetailsDto>(state);
                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing this operation.");
                return(BadRequest(ModelState));
            }
        }