Skip to content

Commit

Permalink
Merge pull request #12 from gooormmoon/feature/core/GRTEAM-4
Browse files Browse the repository at this point in the history
회원 조회 기능 추가 GRTEAM-4
  • Loading branch information
Berygna authored Jun 14, 2024
2 parents 1d99490 + 4e8e235 commit 7276320
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,55 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/member")
public class MemberController {

private final MemberService memberService;

@PostMapping("/register")
@PostMapping("/api/member/register")
public ResponseEntity<MemberResponse> register(@RequestBody @Valid MemberRequest.RegisterRequest registerRequest) {
return ResponseEntity.ok().body(memberService.register(registerRequest));
return ResponseEntity.ok(memberService.register(registerRequest));
}

@ExceptionHandler(DuplicateLoginIdException.class)
public ResponseEntity<ExceptionResponse> handleDuplicateLoginIdException(DuplicateLoginIdException exception) {
return ResponseEntity.badRequest().body(new ExceptionResponse(exception.getMessage()));
}

@PostMapping("/login")
@PostMapping("/api/member/login")
public ResponseEntity<TokenResponse> login(@RequestBody @Valid MemberRequest.LoginRequest loginRequest) {
return ResponseEntity.ok().body(memberService.authenticate(loginRequest.getId(), loginRequest.getPassword()));
return ResponseEntity.ok(memberService.authenticate(loginRequest.getId(), loginRequest.getPassword()));
}

@GetMapping("")
@GetMapping("/api/member")
public ResponseEntity<MemberResponse> myInfo(Authentication auth) {
MemberResponse memberResponse = memberService.getMemberResponseByLoginId(auth.getName());
return ResponseEntity.ok().body(memberResponse);
MemberResponse memberResponse = memberService.getMyInfo(auth.getName());
return ResponseEntity.ok(memberResponse);
}

@PutMapping("/change-password")
@PutMapping("/api/member/change-password")
public ResponseEntity<String> changePassword(Authentication auth, @RequestBody @Valid MemberRequest.ChangePasswordRequest changePasswordRequest) {
memberService.updateMemberPasswordByLoginId(auth.getName(), changePasswordRequest.getPassword());
memberService.updatePassword(auth.getName(), changePasswordRequest.getPassword());
return ResponseEntity.ok().build();
}

@PutMapping("")
@PutMapping("/api/member")
public ResponseEntity<MemberResponse> updateInfo(Authentication auth, @RequestBody @Valid MemberRequest memberRequest) {
MemberResponse memberResponse = memberService.updateMemberInfoByLoginId(auth.getName(), memberRequest);
return ResponseEntity.ok().body(memberResponse);
MemberResponse memberResponse = memberService.updateMemberInfo(auth.getName(), memberRequest);
return ResponseEntity.ok(memberResponse);
}

@DeleteMapping("")
@DeleteMapping("/api/member")
public ResponseEntity<String> deleteMember(Authentication auth, @RequestBody @Valid MemberRequest.LoginRequest deleteRequest) {
memberService.deleteMemberByLoginIdAndPassword(auth.getName(), deleteRequest.getPassword());
memberService.deleteMember(auth.getName(), deleteRequest.getPassword());
return ResponseEntity.ok().build();
}

@GetMapping("/api/members/{loginId}")
public ResponseEntity<MemberResponse> userInfo(@PathVariable String loginId) {
MemberResponse response = memberService.getInfo(loginId);
return ResponseEntity.ok(response);
}

@ExceptionHandler(UsernameNotFoundException.class)
public ResponseEntity<ExceptionResponse> handleUsernameNotFoundException(UsernameNotFoundException exception) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ExceptionResponse(exception.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ public TokenResponse authenticate(String loginId, String password) {
}
}

public MemberResponse getMemberResponseByLoginId(String loginId) {
public MemberResponse getMyInfo(String loginId) {
Optional<Member> optionalMember = memberRepository.findByLoginId(loginId);
if(optionalMember.isPresent())
return fromMember(optionalMember.get());
else
throw new UsernameNotFoundException("존재하지 않는 아이디입니다.");
}

public void updateMemberPasswordByLoginId(String loginId, String password) {
public MemberResponse getInfo(String loginId) {
MemberResponse memberResponse = getMyInfo(loginId);
memberResponse.setLoginDate(null);
return memberResponse;
}

public void updatePassword(String loginId, String password) {
Optional<Member> optionalMember = memberRepository.findByLoginId(loginId);
if(optionalMember.isPresent()) {
Member member = optionalMember.get();
Expand All @@ -77,7 +83,7 @@ public void updateMemberPasswordByLoginId(String loginId, String password) {
throw new UsernameNotFoundException("존재하지 않는 아이디입니다.");
}

public MemberResponse updateMemberInfoByLoginId(String loginId, MemberRequest memberRequest) {
public MemberResponse updateMemberInfo(String loginId, MemberRequest memberRequest) {
Optional<Member> optionalMember = memberRepository.findByLoginId(loginId);


Expand All @@ -96,7 +102,7 @@ public MemberResponse updateMemberInfoByLoginId(String loginId, MemberRequest me
throw new UsernameNotFoundException("존재하지 않는 아이디입니다.");
}

public void deleteMemberByLoginIdAndPassword(String loginId, String password) {
public void deleteMember(String loginId, String password) {
Optional<Member> optionalMember = memberRepository.findByLoginId(loginId);
if(optionalMember.isPresent() && optionalMember.get().getPassword().equals(password)) {
memberRepository.delete(optionalMember.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class MemberResponse {
private String profileImageUrl;
private String description;
private LocalDateTime createdDate;
@Setter
private LocalDateTime loginDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(requests ->
requests
.requestMatchers("/api/member/register", "/api/member/login").permitAll()
.requestMatchers("/api/member/register", "/api/member/login", "/api/members/*").permitAll()
.anyRequest().authenticated())
.addFilterBefore(new JwtFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ void deleteMemberTest() {
assertEquals("user", member.getBody().getId());
}

@Test
@DisplayName("다른 회원 조회")
void getMemberInfoTest() {
registerMember();
HttpHeaders headers = new HttpHeaders();

HttpEntity<Objects> request = new HttpEntity<>(null, headers);
ResponseEntity<MemberResponse> response = testRestTemplate.exchange("/api/members/user", HttpMethod.GET, request, MemberResponse.class);

assertEquals("Lee", response.getBody().getName());
}


ResponseEntity<MemberResponse> registerMember() {
MemberRequest.RegisterRequest registerRequest = MemberRequest.RegisterRequest.builder()
Expand Down

0 comments on commit 7276320

Please sign in to comment.