예제 #1
0
        public void UpdateUserEmpty()
        {
            // Arrange
            var userID = 1;

            var newUser = new PatchUserViewModel();

            var existingUser = MockDataGenerator.CreateUserViewModel(userID);

            var user = MockDataGenerator.CreateUser(userID);

            var mockUserRepo = new Mock <IUserRepository>();
            var mockLoanRepo = new Mock <ILoanRepository>();
            var mockMapper   = new Mock <IMapper>();

            mockUserRepo.Setup(f => f.UpdateUser(userID, It.IsAny <UserViewModel>()));
            mockUserRepo.Setup(f => f.GetUserByID(userID)).Returns(user);
            mockMapper.Setup(x => x.Map <PatchUserViewModel, UserViewModel>(It.IsAny <PatchUserViewModel>())).Returns(existingUser);

            var service = new UserService(mockUserRepo.Object, mockLoanRepo.Object, mockMapper.Object);

            // Act
            service.UpdateUser(userID, newUser);

            // Assert
            mockUserRepo.Verify(f => f.UpdateUser(userID, It.IsAny <UserViewModel>()), Times.Once());
            mockUserRepo.Verify(f => f.GetUserByID(userID), Times.Once());
            mockMapper.Verify(f => f.Map <PatchUserViewModel, UserViewModel>(It.IsAny <PatchUserViewModel>()), Times.Once());
        }
예제 #2
0
        public IActionResult PartiallyUpdateUser(int userID, [FromBody] PatchUserViewModel user)
        {
            if (!ModelState.IsValid)
            {
                var error = new ErrorDTO
                {
                    Code    = 412,
                    Message = "Validation error. Invalid input"
                };

                return(StatusCode(error.Code, error));
            }

            try
            {
                userService.UpdateUser(userID, user);

                return(NoContent());
            }
            catch (NotFoundException ex)
            {
                var error = new ErrorDTO
                {
                    Code    = 404,
                    Message = ex.Message
                };

                return(StatusCode(error.Code, error));
            }
            catch (AlreadyExistsException ex)
            {
                var error = new ErrorDTO
                {
                    Code    = 409,
                    Message = ex.Message
                };

                return(StatusCode(error.Code, error));
            }
            catch (Exception ex)
            {
                var error = new ErrorDTO
                {
                    Code    = 500,
                    Message = ex.Message
                };

                return(StatusCode(error.Code, error));
            }
        }
예제 #3
0
        public void UpdateUser(int userID, PatchUserViewModel user)
        {
            try
            {
                var oldUser = userRepository.GetUserByID(userID);

                MergeUsersForPatch(user, oldUser);

                var newUser = mapper.Map <PatchUserViewModel, UserViewModel>(user);

                userRepository.UpdateUser(userID, newUser);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #4
0
        private void MergeUsersForPatch(PatchUserViewModel user, UserDTO oldUser)
        {
            if (string.IsNullOrEmpty(user.Name))
            {
                user.Name = oldUser.Name;
            }

            if (string.IsNullOrEmpty(user.Address))
            {
                user.Address = oldUser.Address;
            }

            if (string.IsNullOrEmpty(user.Email))
            {
                user.Email = oldUser.Email;
            }
        }
예제 #5
0
        public async Task <IHttpActionResult> Patch(PatchUserViewModel model)
        {
            try
            {
                if (model == null)
                {
                    return(BadRequest());
                }

                var userDb = await _userService.GetByIdAsync(model.Id);

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


                var userViewModel = UserMapper.Map(userDb);


                model.Model.ApplyTo(userViewModel);


                var result = await _userService.UpdateAsync(UserMapper.Map(userViewModel, userDb));

                if (result.Status == ActionStatus.Updated)
                {
                    var returnMapper = UserMapper.Map(result.Entity);
                    return(Ok(returnMapper));
                }

                return(BadRequest());
            }
            catch (Exception)
            {
                return(InternalServerError());
            }
        }