Beispiel #1
0
        public IActionResult Patch(int id, [FromBody] JsonPatchDocument <UserDto> userPatchDocument)
        {
            try
            {
                if (userPatchDocument == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                UserEntity userEntity = _userRepository.GetSingle(id);

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

                UserDto existingUser = _userMapper.MapToDto(userEntity);

                userPatchDocument.ApplyTo(existingUser, ModelState);

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _userRepository.Update(_userMapper.MapToEntity(existingUser));

                return(Ok(existingUser));
            }
            catch (Exception exception)
            {
                //logg exception or do anything with it
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }