Ejemplo 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));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> EditState([FromRoute] Guid id, [FromBody] StateDetailsDto stateResource)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var state = await _stateRepo.GetState(id);

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

                if (state.Protected == true)
                {
                    ModelState.AddModelError("Error", "This record is a protected record. It can not be edited");
                    return(BadRequest(ModelState));
                }

                stateResource.StateCode = state.Code;
                _mapper.Map <StateDetailsDto, State>(stateResource, state);
                await _unitOfWork.CompleteAsync();

                state = await _stateRepo.GetState(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));
            }
        }