From 5b2f88b47fb73c6fdad13352d1016b2af1c032c6 Mon Sep 17 00:00:00 2001 From: forrest1398 <129135885+forrest1398@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:10:26 +0900 Subject: [PATCH 1/6] =?UTF-8?q?refactor=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20Controller,=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=82=AD=EC=A0=9C=20(TokentController)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/MemberProfileServiceImpl.java | 18 ++------------ .../service/MemberProfileService.java | 2 -- .../presentation/TokenController.java | 24 ------------------- 3 files changed, 2 insertions(+), 42 deletions(-) delete mode 100644 src/main/java/jungle/HandTris/presentation/TokenController.java diff --git a/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java b/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java index d9b1626..09e6341 100644 --- a/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java +++ b/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java @@ -10,15 +10,14 @@ import jungle.HandTris.domain.repo.MemberRepository; import jungle.HandTris.global.jwt.JWTUtil; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberDetailRes; import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import org.springframework.beans.factory.annotation.Value; import java.io.IOException; @@ -45,19 +44,6 @@ public Pair getMemberProfileWithStatsByNickname(S return Pair.of(profileImageUrl, memberRecordDetails); } - @Override - public MemberDetailRes loadMemberProfileByToken(HttpServletRequest request) { - String accessToken = request.getHeader("Authorization").substring(7); - - String nickname = jwtUtil.getNickname(accessToken); - - Member member = memberRepository.findByNickname(nickname) - .orElseThrow(UserNotFoundException::new); - - String username = member.getUsername(); - return new MemberDetailRes(username, nickname); - } - @Override public Pair myPage(HttpServletRequest request, String username) { String token = jwtUtil.resolveAccessToken(request); @@ -110,7 +96,7 @@ public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest requ if (deleteProfileImage) { member.updateProfileImageUrl(defaultImage); } else if (profileImage != null && profileImage.getSize() > 0) { - // 이미지가 존재하는 경우에만 업데이트 + // 이미지가 존재하는 경우에만 업데이트 validateImage(profileImage); uploadImage(profileImage, member); } diff --git a/src/main/java/jungle/HandTris/application/service/MemberProfileService.java b/src/main/java/jungle/HandTris/application/service/MemberProfileService.java index 3a4d9fa..edf0451 100644 --- a/src/main/java/jungle/HandTris/application/service/MemberProfileService.java +++ b/src/main/java/jungle/HandTris/application/service/MemberProfileService.java @@ -2,7 +2,6 @@ import jakarta.servlet.http.HttpServletRequest; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberDetailRes; import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; @@ -12,7 +11,6 @@ public interface MemberProfileService { Pair getMemberProfileWithStatsByNickname(String nickname); - MemberDetailRes loadMemberProfileByToken(HttpServletRequest request); Pair myPage(HttpServletRequest request, String username); diff --git a/src/main/java/jungle/HandTris/presentation/TokenController.java b/src/main/java/jungle/HandTris/presentation/TokenController.java deleted file mode 100644 index bffb1c4..0000000 --- a/src/main/java/jungle/HandTris/presentation/TokenController.java +++ /dev/null @@ -1,24 +0,0 @@ -package jungle.HandTris.presentation; - -import jakarta.servlet.http.HttpServletRequest; -import jungle.HandTris.application.service.MemberProfileService; -import jungle.HandTris.global.dto.ResponseEnvelope; -import jungle.HandTris.presentation.dto.response.MemberDetailRes; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/token") -@RequiredArgsConstructor -public class TokenController { - private final MemberProfileService memberProfileService; - - @GetMapping("/member/info") - public ResponseEnvelope getMemberInfoToToken(HttpServletRequest request) { - MemberDetailRes memberDetail = memberProfileService.loadMemberProfileByToken(request); - - return ResponseEnvelope.of(memberDetail); - } -} From c94e54bf677475564e09875d467a4bfe4ecfe0b1 Mon Sep 17 00:00:00 2001 From: forrest1398 <129135885+forrest1398@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:13:54 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor=20:=20controller=EC=99=80=20servic?= =?UTF-8?q?e=20=EC=9D=B4=EB=A6=84=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= =?UTF-8?q?=20-=20MemberController=20->=20MemberProfileController=20-=20me?= =?UTF-8?q?mberService=20->=20AuthService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rServiceImpl.java => AuthServiceImpl.java} | 6 +- .../{MemberService.java => AuthService.java} | 2 +- .../service/ExpirationListener.java | 2 +- .../HandTris/presentation/AuthController.java | 10 +- ...ller.java => MemberProfileController.java} | 2 +- ...rServiceTest.java => AuthServiceTest.java} | 141 ++++++++---------- 6 files changed, 72 insertions(+), 91 deletions(-) rename src/main/java/jungle/HandTris/application/impl/{MemberServiceImpl.java => AuthServiceImpl.java} (94%) rename src/main/java/jungle/HandTris/application/service/{MemberService.java => AuthService.java} (92%) rename src/main/java/jungle/HandTris/presentation/{MemberController.java => MemberProfileController.java} (98%) rename src/test/java/jungle/HandTris/Member/{MemberServiceTest.java => AuthServiceTest.java} (81%) diff --git a/src/main/java/jungle/HandTris/application/impl/MemberServiceImpl.java b/src/main/java/jungle/HandTris/application/impl/AuthServiceImpl.java similarity index 94% rename from src/main/java/jungle/HandTris/application/impl/MemberServiceImpl.java rename to src/main/java/jungle/HandTris/application/impl/AuthServiceImpl.java index 95dc156..aa5dd6d 100644 --- a/src/main/java/jungle/HandTris/application/impl/MemberServiceImpl.java +++ b/src/main/java/jungle/HandTris/application/impl/AuthServiceImpl.java @@ -2,8 +2,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.transaction.Transactional; +import jungle.HandTris.application.service.AuthService; import jungle.HandTris.application.service.BCryptPasswordService; -import jungle.HandTris.application.service.MemberService; import jungle.HandTris.domain.Member; import jungle.HandTris.domain.MemberRecord; import jungle.HandTris.domain.exception.*; @@ -21,7 +21,7 @@ @Service @RequiredArgsConstructor @Transactional -public class MemberServiceImpl implements MemberService { +public class AuthServiceImpl implements AuthService { private final MemberRepository memberRepository; private final MemberRecordRepository memberRecordRepository; private final BCryptPasswordService bCryptPasswordService; @@ -30,7 +30,7 @@ public class MemberServiceImpl implements MemberService { private String defaultImage; @Override - public Pair signin (MemberRequest memberRequest) { + public Pair signin(MemberRequest memberRequest) { String username = memberRequest.username(); String password = memberRequest.password(); diff --git a/src/main/java/jungle/HandTris/application/service/MemberService.java b/src/main/java/jungle/HandTris/application/service/AuthService.java similarity index 92% rename from src/main/java/jungle/HandTris/application/service/MemberService.java rename to src/main/java/jungle/HandTris/application/service/AuthService.java index 4041574..6cb3a06 100644 --- a/src/main/java/jungle/HandTris/application/service/MemberService.java +++ b/src/main/java/jungle/HandTris/application/service/AuthService.java @@ -5,7 +5,7 @@ import jungle.HandTris.presentation.dto.request.MemberRequest; import org.springframework.data.util.Pair; -public interface MemberService { +public interface AuthService { Pair signin(MemberRequest memberRequest); void signup(MemberRequest memberRequest); diff --git a/src/main/java/jungle/HandTris/application/service/ExpirationListener.java b/src/main/java/jungle/HandTris/application/service/ExpirationListener.java index 1334abb..4714f39 100644 --- a/src/main/java/jungle/HandTris/application/service/ExpirationListener.java +++ b/src/main/java/jungle/HandTris/application/service/ExpirationListener.java @@ -11,7 +11,7 @@ @Component public class ExpirationListener implements MessageListener { - private final MemberService memberService; + private final AuthService authService; @Override public void onMessage(Message message, byte[] pattern) { diff --git a/src/main/java/jungle/HandTris/presentation/AuthController.java b/src/main/java/jungle/HandTris/presentation/AuthController.java index 9de81d6..f476ef0 100644 --- a/src/main/java/jungle/HandTris/presentation/AuthController.java +++ b/src/main/java/jungle/HandTris/presentation/AuthController.java @@ -2,7 +2,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; -import jungle.HandTris.application.service.MemberService; +import jungle.HandTris.application.service.AuthService; import jungle.HandTris.domain.Member; import jungle.HandTris.global.dto.ResponseEnvelope; import jungle.HandTris.presentation.dto.request.MemberRequest; @@ -17,19 +17,19 @@ @RequiredArgsConstructor public class AuthController { - private final MemberService memberService; + private final AuthService authService; @PostMapping("/signup") @ResponseStatus(HttpStatus.CREATED) public ResponseEnvelope signup(@RequestBody @Valid MemberRequest memberRequest) { - memberService.signup(memberRequest); + authService.signup(memberRequest); return ResponseEnvelope.of("Signup Successful"); } @PostMapping("/signin") public ResponseEnvelope signin(@RequestBody MemberRequest memberRequest) { - Pair result = memberService.signin(memberRequest); + Pair result = authService.signin(memberRequest); Member member = result.getFirst(); String accessToken = result.getSecond(); @@ -46,7 +46,7 @@ public ResponseEnvelope signin(@RequestBody MemberR @GetMapping("/signout") public ResponseEnvelope signout(HttpServletRequest request) { - memberService.signout(request); + authService.signout(request); return ResponseEnvelope.of("Signout Successful"); } diff --git a/src/main/java/jungle/HandTris/presentation/MemberController.java b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java similarity index 98% rename from src/main/java/jungle/HandTris/presentation/MemberController.java rename to src/main/java/jungle/HandTris/presentation/MemberProfileController.java index f82a850..ddb390d 100644 --- a/src/main/java/jungle/HandTris/presentation/MemberController.java +++ b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java @@ -16,7 +16,7 @@ @RestController @RequiredArgsConstructor @RequestMapping("/member") -public class MemberController { +public class MemberProfileController { private final MemberProfileService memberProfileService; @GetMapping("/{username}/mypage") diff --git a/src/test/java/jungle/HandTris/Member/MemberServiceTest.java b/src/test/java/jungle/HandTris/Member/AuthServiceTest.java similarity index 81% rename from src/test/java/jungle/HandTris/Member/MemberServiceTest.java rename to src/test/java/jungle/HandTris/Member/AuthServiceTest.java index 3eb428f..2a0e6ff 100644 --- a/src/test/java/jungle/HandTris/Member/MemberServiceTest.java +++ b/src/test/java/jungle/HandTris/Member/AuthServiceTest.java @@ -3,16 +3,15 @@ import jakarta.transaction.Transactional; import jakarta.validation.ConstraintViolation; import jakarta.validation.Validator; +import jungle.HandTris.application.service.AuthService; import jungle.HandTris.application.service.MemberProfileService; import jungle.HandTris.application.service.MemberRecordService; -import jungle.HandTris.application.service.MemberService; import jungle.HandTris.domain.Member; import jungle.HandTris.domain.MemberRecord; import jungle.HandTris.domain.exception.*; import jungle.HandTris.domain.repo.MemberRepository; import jungle.HandTris.presentation.dto.request.MemberRequest; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberDetailRes; import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; @@ -28,20 +27,23 @@ import org.springframework.data.util.Pair; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import java.util.Set; @SpringBootTest @Transactional -public class MemberServiceTest { - - @Autowired MemberService memberService; - @Autowired MemberProfileService memberProfileService; - @Autowired MemberRecordService memberRecordService; - @Autowired MemberRepository memberRepository; - @Autowired BCryptPasswordEncoder bCryptPasswordEncoder; - @Autowired private Validator validator; +public class AuthServiceTest { + + @Autowired + AuthService authService; + @Autowired + MemberProfileService memberProfileService; + @Autowired + MemberRecordService memberRecordService; + @Autowired + MemberRepository memberRepository; + @Autowired + private Validator validator; @Nested @DisplayName("회원 가입") @@ -54,13 +56,12 @@ public void testSignupSuccess() { MemberRequest member = new MemberRequest("user1", "1q2w3e4r!", "user1"); // when - memberService.signup(member); + authService.signup(member); // then Member findMember = memberRepository.findByUsername(member.username()); Assertions.assertThat(findMember.getUsername()).isEqualTo(member.username()); - Assertions.assertThat(bCryptPasswordEncoder.matches(member.password(), findMember.getPassword())).isTrue(); Assertions.assertThat(findMember.getNickname()).isEqualTo(member.nickname()); } @@ -72,15 +73,15 @@ class Username { @Test @DisplayName("중복 Username") public void testSignupFailWithDuplicateUsername() { - // given - MemberRequest member1 = new MemberRequest("user1", "1q2w3e4r!", "user1"); - MemberRequest member2 = new MemberRequest("user1", "1q2w3e4r!", "user2"); - memberService.signup(member1); + // given + MemberRequest member1 = new MemberRequest("user1", "1q2w3e4r!", "user1"); + MemberRequest member2 = new MemberRequest("user1", "1q2w3e4r!", "user2"); + authService.signup(member1); - // when & then + // when & then - Assertions.assertThatThrownBy(() -> memberService.signup(member2)) - .isInstanceOf(DuplicateUsernameException.class); + Assertions.assertThatThrownBy(() -> authService.signup(member2)) + .isInstanceOf(DuplicateUsernameException.class); } @Test @@ -165,11 +166,11 @@ public void testSignupFailWithDuplicateNickname() { // given MemberRequest member1 = new MemberRequest("user1", "1q2w3e4r!", "user1"); MemberRequest member2 = new MemberRequest("user2", "1q2w3e4r!", "user1"); - memberService.signup(member1); + authService.signup(member1); // when & then - Assertions.assertThatThrownBy(() -> memberService.signup(member2)) + Assertions.assertThatThrownBy(() -> authService.signup(member2)) .isInstanceOf(DuplicateNicknameException.class); } @@ -217,7 +218,7 @@ public void testSignupFailWithSpecialCharacterNickname() { } @ParameterizedTest - @ValueSource(strings ={"admin", "administator", "useradmin", "adminuser"}) + @ValueSource(strings = {"admin", "administator", "useradmin", "adminuser"}) @DisplayName("admin 작성된 Nickname") public void testSignupFailWithAdminNickname(String nickname) { // given @@ -243,10 +244,10 @@ class Signin { public void testLoginSuccess() { // given MemberRequest member = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(member); + authService.signup(member); // when - Pair result = memberService.signin(member); + Pair result = authService.signin(member); // then Assertions.assertThat(result).isNotNull(); @@ -266,7 +267,7 @@ public void testLoginFailWithNonexistentUsername() { MemberRequest member = new MemberRequest("user1", "1q2w3e4r!", "user1"); // when & then - Assertions.assertThatThrownBy(() -> memberService.signin(member)) + Assertions.assertThatThrownBy(() -> authService.signin(member)) .isInstanceOf(UserNotFoundException.class); } @@ -276,12 +277,12 @@ public void testLoginFailWithNonexistentUsername() { public void testLoginFailWithIncorrectPassword() { // given MemberRequest member = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(member); + authService.signup(member); // when MemberRequest incorrectMember = new MemberRequest("user1", "wrongPassword", "user1"); - Assertions.assertThatThrownBy(() -> memberService.signin(incorrectMember)) + Assertions.assertThatThrownBy(() -> authService.signin(incorrectMember)) .isInstanceOf(PasswordMismatchException.class); // then @@ -293,11 +294,11 @@ public void testLoginFailWithIncorrectPassword() { class MyPage { @Test @DisplayName("정상 마이페이지 호출") - public void testMyPageSuccess () { + public void testMyPageSuccess() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -319,46 +320,26 @@ public void testMyPageSuccess () { @Test @DisplayName("A유저가 B유저의 마이페이지 호출") - public void testMyPageConnectWrongUser () { + public void testMyPageConnectWrongUser() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - String token = memberService.signin(requestMember).getSecond(); + authService.signup(requestMember); + String token = authService.signin(requestMember).getSecond(); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Authorization", "Bearer " + token); // when & then Assertions.assertThatThrownBy(() -> { - Pair result = memberProfileService.myPage(request, "user2"); - }).isInstanceOf(UnauthorizedAccessException.class); + Pair result = memberProfileService.myPage(request, "user2"); + }).isInstanceOf(UnauthorizedAccessException.class); } } @Nested @DisplayName("회원 정보 요청") class GetMemberProfile { - @Test - @DisplayName("회원 정보 요청") - public void testLoadMemberProfileByTokenSuccess () { - // given - MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); - Member member = SigninResult.getFirst(); - String token = SigninResult.getSecond(); - - MockHttpServletRequest request = new MockHttpServletRequest(); - request.addHeader("Authorization", "Bearer " + token); - - // when - MemberDetailRes profileResult = - memberProfileService.loadMemberProfileByToken(request); - // then - Assertions.assertThat(profileResult.nickname()).isEqualTo(member.getNickname()); - Assertions.assertThat(profileResult.username()).isEqualTo(member.getUsername()); - } } @Nested @@ -369,11 +350,11 @@ class ChangeMemberProfile { class ChangeMemberNickname { @Test @DisplayName("정상 닉네임 변경") - public void testChangeMemberNicknameSuccess () { + public void testChangeMemberNicknameSuccess() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -396,8 +377,8 @@ public void testChangeMemberNicknameSuccess () { public void testChangeMemberNicknameWithSameNickname() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -408,9 +389,9 @@ public void testChangeMemberNicknameWithSameNickname() { // when & then Assertions.assertThatThrownBy(() -> { - memberProfileService.updateMemberProfile( - request, changeNickname, null, null, member.getUsername()); - }).isInstanceOf(NicknameNotChangedException.class); + memberProfileService.updateMemberProfile( + request, changeNickname, null, null, member.getUsername()); + }).isInstanceOf(NicknameNotChangedException.class); } @Test @@ -419,9 +400,9 @@ public void testChangeMemberNicknameWithDuplicateNickname() { // given MemberRequest requestMember1 = new MemberRequest("user1", "1q2w3e4r!", "user1"); MemberRequest requestMember2 = new MemberRequest("user2", "1q2w3e4r!", "user2"); - memberService.signup(requestMember1); - memberService.signup(requestMember2); - Pair SigninResult = memberService.signin(requestMember1); + authService.signup(requestMember1); + authService.signup(requestMember2); + Pair SigninResult = authService.signin(requestMember1); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -432,9 +413,9 @@ public void testChangeMemberNicknameWithDuplicateNickname() { // when & then Assertions.assertThatThrownBy(() -> { - memberProfileService.updateMemberProfile( - request, changeNickname, null, null, member.getUsername()); - }).isInstanceOf(DuplicateNicknameException.class); + memberProfileService.updateMemberProfile( + request, changeNickname, null, null, member.getUsername()); + }).isInstanceOf(DuplicateNicknameException.class); } } @@ -447,11 +428,11 @@ class ChangeMemberProfileImage { @Test @DisplayName("정상 프로필 사진 변경") - public void testChangeMemberProfileImageSuccess () { + public void testChangeMemberProfileImageSuccess() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -479,11 +460,11 @@ public void testChangeMemberProfileImageSuccess () { @Test @DisplayName("정상 프로필 사진 제거") - public void testChangeMemberDefaultProfileImageSuccess () { + public void testChangeMemberDefaultProfileImageSuccess() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -516,8 +497,8 @@ public void testChangeMemberDefaultProfileImageSuccess () { public void testChangeMemberNicknameWithSameNickname() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); @@ -545,8 +526,8 @@ public void testChangeMemberNicknameWithSameNickname() { public void testChangeMemberNicknameWithDuplicateNickname() { // given MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1"); - memberService.signup(requestMember); - Pair SigninResult = memberService.signin(requestMember); + authService.signup(requestMember); + Pair SigninResult = authService.signin(requestMember); Member member = SigninResult.getFirst(); String token = SigninResult.getSecond(); From 3a1fef05a348d2f6c354356803fe05e26393a532 Mon Sep 17 00:00:00 2001 From: forrest1398 <129135885+forrest1398@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:38:38 +0900 Subject: [PATCH 3/6] =?UTF-8?q?refactor=20:=20=EA=B0=99=EC=9D=80=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EC=9D=98=20=ED=95=A8=EC=88=98=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=EC=A0=9C=EA=B1=B0=20&=20username=ED=99=95=EC=9D=B8?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/MemberProfileServiceImpl.java | 24 --------------- .../service/MemberProfileService.java | 4 --- .../presentation/MemberProfileController.java | 16 +++++----- .../presentation/MemberRecordController.java | 29 +++++++++---------- .../HandTris/Member/AuthServiceTest.java | 23 ++++++++------- 5 files changed, 35 insertions(+), 61 deletions(-) diff --git a/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java b/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java index 09e6341..8020f85 100644 --- a/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java +++ b/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java @@ -10,7 +10,6 @@ import jungle.HandTris.domain.repo.MemberRepository; import jungle.HandTris.global.jwt.JWTUtil; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; import lombok.RequiredArgsConstructor; @@ -44,25 +43,6 @@ public Pair getMemberProfileWithStatsByNickname(S return Pair.of(profileImageUrl, memberRecordDetails); } - @Override - public Pair myPage(HttpServletRequest request, String username) { - String token = jwtUtil.resolveAccessToken(request); - String nickname = jwtUtil.getNickname(token); - Member member = memberRepository.findByNickname(nickname) - .orElseThrow(MemberNotFoundException::new); - - // 요청한 유저와 토큰의 주인이 같은지 검증 - if (!username.equals(member.getUsername())) { - throw new UnauthorizedAccessException(); - } - - MemberProfileDetailsRes memberInfoDetails = new MemberProfileDetailsRes(member.getNickname(), member.getProfileImageUrl()); - MemberRecordDetailRes memberRecordDetails = new MemberRecordDetailRes(memberRecordService.getMemberRecord(member.getNickname())); - - return Pair.of(memberInfoDetails, memberRecordDetails); - } - - @Override public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage, String username) { @@ -72,10 +52,6 @@ public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest requ Member member = memberRepository.findByNickname(nickname) .orElseThrow(MemberNotFoundException::new); - // 요청한 유저와 토큰의 주인이 같은지 검증 - if (!username.equals(member.getUsername())) { - throw new UnauthorizedAccessException(); - } // 변경할 닉네임이 있을 경우에만 업데이트 if (memberUpdateReq.nickname() != null && !memberUpdateReq.nickname().isEmpty()) { diff --git a/src/main/java/jungle/HandTris/application/service/MemberProfileService.java b/src/main/java/jungle/HandTris/application/service/MemberProfileService.java index edf0451..7c38c32 100644 --- a/src/main/java/jungle/HandTris/application/service/MemberProfileService.java +++ b/src/main/java/jungle/HandTris/application/service/MemberProfileService.java @@ -2,7 +2,6 @@ import jakarta.servlet.http.HttpServletRequest; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; import org.springframework.data.util.Pair; @@ -11,8 +10,5 @@ public interface MemberProfileService { Pair getMemberProfileWithStatsByNickname(String nickname); - - Pair myPage(HttpServletRequest request, String username); - MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage, String username); } diff --git a/src/main/java/jungle/HandTris/presentation/MemberProfileController.java b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java index ddb390d..3bb5cd7 100644 --- a/src/main/java/jungle/HandTris/presentation/MemberProfileController.java +++ b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java @@ -3,13 +3,14 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import jungle.HandTris.application.service.MemberProfileService; +import jungle.HandTris.application.service.MemberRecordService; +import jungle.HandTris.domain.MemberRecord; import jungle.HandTris.global.dto.ResponseEnvelope; +import jungle.HandTris.global.validation.UserNicknameFromJwt; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; -import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; +import jungle.HandTris.presentation.dto.response.ParticipantRes; import lombok.RequiredArgsConstructor; -import org.springframework.data.util.Pair; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -18,12 +19,13 @@ @RequestMapping("/member") public class MemberProfileController { private final MemberProfileService memberProfileService; + private final MemberRecordService memberRecordService; @GetMapping("/{username}/mypage") - public ResponseEnvelope> myPage(HttpServletRequest request, @PathVariable("username") String username) { - Pair result = memberProfileService.myPage(request, username); - - return ResponseEnvelope.of(result); + public ResponseEnvelope myPage(@UserNicknameFromJwt String nickname) { + MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); + ParticipantRes participantRes = new ParticipantRes(memberRecord); + return ResponseEnvelope.of(participantRes); } @PatchMapping("/{username}/mypage") diff --git a/src/main/java/jungle/HandTris/presentation/MemberRecordController.java b/src/main/java/jungle/HandTris/presentation/MemberRecordController.java index 66dc3c7..29fb042 100644 --- a/src/main/java/jungle/HandTris/presentation/MemberRecordController.java +++ b/src/main/java/jungle/HandTris/presentation/MemberRecordController.java @@ -6,7 +6,6 @@ import jungle.HandTris.global.dto.ResponseEnvelope; import jungle.HandTris.global.validation.UserNicknameFromJwt; import jungle.HandTris.presentation.dto.request.GameResultReq; -import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; import jungle.HandTris.presentation.dto.response.ParticipantRes; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -22,20 +21,27 @@ public class MemberRecordController { private final MemberRecordService memberRecordService; - @GetMapping("/{nickname}") - public ResponseEnvelope getMemberRecord(@PathVariable("nickname") String nickname) { + @GetMapping + public ResponseEnvelope getMyRecord(@UserNicknameFromJwt String nickname) { MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); - MemberRecordDetailRes memberRecordDetailRes = new MemberRecordDetailRes(memberRecord); - return ResponseEnvelope.of(memberRecordDetailRes); + ParticipantRes participantRes = new ParticipantRes(memberRecord); + return ResponseEnvelope.of(participantRes); } @PutMapping - public ResponseEnvelope updateMemberRecord(@Valid @RequestBody GameResultReq gameResultReq, @UserNicknameFromJwt String nickname) { + public ResponseEnvelope updateMemberRecord(@Valid @RequestBody GameResultReq gameResultReq, @UserNicknameFromJwt String nickname) { MemberRecord memberRecord = memberRecordService.updateMemberRecord(gameResultReq, nickname); - MemberRecordDetailRes memberRecordDetailRes = new MemberRecordDetailRes(memberRecord); - return ResponseEnvelope.of(memberRecordDetailRes); + ParticipantRes participantRes = new ParticipantRes(memberRecord); + return ResponseEnvelope.of(participantRes); } + @GetMapping("/{nickname}") + public ResponseEnvelope getMemberRecord(@PathVariable("nickname") String nickname) { + MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); + ParticipantRes participantRes = new ParticipantRes(memberRecord); + return ResponseEnvelope.of(participantRes); + } + @GetMapping("/participant/{roomCode}") public ResponseEnvelope> getParticipants(@PathVariable("roomCode") String roomCode, @UserNicknameFromJwt String nickname) { List participantsMemberRecord = memberRecordService.getParticipants(roomCode, nickname); @@ -48,11 +54,4 @@ public ResponseEnvelope> getParticipants(@PathVariable("roo } return ResponseEnvelope.of(participantResList); } - - @GetMapping - public ResponseEnvelope getMyRecord(@UserNicknameFromJwt String nickname) { - MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); - ParticipantRes participantRes = new ParticipantRes(memberRecord); - return ResponseEnvelope.of(participantRes); - } } diff --git a/src/test/java/jungle/HandTris/Member/AuthServiceTest.java b/src/test/java/jungle/HandTris/Member/AuthServiceTest.java index 2a0e6ff..0573b6a 100644 --- a/src/test/java/jungle/HandTris/Member/AuthServiceTest.java +++ b/src/test/java/jungle/HandTris/Member/AuthServiceTest.java @@ -12,7 +12,6 @@ import jungle.HandTris.domain.repo.MemberRepository; import jungle.HandTris.presentation.dto.request.MemberRequest; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; -import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes; import org.assertj.core.api.Assertions; @@ -309,13 +308,14 @@ public void testMyPageSuccess() { MemberRecordDetailRes memberRecordDetailRes = new MemberRecordDetailRes(memberRecord); // when - Pair profileResult = - memberProfileService.myPage(request, member.getUsername()); - - // then - Assertions.assertThat(profileResult.getFirst().nickname()).isEqualTo(member.getNickname()); - Assertions.assertThat(profileResult.getFirst().profileImageUrl()).isEqualTo(member.getProfileImageUrl()); - Assertions.assertThat(profileResult.getSecond()).isEqualTo(memberRecordDetailRes); + // todo : myPage 함수 삭제됨 +// Pair profileResult = +// memberProfileService.myPage(request, member.getUsername()); +// +// // then +// Assertions.assertThat(profileResult.getFirst().nickname()).isEqualTo(member.getNickname()); +// Assertions.assertThat(profileResult.getFirst().profileImageUrl()).isEqualTo(member.getProfileImageUrl()); +// Assertions.assertThat(profileResult.getSecond()).isEqualTo(memberRecordDetailRes); } @Test @@ -330,9 +330,10 @@ public void testMyPageConnectWrongUser() { request.addHeader("Authorization", "Bearer " + token); // when & then - Assertions.assertThatThrownBy(() -> { - Pair result = memberProfileService.myPage(request, "user2"); - }).isInstanceOf(UnauthorizedAccessException.class); + // todo : myPage 함수 삭제됨 +// Assertions.assertThatThrownBy(() -> { +// Pair result = memberProfileService.myPage(request, "user2"); +// }).isInstanceOf(UnauthorizedAccessException.class); } } From 2d78fb57cdf9902617972752128dcb298ba1f90b Mon Sep 17 00:00:00 2001 From: forrest1398 <129135885+forrest1398@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:47:39 +0900 Subject: [PATCH 4/6] =?UTF-8?q?refactor=20:=20username=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/MemberProfileServiceImpl.java | 2 +- .../service/MemberProfileService.java | 2 +- .../presentation/MemberProfileController.java | 10 +++++----- .../jungle/HandTris/Member/AuthServiceTest.java | 16 ++++++++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java b/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java index 8020f85..33df3b8 100644 --- a/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java +++ b/src/main/java/jungle/HandTris/application/impl/MemberProfileServiceImpl.java @@ -44,7 +44,7 @@ public Pair getMemberProfileWithStatsByNickname(S } @Override - public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage, String username) { + public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage) { Boolean nicknameChanged = false; String token = jwtUtil.resolveAccessToken(request); diff --git a/src/main/java/jungle/HandTris/application/service/MemberProfileService.java b/src/main/java/jungle/HandTris/application/service/MemberProfileService.java index 7c38c32..51e59a0 100644 --- a/src/main/java/jungle/HandTris/application/service/MemberProfileService.java +++ b/src/main/java/jungle/HandTris/application/service/MemberProfileService.java @@ -10,5 +10,5 @@ public interface MemberProfileService { Pair getMemberProfileWithStatsByNickname(String nickname); - MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage, String username); + MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage); } diff --git a/src/main/java/jungle/HandTris/presentation/MemberProfileController.java b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java index 3bb5cd7..9932bfe 100644 --- a/src/main/java/jungle/HandTris/presentation/MemberProfileController.java +++ b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java @@ -21,22 +21,22 @@ public class MemberProfileController { private final MemberProfileService memberProfileService; private final MemberRecordService memberRecordService; - @GetMapping("/{username}/mypage") + @GetMapping("/mypage") public ResponseEnvelope myPage(@UserNicknameFromJwt String nickname) { MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); ParticipantRes participantRes = new ParticipantRes(memberRecord); return ResponseEnvelope.of(participantRes); } - @PatchMapping("/{username}/mypage") + @PatchMapping("/mypage") public ResponseEnvelope updateInfo( HttpServletRequest request, @Valid MemberUpdateReq memberUpdateReq, @RequestPart(value = "profileImage", required = false) MultipartFile profileImage, - @RequestParam(value = "deleteProfileImage", required = false, defaultValue = "false") boolean deleteProfileImage, - @PathVariable("username") String username) { + @RequestParam(value = "deleteProfileImage", required = false, defaultValue = "false") boolean deleteProfileImage + ) { - MemberProfileUpdateDetailsRes updateMemberDetails = memberProfileService.updateMemberProfile(request, memberUpdateReq, profileImage, deleteProfileImage, username); + MemberProfileUpdateDetailsRes updateMemberDetails = memberProfileService.updateMemberProfile(request, memberUpdateReq, profileImage, deleteProfileImage); return ResponseEnvelope.of(updateMemberDetails); } diff --git a/src/test/java/jungle/HandTris/Member/AuthServiceTest.java b/src/test/java/jungle/HandTris/Member/AuthServiceTest.java index 0573b6a..0db6cb3 100644 --- a/src/test/java/jungle/HandTris/Member/AuthServiceTest.java +++ b/src/test/java/jungle/HandTris/Member/AuthServiceTest.java @@ -366,7 +366,7 @@ public void testChangeMemberNicknameSuccess() { // when MemberProfileUpdateDetailsRes changeMemberProfile = memberProfileService.updateMemberProfile( - request, changeNickname, null, false, member.getUsername()); + request, changeNickname, null, false); // then Assertions.assertThat(changeMemberProfile.nickname()).isEqualTo(member.getNickname()); @@ -391,7 +391,7 @@ public void testChangeMemberNicknameWithSameNickname() { // when & then Assertions.assertThatThrownBy(() -> { memberProfileService.updateMemberProfile( - request, changeNickname, null, null, member.getUsername()); + request, changeNickname, null, null); }).isInstanceOf(NicknameNotChangedException.class); } @@ -415,7 +415,7 @@ public void testChangeMemberNicknameWithDuplicateNickname() { // when & then Assertions.assertThatThrownBy(() -> { memberProfileService.updateMemberProfile( - request, changeNickname, null, null, member.getUsername()); + request, changeNickname, null, null); }).isInstanceOf(DuplicateNicknameException.class); } } @@ -453,7 +453,7 @@ public void testChangeMemberProfileImageSuccess() { // when MemberProfileUpdateDetailsRes changeMemberProfile = memberProfileService.updateMemberProfile( - request, changeNickname, changeProfileImage, false, member.getUsername()); + request, changeNickname, changeProfileImage, false); // then Assertions.assertThat(changeMemberProfile.profileImageUrl()).isEqualTo(member.getProfileImageUrl()); @@ -482,13 +482,13 @@ public void testChangeMemberDefaultProfileImageSuccess() { ); MemberProfileUpdateDetailsRes changeMemberProfile = memberProfileService.updateMemberProfile( - request, changeNickname, changeProfileImage, false, member.getUsername()); + request, changeNickname, changeProfileImage, false); Assertions.assertThat(changeMemberProfile.profileImageUrl()).isEqualTo(member.getProfileImageUrl()); // when memberProfileService.updateMemberProfile( - request, changeNickname, null, true, member.getUsername()); + request, changeNickname, null, true); // then Assertions.assertThat(defaultImage).isEqualTo(member.getProfileImageUrl()); } @@ -518,7 +518,7 @@ public void testChangeMemberNicknameWithSameNickname() { // when & then Assertions.assertThatThrownBy(() -> { memberProfileService.updateMemberProfile( - request, changeNickname, changeProfileImage, false, member.getUsername()); + request, changeNickname, changeProfileImage, false); }).isInstanceOf(InvalidImageTypeException.class); } @@ -551,7 +551,7 @@ public void testChangeMemberNicknameWithDuplicateNickname() { // when & then Assertions.assertThatThrownBy(() -> { memberProfileService.updateMemberProfile( - request, changeNickname, changeProfileImage, false, member.getUsername()); + request, changeNickname, changeProfileImage, false); }).isInstanceOf(InvalidImageTypeException.class); } } From f3b19d6f19376f6e10c9e7296b9ceb5c7ac23770 Mon Sep 17 00:00:00 2001 From: forrest1398 <129135885+forrest1398@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:51:04 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor=20:=20ParticipantRes=20DTO=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=AA=85=20=EB=B3=80=EA=B2=BD(->Mem?= =?UTF-8?q?berProfileRes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/MemberProfileController.java | 8 ++--- .../presentation/MemberRecordController.java | 32 +++++++++---------- ...ticipantRes.java => MemberProfileRes.java} | 4 +-- 3 files changed, 22 insertions(+), 22 deletions(-) rename src/main/java/jungle/HandTris/presentation/dto/response/{ParticipantRes.java => MemberProfileRes.java} (85%) diff --git a/src/main/java/jungle/HandTris/presentation/MemberProfileController.java b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java index 9932bfe..77b1792 100644 --- a/src/main/java/jungle/HandTris/presentation/MemberProfileController.java +++ b/src/main/java/jungle/HandTris/presentation/MemberProfileController.java @@ -8,8 +8,8 @@ import jungle.HandTris.global.dto.ResponseEnvelope; import jungle.HandTris.global.validation.UserNicknameFromJwt; import jungle.HandTris.presentation.dto.request.MemberUpdateReq; +import jungle.HandTris.presentation.dto.response.MemberProfileRes; import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes; -import jungle.HandTris.presentation.dto.response.ParticipantRes; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -22,10 +22,10 @@ public class MemberProfileController { private final MemberRecordService memberRecordService; @GetMapping("/mypage") - public ResponseEnvelope myPage(@UserNicknameFromJwt String nickname) { + public ResponseEnvelope myPage(@UserNicknameFromJwt String nickname) { MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); - ParticipantRes participantRes = new ParticipantRes(memberRecord); - return ResponseEnvelope.of(participantRes); + MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord); + return ResponseEnvelope.of(memberProfileRes); } @PatchMapping("/mypage") diff --git a/src/main/java/jungle/HandTris/presentation/MemberRecordController.java b/src/main/java/jungle/HandTris/presentation/MemberRecordController.java index 29fb042..aa24d73 100644 --- a/src/main/java/jungle/HandTris/presentation/MemberRecordController.java +++ b/src/main/java/jungle/HandTris/presentation/MemberRecordController.java @@ -6,7 +6,7 @@ import jungle.HandTris.global.dto.ResponseEnvelope; import jungle.HandTris.global.validation.UserNicknameFromJwt; import jungle.HandTris.presentation.dto.request.GameResultReq; -import jungle.HandTris.presentation.dto.response.ParticipantRes; +import jungle.HandTris.presentation.dto.response.MemberProfileRes; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -22,36 +22,36 @@ public class MemberRecordController { private final MemberRecordService memberRecordService; @GetMapping - public ResponseEnvelope getMyRecord(@UserNicknameFromJwt String nickname) { + public ResponseEnvelope getMyRecord(@UserNicknameFromJwt String nickname) { MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); - ParticipantRes participantRes = new ParticipantRes(memberRecord); - return ResponseEnvelope.of(participantRes); + MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord); + return ResponseEnvelope.of(memberProfileRes); } @PutMapping - public ResponseEnvelope updateMemberRecord(@Valid @RequestBody GameResultReq gameResultReq, @UserNicknameFromJwt String nickname) { + public ResponseEnvelope updateMemberRecord(@Valid @RequestBody GameResultReq gameResultReq, @UserNicknameFromJwt String nickname) { MemberRecord memberRecord = memberRecordService.updateMemberRecord(gameResultReq, nickname); - ParticipantRes participantRes = new ParticipantRes(memberRecord); - return ResponseEnvelope.of(participantRes); + MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord); + return ResponseEnvelope.of(memberProfileRes); } @GetMapping("/{nickname}") - public ResponseEnvelope getMemberRecord(@PathVariable("nickname") String nickname) { + public ResponseEnvelope getMemberRecord(@PathVariable("nickname") String nickname) { MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname); - ParticipantRes participantRes = new ParticipantRes(memberRecord); - return ResponseEnvelope.of(participantRes); + MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord); + return ResponseEnvelope.of(memberProfileRes); } - + @GetMapping("/participant/{roomCode}") - public ResponseEnvelope> getParticipants(@PathVariable("roomCode") String roomCode, @UserNicknameFromJwt String nickname) { + public ResponseEnvelope> getParticipants(@PathVariable("roomCode") String roomCode, @UserNicknameFromJwt String nickname) { List participantsMemberRecord = memberRecordService.getParticipants(roomCode, nickname); - List participantResList = new ArrayList<>(); + List memberProfileResList = new ArrayList<>(); for (MemberRecord memberRecord : participantsMemberRecord) { if (memberRecord == null) continue; - ParticipantRes participantRes = new ParticipantRes(memberRecord); - participantResList.add(participantRes); + MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord); + memberProfileResList.add(memberProfileRes); } - return ResponseEnvelope.of(participantResList); + return ResponseEnvelope.of(memberProfileResList); } } diff --git a/src/main/java/jungle/HandTris/presentation/dto/response/ParticipantRes.java b/src/main/java/jungle/HandTris/presentation/dto/response/MemberProfileRes.java similarity index 85% rename from src/main/java/jungle/HandTris/presentation/dto/response/ParticipantRes.java rename to src/main/java/jungle/HandTris/presentation/dto/response/MemberProfileRes.java index a236467..6212ec3 100644 --- a/src/main/java/jungle/HandTris/presentation/dto/response/ParticipantRes.java +++ b/src/main/java/jungle/HandTris/presentation/dto/response/MemberProfileRes.java @@ -4,14 +4,14 @@ import java.math.BigDecimal; -public record ParticipantRes( +public record MemberProfileRes( String nickname, String profileImageUrl, long win, long lose, BigDecimal winRate ) { - public ParticipantRes(MemberRecord memberRecord) { + public MemberProfileRes(MemberRecord memberRecord) { this( memberRecord.getMember().getNickname(), memberRecord.getMember().getProfileImageUrl(), From d0450f6330c9c10e6f512c9e219c84b849be7ff8 Mon Sep 17 00:00:00 2001 From: forrest1398 <129135885+forrest1398@users.noreply.github.com> Date: Fri, 12 Jul 2024 22:11:44 +0900 Subject: [PATCH 6/6] =?UTF-8?q?fix=20:=20=ED=87=B4=EC=9E=A5=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=EC=97=90=20=EB=A7=9E=EA=B2=8C=20disconnection=20?= =?UTF-8?q?=EC=83=81=ED=99=A9=EC=97=90=EC=84=9C=EC=9D=98=20case=EB=A5=BC?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HandTris/presentation/TetrisController.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/jungle/HandTris/presentation/TetrisController.java b/src/main/java/jungle/HandTris/presentation/TetrisController.java index 3b91bf4..255235a 100644 --- a/src/main/java/jungle/HandTris/presentation/TetrisController.java +++ b/src/main/java/jungle/HandTris/presentation/TetrisController.java @@ -2,6 +2,7 @@ import jungle.HandTris.application.service.GameRoomService; import jungle.HandTris.application.service.TetrisService; +import jungle.HandTris.domain.GameRoom; import jungle.HandTris.presentation.dto.request.RoomStateReq; import jungle.HandTris.presentation.dto.request.TetrisMessageReq; import jungle.HandTris.presentation.dto.response.RoomOwnerRes; @@ -54,6 +55,7 @@ public void roomOwnerInfo(@DestinationVariable("roomCode") String roomCode, Simp @MessageMapping("/{roomCode}/disconnect") public void handleDisconnect(SimpMessageHeaderAccessor headerAccessor, @DestinationVariable(value = "roomCode") String roomCode) { System.out.println("\n========================================= controller disconnect send ========================================="); + // playing 중인 게임에서 탈주한 경우 // message에서 isStart 확인 boolean isStart = headerAccessor.getFirstNativeHeader("isStart").equals("true"); @@ -68,13 +70,15 @@ public void handleDisconnect(SimpMessageHeaderAccessor headerAccessor, @Destinat // DB 최신화 System.out.println("DB 최신화"); String user = headerAccessor.getHeader("User").toString(); - gameRoomService.exitGameRoom(user, roomCode); - - // 방장 최신화 : DB 최신화 아래 있어야 한다. - System.out.println("방장 최신화"); - RoomOwnerRes roomOwnerRes = tetrisService.checkRoomOwnerAndReady(roomCode); - messagingTemplate.convertAndSend("/topic/owner/" + roomCode, roomOwnerRes); + GameRoom gameRoom = gameRoomService.exitGameRoom(user, roomCode); + // 참여자가 나갔지만 누군가 남아 있다면 + if (gameRoom.getParticipantCount() != 0) { + // 방장 최신화 : DB 최신화 아래 있어야 한다. + System.out.println("방장 최신화"); + RoomOwnerRes roomOwnerRes = tetrisService.checkRoomOwnerAndReady(roomCode); + messagingTemplate.convertAndSend("/topic/owner/" + roomCode, roomOwnerRes); + } System.out.println("\n========================================= disconnect send 종료 ========================================="); }