Skip to content

Commit

Permalink
회원 정보 수정 기능 구현 (#76)
Browse files Browse the repository at this point in the history
* feat: jwt 라이브러리 추가

* fix: merge 과정에서 충돌 이슈 제거

* feat: 토큰 생성, 검증을 하는 TokenProvider 클래스 생성

* feat: SecurityConfig 클래스 추가

* feat: Jwt Filter 적용

* feat: UserDetailService 추가 및 Member에 메서드 추가

* feat: 로그인, 회원가입 시 필요한 dto 클래스 생성

* feat: 유저 회원가입 기능 구현

* feat: 유저 로그인 기능 구현

* feat: 공통 Exception 클래스들 추가

* refactor: LoginSuccessDto 추가

* feat: LoginSuccessDto 추가

* feat: 회원 정보 조회 기능 추가

* feat: 회원 정보 조회 기능 추가

* fix: securityConfig에서 posts url 허용

* feat: 회원 전체 목록 기능 구현

* feat: ErrorCode 적용

* feat: 단위 테스트 작성

* fix: postCount, postLike 값이 null이 들어가는 오류 수정

* refactor: 불필요한 코드 제거

* feat: 회원 로그인, 조회 예외 처리

* merge branch

* 댓글 추천 기능 구현 (#55)

* feat: 댓글 추천 기능 구현

* test: 댓글 추천 기능 단위 테스트

* test: 댓글 중복 추천 시 예외 처리 단위 테스트

* style: 각 코드의 구분자를 세미콜론에서 쉼표로 변경

* feat: 유저 정보를 업데이트 하는 메서드 추가

* refactor: merge 충돌 제거

* feat: 회원 정보 수정 기능 구현

* feat: 회원 정보 수정 단위 테스트 추가

* refactor: save 메서드 없이도 변경감지로 업데이트 적용

---------

Co-authored-by: Hanjaemo <[email protected]>
  • Loading branch information
jschoi-96 and Hanjaemo authored Feb 15, 2024
1 parent 95b6963 commit bac8179
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import balancetalk.global.jwt.JwtTokenProvider;
import balancetalk.module.member.domain.Member;
import balancetalk.module.member.domain.MemberRepository;
import balancetalk.module.member.dto.JoinDto;
import balancetalk.module.member.dto.LoginDto;
import balancetalk.module.member.dto.LoginSuccessDto;
import balancetalk.module.member.dto.MemberResponseDto;
import balancetalk.module.member.dto.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class MemberService {
Expand Down Expand Up @@ -51,7 +49,7 @@ public LoginSuccessDto login(final LoginDto loginDto) {
@Transactional(readOnly = true)
public MemberResponseDto findById(Long id) {
Member member = memberRepository.findById(id)
.orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_MEMBER));
.orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_MEMBER_INFORMATION));
return MemberResponseDto.fromEntity(member);
}

Expand All @@ -62,4 +60,12 @@ public List<MemberResponseDto> findAll() {
.map(MemberResponseDto::fromEntity)
.collect(Collectors.toList());
}

@Transactional
public MemberResponseDto update(Long memberId, final MemberUpdateDto memberUpdateDto) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_MEMBER));
member.updateMember(memberUpdateDto.getNickname(), memberUpdateDto.getPassword());
return MemberResponseDto.fromEntity(member);
}
}
5 changes: 5 additions & 0 deletions src/main/java/balancetalk/module/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public boolean isEnabled() {
return true;
}

public void updateMember(String nickname, String password) {
this.nickname = nickname;
this.password = password;
}

public int getPostCount() {
return Optional.ofNullable(posts)
.map(List::size).orElse(0);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/balancetalk/module/member/dto/MemberUpdateDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package balancetalk.module.member.dto;

import balancetalk.module.member.domain.Member;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MemberUpdateDto {
private String nickname;
private String password;

public Member toEntity() {
return Member.builder()
.nickname(nickname)
.password(password)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package balancetalk.module.member.presentation;

import balancetalk.module.member.application.MemberService;
import balancetalk.module.member.dto.JoinDto;
import balancetalk.module.member.dto.LoginDto;
import balancetalk.module.member.dto.LoginSuccessDto;
import balancetalk.module.member.dto.MemberResponseDto;
import balancetalk.module.member.dto.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -46,4 +43,9 @@ public List<MemberResponseDto> findAllMemberInfo() {
return memberService.findAll();
}

@ResponseStatus(HttpStatus.OK)
@PutMapping("/{memberId}")
public MemberResponseDto updateMemberInfo(@PathVariable("memberId") Long memberId, @Valid @RequestBody MemberUpdateDto memberUpdateDto) {
return memberService.update(memberId, memberUpdateDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import balancetalk.module.member.domain.Member;
import balancetalk.module.member.domain.MemberRepository;
import balancetalk.module.member.domain.Role;
import balancetalk.module.member.dto.JoinDto;
import balancetalk.module.member.dto.LoginDto;
import balancetalk.module.member.dto.LoginSuccessDto;
import balancetalk.module.member.dto.MemberResponseDto;
import balancetalk.module.member.dto.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -66,7 +63,8 @@ void LoginMember_Success() {
Member member = joinDto.toEntity();
when(memberRepository.findByEmail(any())).thenReturn(Optional.of(member));
when(jwtTokenProvider.createToken(member.getEmail(), member.getRole())).thenReturn(null);
// TODO: jwt 토큰 returns null
// TODO: jwt 토큰 null -> 인증 오류


// when
LoginSuccessDto result = memberService.login(loginDto);
Expand Down Expand Up @@ -114,4 +112,25 @@ void Member_All_Information_Success() {
assertThat(result.get(0).getNickname()).isEqualTo("닉네임1");
assertThat(result.get(1).getNickname()).isEqualTo("닉네임2");
}

@Test
@DisplayName("회원 정보 수정 - 성공")
void Member_Update_Success() {
// given
Member member = joinDto.toEntity();
MemberUpdateDto memberUpdateDto = MemberUpdateDto.builder()
.nickname("새로운닉네임")
.password("Testcase1234!")
.build();
// when
when(memberRepository.findById(any())).thenReturn(Optional.of(member));
member.updateMember(memberUpdateDto.getNickname(), memberUpdateDto.getPassword());
when(memberRepository.save(any())).thenReturn(member);

memberService.update(member.getId(), memberUpdateDto);

// then
assertThat(member.getNickname()).isEqualTo(memberUpdateDto.getNickname());
assertThat(member.getPassword()).isEqualTo(memberUpdateDto.getPassword());
}
}

0 comments on commit bac8179

Please sign in to comment.