From b64fe5f8335663bd9d110bdb2ecff70ad908f709 Mon Sep 17 00:00:00 2001 From: namhyo01 Date: Wed, 18 Oct 2023 17:12:04 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(BE):=20Security=20UserID=EB=A1=9C=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20#114?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/auth/adapter/in/AuthController.java | 16 +---------- .../example/api/auth/domain/SecurityUser.java | 23 ++++++++++++++++ .../api/auth/filter/JwtAuthFilter.java | 7 ++--- .../api/common/utils/AuthenticationUtils.java | 7 ++--- .../example/api/user/service/UserService.java | 27 ++++++++++--------- 5 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/example/api/auth/domain/SecurityUser.java diff --git a/src/main/java/com/example/api/auth/adapter/in/AuthController.java b/src/main/java/com/example/api/auth/adapter/in/AuthController.java index d98bb08..5f54011 100644 --- a/src/main/java/com/example/api/auth/adapter/in/AuthController.java +++ b/src/main/java/com/example/api/auth/adapter/in/AuthController.java @@ -3,6 +3,7 @@ import com.example.api.auth.application.port.in.LogoutUsecase; import com.example.api.auth.application.port.in.FindRefreshUsecase; +import com.example.api.auth.domain.SecurityUser; import com.example.api.auth.service.JwtUtilService; import com.example.api.auth.type.RefreshToken; import com.example.api.auth.type.TokenResponseStatus; @@ -70,19 +71,4 @@ public ResponseEntity refresh(@CookieValue String access_to return ResponseEntity.badRequest().body(TokenResponseStatus.addStatus(400,null)); } - - @GetMapping("/test") - public User test(Principal principal){ - User user = AuthenticationUtils.getCurrentUserAuthentication(); -// UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - log.info("안녕"); - log.info(user.toString()); - log.info(user.getUserId().toString() ); -// log.info(userDetails.getUsername()); -// log.info(userDetails.getPassword()); - if(user != null){ - log.info(user.getUserId().toString()); - } - return user; - } } \ No newline at end of file diff --git a/src/main/java/com/example/api/auth/domain/SecurityUser.java b/src/main/java/com/example/api/auth/domain/SecurityUser.java new file mode 100644 index 0000000..d9f8d32 --- /dev/null +++ b/src/main/java/com/example/api/auth/domain/SecurityUser.java @@ -0,0 +1,23 @@ +package com.example.api.auth.domain; +import lombok.Builder; +import lombok.Getter; + +import java.util.UUID; + +@Builder +@Getter +public class SecurityUser { + private UUID userId; + + private String naverId; + + private String kakaoId; + + private String googleId; + + private String instaId; + + private String appleId; + + private String role; +} diff --git a/src/main/java/com/example/api/auth/filter/JwtAuthFilter.java b/src/main/java/com/example/api/auth/filter/JwtAuthFilter.java index 7ba5d28..7d91ac4 100644 --- a/src/main/java/com/example/api/auth/filter/JwtAuthFilter.java +++ b/src/main/java/com/example/api/auth/filter/JwtAuthFilter.java @@ -1,6 +1,7 @@ package com.example.api.auth.filter; +import com.example.api.auth.domain.SecurityUser; import com.example.api.auth.dto.SecurityUserDto; import com.example.api.auth.service.JwtUtilService; import com.example.api.chat.exception.JwtException; @@ -83,7 +84,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse // UserEntity user = userService.findSocialUser(jwtUtilService.getId(atc), jwtUtilService.getProvider(atc)) // .orElseThrow(IllegalStateException::new); // Security Context에 등록할 user 객체 생성 - User userinfo = userService.findSocialUser(jwtUtilService.getId(atc), jwtUtilService.getProvider(atc)); + SecurityUser userinfo = userService.findSocialUser(jwtUtilService.getId(atc), jwtUtilService.getProvider(atc)); // Security Context에 인증 객체 등록 Authentication authentication = getAuthentication(userinfo); @@ -94,10 +95,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse filterChain.doFilter(request, response); } - public Authentication getAuthentication(User user){ + public Authentication getAuthentication(SecurityUser user){ return new UsernamePasswordAuthenticationToken(user, "", - List.of(new SimpleGrantedAuthority(user.getRole().toString())) + List.of(new SimpleGrantedAuthority(user.getRole())) ); } } \ No newline at end of file diff --git a/src/main/java/com/example/api/common/utils/AuthenticationUtils.java b/src/main/java/com/example/api/common/utils/AuthenticationUtils.java index 4a8e841..123ac4d 100644 --- a/src/main/java/com/example/api/common/utils/AuthenticationUtils.java +++ b/src/main/java/com/example/api/common/utils/AuthenticationUtils.java @@ -1,5 +1,6 @@ package com.example.api.common.utils; +import com.example.api.auth.domain.SecurityUser; import com.example.api.user.domain.User; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -9,10 +10,10 @@ * 이걸로 유저 정보를 가져오자 */ public class AuthenticationUtils { - public static User getCurrentUserAuthentication(){ + public static SecurityUser getCurrentUserAuthentication(){ Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication != null && authentication.getPrincipal() instanceof User){ - return (User) authentication.getPrincipal(); + if (authentication != null && authentication.getPrincipal() instanceof SecurityUser){ + return (SecurityUser) authentication.getPrincipal(); } return null; } diff --git a/src/main/java/com/example/api/user/service/UserService.java b/src/main/java/com/example/api/user/service/UserService.java index 6f4942f..e5f0364 100644 --- a/src/main/java/com/example/api/user/service/UserService.java +++ b/src/main/java/com/example/api/user/service/UserService.java @@ -1,5 +1,6 @@ package com.example.api.user.service; +import com.example.api.auth.domain.SecurityUser; import com.example.api.auth.dto.SecurityUserDto; import com.example.api.common.exception.CustomException; import com.example.api.common.type.ErrorCodeEnum; @@ -97,20 +98,22 @@ public void deleteUser(String userId) { // Social - public User findSocialUser(String id, String provider) { - return userMapper.toDomain(findUserPort.findSocialUser(id, provider).orElseThrow(IllegalStateException::new)); -// return SecurityUserDto.builder() -// .userId(user.getUserId()) -// .naverId(user.getSocialId().getNaverId()) -// .appleId(user.getSocialId().getAppleId()) -// .kakaoId(user.getSocialId().getKakaoId()) -// .googleId(user.getSocialId().getGoogleId()) -// .instaId(user.getSocialId().getInstaId()) -// .role(user.getRole().getRole()) -// .build(); + public SecurityUser findSocialUser(String id, String provider) { + User user = userMapper.toDomain(findUserPort.findSocialUser(id, provider).orElseThrow(IllegalStateException::new)); + + return SecurityUser.builder() + .userId(user.getUserId()) + .naverId(user.getSocialId().getNaverId()) + .appleId(user.getSocialId().getAppleId()) + .kakaoId(user.getSocialId().getKakaoId()) + .googleId(user.getSocialId().getGoogleId()) + .instaId(user.getSocialId().getInstaId()) + .role(user.getRole().getRole()) + .build(); + } public Optional findUserSigned(Long id) { return findUserPort.findUserSigned(id); } -} \ No newline at end of file +} From b936452fb1c665d6ee2e3a3b7be97c1101e6f593 Mon Sep 17 00:00:00 2001 From: Lemonade255 Date: Thu, 19 Oct 2023 12:32:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat(BE):=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=ED=95=9C=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=9E=90=EB=8F=99=20?= =?UTF-8?q?=EC=9D=B8=EC=8B=9D=20#115?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/auth/adapter/in/AuthController.java | 9 +-- .../chat/adapter/in/rest/ChatController.java | 12 ++-- .../adapter/in/rest/ChatRoomController.java | 14 ++--- .../adapter/in/rest/FriendController.java | 7 +-- .../port/in/FindFriendUsecase.java | 2 +- .../api/friend/service/FriendService.java | 20 ++++--- .../port/in/FindMatchingUsecase.java | 4 +- .../port/in/MatchingApplicationUsecase.java | 2 +- .../service/MatchingApplicationService.java | 22 ++++---- .../api/matching/service/MatchingService.java | 29 ++++++---- .../adapter/in/rest/MemberController.java | 2 +- .../api/member/service/MemberService.java | 2 +- .../in/rest/NotificationController.java | 7 +-- .../out/persistence/NotificationEntity.java | 11 ---- .../NotificationMapperInterface.java | 1 + .../NotificationPersistenceAdapter.java | 9 +-- .../persistence/UserNotificationEntity.java | 3 +- .../out/persistence/UserNotificationPK.java | 3 +- .../port/in/FindNotificationUsecase.java | 2 +- .../port/out/FindNotificationPort.java | 3 +- .../repository/NotificationRepository.java | 2 +- .../UserNotificationRepository.java | 5 +- .../service/NotificationService.java | 17 ++++-- .../adapter/in/rest/PreferenceController.java | 14 ++--- .../port/in/ComparePreferenceUsecase.java | 3 +- .../port/in/UserPreferenceUsecase.java | 2 +- .../preference/service/PreferenceService.java | 25 +++++---- .../service/UserPreferenceService.java | 16 +++--- .../sms/adapter/in/rest/SmsController.java | 15 ++--- .../user/adapter/in/rest/UserController.java | 55 ++++++++----------- .../port/in/DeleteUserUsecase.java | 2 +- .../port/in/RecommendedMatchingUsecase.java | 9 --- .../application/port/in/SaveUserUsecase.java | 2 +- .../example/api/user/service/UserService.java | 52 +++++++++++++----- .../example/api/user/UserControllerTests.java | 3 - 35 files changed, 197 insertions(+), 189 deletions(-) delete mode 100644 src/main/java/com/example/api/user/application/port/in/RecommendedMatchingUsecase.java diff --git a/src/main/java/com/example/api/auth/adapter/in/AuthController.java b/src/main/java/com/example/api/auth/adapter/in/AuthController.java index 5f54011..126448f 100644 --- a/src/main/java/com/example/api/auth/adapter/in/AuthController.java +++ b/src/main/java/com/example/api/auth/adapter/in/AuthController.java @@ -3,30 +3,23 @@ import com.example.api.auth.application.port.in.LogoutUsecase; import com.example.api.auth.application.port.in.FindRefreshUsecase; -import com.example.api.auth.domain.SecurityUser; import com.example.api.auth.service.JwtUtilService; import com.example.api.auth.type.RefreshToken; import com.example.api.auth.type.TokenResponseStatus; import com.example.api.auth.utils.CookieUtils; import com.example.api.common.dto.StatusResponseDto; -import com.example.api.common.utils.AuthenticationUtils; -import com.example.api.user.domain.User; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; -import java.security.Principal; import java.util.Optional; -import java.util.UUID; -@Slf4j @RestController +@Slf4j @RequiredArgsConstructor @RefreshScope public class AuthController { diff --git a/src/main/java/com/example/api/chat/adapter/in/rest/ChatController.java b/src/main/java/com/example/api/chat/adapter/in/rest/ChatController.java index 57b56db..c6963e8 100644 --- a/src/main/java/com/example/api/chat/adapter/in/rest/ChatController.java +++ b/src/main/java/com/example/api/chat/adapter/in/rest/ChatController.java @@ -23,8 +23,8 @@ import java.util.List; import java.util.UUID; -@Slf4j @RestController +@Slf4j @RequiredArgsConstructor @Tag(name = "Chat", description = "Chat API") public class ChatController { @@ -35,17 +35,17 @@ public class ChatController { /** * 추후에 jwt 인증을 통해 유저 데이터를 불러와 message에 추가할 예정 - * @param roomNumber (ID) + * @param roomId (ID) * @param message (데이터) */ @Operation(summary = "Send message", description = "채팅방에 메시지를 보낸다.") - @MessageMapping("/chat/{roomNumber}") - public void sendMessage(@DestinationVariable String roomNumber, AddChatDto message, String contentType, @RequestParam("file") MultipartFile file) { - log.info("roomNumber : {}", roomNumber); + @MessageMapping("/chat/{roomId}") + public void sendMessage(@DestinationVariable String roomId, AddChatDto message, String contentType, @RequestParam("file") MultipartFile file) { + log.info("roomId : {}", roomId); if (contentType.equals("image")) { message.setContent(uploadFileUsecase.uploadFile(file)); } - sendChatUsecase.send(roomNumber, message); + sendChatUsecase.send(roomId, message); } /** diff --git a/src/main/java/com/example/api/chatroom/adapter/in/rest/ChatRoomController.java b/src/main/java/com/example/api/chatroom/adapter/in/rest/ChatRoomController.java index 129fb3d..36b946a 100644 --- a/src/main/java/com/example/api/chatroom/adapter/in/rest/ChatRoomController.java +++ b/src/main/java/com/example/api/chatroom/adapter/in/rest/ChatRoomController.java @@ -16,9 +16,9 @@ import java.util.List; import java.util.UUID; -@Slf4j + @RestController -@RequestMapping("/chatroom") +@Slf4j @RequiredArgsConstructor @Tag(name = "ChatRoom", description = "ChatRoom API") public class ChatRoomController { @@ -26,11 +26,11 @@ public class ChatRoomController { private final FindChatRomListUsecase findChatRomListUsecase; /** * 채팅방 생성 - * @param createChatRoomDto (Data) - * @return 채팅 방 ID 값 + * @param createChatRoomDto (데이터) + * @return UUID */ @Operation(summary = "Create chatroom", description = "새로운 채팅방을 생성한다.") - @PostMapping + @PostMapping("/chatroom") public UUID createChatroom(@RequestBody @Valid CreateChatRoomDto createChatRoomDto){ ChatRoom chatRoom = createChatRoomUsecase.createRoom(createChatRoomDto); return chatRoom.getChatroomId(); @@ -42,13 +42,13 @@ public UUID createChatroom(@RequestBody @Valid CreateChatRoomDto createChatRoomD * @return List */ @Operation(summary = "Get chatroom list", description = "사용자의 채팅방 목록을 불러온다.") - @GetMapping + @GetMapping("/chatroom") public List chatRoomList(@RequestParam Long userid, @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) { //추후 바꾸자함 return findChatRomListUsecase.chatRoomList(pageable, userid); } @Operation(summary = "Delete chatroom", description = "채팅방을 삭제한다.") - @DeleteMapping(value = "/{chatRoomId}") + @DeleteMapping("/chatroom/{chatRoomId}") public void outChatRoom(@PathVariable UUID chatRoomId){ log.info("chatroom = {}", chatRoomId); } diff --git a/src/main/java/com/example/api/friend/adapter/in/rest/FriendController.java b/src/main/java/com/example/api/friend/adapter/in/rest/FriendController.java index e6e8eb4..d70d346 100644 --- a/src/main/java/com/example/api/friend/adapter/in/rest/FriendController.java +++ b/src/main/java/com/example/api/friend/adapter/in/rest/FriendController.java @@ -33,13 +33,12 @@ public FriendDto addFriend(@RequestBody FriendDto friendDto) { /** * 친구 목록 조회 - * @param userId (ID) * @return List */ @Operation(summary = "Get friend list", description = "사용자의 친구 목록을 조회한다.") - @GetMapping("/user/{userId}/friend") - public List getFriendList(@PathVariable String userId) { - return findFriendUsecase.getFriendList(userId); + @GetMapping("/user/friend") + public List getFriendList() { + return findFriendUsecase.getFriendList(); } /** diff --git a/src/main/java/com/example/api/friend/application/port/in/FindFriendUsecase.java b/src/main/java/com/example/api/friend/application/port/in/FindFriendUsecase.java index 07d1961..fe43c17 100644 --- a/src/main/java/com/example/api/friend/application/port/in/FindFriendUsecase.java +++ b/src/main/java/com/example/api/friend/application/port/in/FindFriendUsecase.java @@ -5,5 +5,5 @@ import java.util.List; public interface FindFriendUsecase { - List getFriendList(String userId); + List getFriendList(); } \ No newline at end of file diff --git a/src/main/java/com/example/api/friend/service/FriendService.java b/src/main/java/com/example/api/friend/service/FriendService.java index d1932a1..c9e2ab3 100644 --- a/src/main/java/com/example/api/friend/service/FriendService.java +++ b/src/main/java/com/example/api/friend/service/FriendService.java @@ -1,5 +1,7 @@ package com.example.api.friend.service; +import com.example.api.auth.domain.SecurityUser; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.friend.adapter.out.persistence.FriendEntity; import com.example.api.friend.adapter.out.persistence.FriendMapperInterface; import com.example.api.friend.application.port.in.AddFriendUsecase; @@ -20,7 +22,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.UUID; @Service @Slf4j @@ -42,15 +43,16 @@ public FriendDto addFriend(FriendDto friendDto) { } @Override - public List getFriendList(String userId) { + public List getFriendList() { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("FriendService::getFriendList: Authentication is needed."); + return new ArrayList<>(); + } List friendList = new ArrayList<>(); - try { - List friendPairList = findFriendPort.getFriendList(UUID.fromString(userId)); - for (FriendEntity friendPair: friendPairList) { - friendList.add(userMapper.toDto(findUserPort.getByUserId(friendPair.getUserId()).orElseThrow())); - } - } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + List friendPairList = findFriendPort.getFriendList(securityUser.getUserId()); + for (FriendEntity friendPair: friendPairList) { + friendList.add(userMapper.toDto(findUserPort.getByUserId(friendPair.getUserId()).orElseThrow())); } return friendList; } diff --git a/src/main/java/com/example/api/matching/application/port/in/FindMatchingUsecase.java b/src/main/java/com/example/api/matching/application/port/in/FindMatchingUsecase.java index 54ec5ce..0f5ca97 100644 --- a/src/main/java/com/example/api/matching/application/port/in/FindMatchingUsecase.java +++ b/src/main/java/com/example/api/matching/application/port/in/FindMatchingUsecase.java @@ -8,7 +8,7 @@ public interface FindMatchingUsecase { List getAll(); Optional getMatchingById(Long matchingId); - List getMatchingByWriterId(String userId); + List getMatchingByWriterId(); List getMatchingByIsActive(Boolean isActive); - List getRecommendedMatchingList(String userId); + List getRecommendedMatchingList(); } \ No newline at end of file diff --git a/src/main/java/com/example/api/matching/application/port/in/MatchingApplicationUsecase.java b/src/main/java/com/example/api/matching/application/port/in/MatchingApplicationUsecase.java index de62bef..291a4c7 100644 --- a/src/main/java/com/example/api/matching/application/port/in/MatchingApplicationUsecase.java +++ b/src/main/java/com/example/api/matching/application/port/in/MatchingApplicationUsecase.java @@ -10,7 +10,7 @@ public interface MatchingApplicationUsecase { ChatRoom createMatchingApplication(MatchingApplicationDto matchingApplicationDto); - List getByUserIdIsAndStateEquals(String userId, ApplicationStateEnum state); + List getByUserIdIsAndStateEquals(ApplicationStateEnum state); List getByMatchingIdIsAndStateEquals(Long matchingId, ApplicationStateEnum state); MatchingApplicationDto updateMatchingApplication(MatchingApplicationDto matchingApplicationDto); } \ No newline at end of file diff --git a/src/main/java/com/example/api/matching/service/MatchingApplicationService.java b/src/main/java/com/example/api/matching/service/MatchingApplicationService.java index e379ed5..8850ec2 100644 --- a/src/main/java/com/example/api/matching/service/MatchingApplicationService.java +++ b/src/main/java/com/example/api/matching/service/MatchingApplicationService.java @@ -1,10 +1,12 @@ package com.example.api.matching.service; +import com.example.api.auth.domain.SecurityUser; import com.example.api.chatroom.domain.ChatRoom; import com.example.api.chatroom.dto.CreateChatRoomDto; import com.example.api.chatroom.service.ChatRoomService; import com.example.api.chatroom.type.ChatRoomEnum; import com.example.api.common.type.ApplicationStateEnum; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.fcm.dto.FcmDto; import com.example.api.fcm.service.FcmService; import com.example.api.matching.adapter.out.persistence.MatchingApplicationEntity; @@ -75,18 +77,18 @@ public ChatRoom createMatchingApplication(MatchingApplicationDto matchingApplica } @Override - public List getByUserIdIsAndStateEquals(String userId, ApplicationStateEnum state) { - try { - List matchingPairList = matchingApplicationPort.getByUserIdIsAndStateEquals(UUID.fromString(userId), state); - List matchingData = new ArrayList<>(); - for (MatchingApplicationEntity matchingPair: matchingPairList) { - matchingData.add(matchingMapper.toDto(findMatchingPort.getByMatchingId(matchingPair.getMatchingId()).orElseThrow())); - } - return matchingData; - } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + public List getByUserIdIsAndStateEquals(ApplicationStateEnum state) { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("MatchingApplicationService::getByUserIdAndStateEquals: Authentication is needed."); return new ArrayList<>(); } + List matchingPairList = matchingApplicationPort.getByUserIdIsAndStateEquals(securityUser.getUserId(), state); + List matchingData = new ArrayList<>(); + for (MatchingApplicationEntity matchingPair: matchingPairList) { + matchingData.add(matchingMapper.toDto(findMatchingPort.getByMatchingId(matchingPair.getMatchingId()).orElseThrow())); + } + return matchingData; } @Override diff --git a/src/main/java/com/example/api/matching/service/MatchingService.java b/src/main/java/com/example/api/matching/service/MatchingService.java index a77c461..df68c48 100644 --- a/src/main/java/com/example/api/matching/service/MatchingService.java +++ b/src/main/java/com/example/api/matching/service/MatchingService.java @@ -1,6 +1,8 @@ package com.example.api.matching.service; +import com.example.api.auth.domain.SecurityUser; import com.example.api.common.type.Pair; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.matching.adapter.out.persistence.MatchingMapperInterface; import com.example.api.matching.application.port.in.DeleteMatchingUsecase; import com.example.api.matching.application.port.in.FindMatchingUsecase; @@ -15,7 +17,6 @@ import com.example.api.matching.dto.LikeDto; import com.example.api.matching.dto.SaveMatchingDto; import com.example.api.preference.service.PreferenceService; -import com.example.api.user.application.port.in.RecommendedMatchingUsecase; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -28,8 +29,7 @@ @Slf4j @RequiredArgsConstructor @Transactional(readOnly = true) -public class MatchingService implements - SaveMatchingUsecase, FindMatchingUsecase, DeleteMatchingUsecase, LikeUsecase, RecommendedMatchingUsecase { +public class MatchingService implements SaveMatchingUsecase, FindMatchingUsecase, DeleteMatchingUsecase, LikeUsecase { private final PreferenceService preferenceService; private final MatchingMapperInterface matchingMapper; private final SaveMatchingPort saveMatchingPort; @@ -58,15 +58,16 @@ public Optional getMatchingById(Long matchingId) { } @Override - public List getMatchingByWriterId(String userId) { - try { - return findMatchingPort.getByWriterId(UUID.fromString(userId)).stream() - .map(matchingMapper::toDto) - .collect(Collectors.toList()); - } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + public List getMatchingByWriterId() { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("MatchingService::getMatchingByWriterId: Authentication is needed."); return new ArrayList<>(); } + UUID userId = securityUser.getUserId(); + return findMatchingPort.getByWriterId(userId).stream() + .map(matchingMapper::toDto) + .collect(Collectors.toList()); } @Override @@ -77,12 +78,16 @@ public List getMatchingByIsActive(Boolean isActive) { } @Override - public List getRecommendedMatchingList(String userId) { + public List getRecommendedMatchingList() { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + return new ArrayList<>(); + } List activeMatchingList = this.getMatchingByIsActive(true); // 유효한 매칭만 뽑아옴 List> matchingScoreList = new ArrayList<>(); for (FindMatchingDto matchingData: activeMatchingList) { // 매칭별로 유사도 점수를 계산함 Long matchingId = matchingData.getMatchingId(); - Integer matchingScore = preferenceService.getMatchingScore(userId, matchingId); + Integer matchingScore = preferenceService.getMatchingScore(matchingId); matchingScoreList.add(new Pair<>(matchingId, matchingScore)); } matchingScoreList.sort(Comparator.comparing(Pair::getSecond)); diff --git a/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java b/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java index e234d4d..9485dee 100644 --- a/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java +++ b/src/main/java/com/example/api/member/adapter/in/rest/MemberController.java @@ -18,7 +18,7 @@ public class MemberController { /** * 방 생성 후, 유저들을 초대해 들어오는 경우 - * @param addMemberDto + * @param addMemberDto (데이터) */ @Operation(summary = "Add member", description = "채팅방에 사용자를 초대한다.") @PostMapping(value="/members") diff --git a/src/main/java/com/example/api/member/service/MemberService.java b/src/main/java/com/example/api/member/service/MemberService.java index 7add7ea..709a7e5 100644 --- a/src/main/java/com/example/api/member/service/MemberService.java +++ b/src/main/java/com/example/api/member/service/MemberService.java @@ -25,7 +25,7 @@ public class MemberService implements AddMemberChatRoomUsecase { /** * 채팅방에 멤버 추가 - * @param addMemberDto (Data) + * @param addMemberDto (데이터) */ @Override @Transactional diff --git a/src/main/java/com/example/api/notification/adapter/in/rest/NotificationController.java b/src/main/java/com/example/api/notification/adapter/in/rest/NotificationController.java index 8876c11..20d79c8 100644 --- a/src/main/java/com/example/api/notification/adapter/in/rest/NotificationController.java +++ b/src/main/java/com/example/api/notification/adapter/in/rest/NotificationController.java @@ -59,13 +59,12 @@ public Optional getNotificationById(@PathVariable Long noti /** * 사용자의 알림 리스트 조회 - * @param userId (ID) * @return List */ @Operation(summary = "Ger notification list of user", description = "사용자의 알림 리스트를 조회한다.") - @GetMapping("/user/{userId}/notification") - public List getUserNotificationList(@PathVariable Long userId) { - return findNotificationUsecase.getUserNotificationList(userId); + @GetMapping("/user/notification") + public List getUserNotificationList() { + return findNotificationUsecase.getUserNotificationList(); } /** diff --git a/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationEntity.java b/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationEntity.java index cd9585c..ff6a83b 100644 --- a/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationEntity.java +++ b/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationEntity.java @@ -1,8 +1,6 @@ package com.example.api.notification.adapter.out.persistence; import com.example.api.common.entity.BaseEntity; -import com.example.api.notification.dto.FindNotificationDto; -import com.example.api.notification.dto.SaveNotificationDto; import com.example.api.notification.type.NotificationTypeEnum; import jakarta.persistence.*; import lombok.*; @@ -29,13 +27,4 @@ public class NotificationEntity extends BaseEntity { @Column(nullable = false) private Boolean isActive; - - public FindNotificationDto toDto() { - return FindNotificationDto.builder() - .notificationId(notificationId) - .type(type) - .content(content) - .isActive(isActive) - .build(); - } } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationMapperInterface.java b/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationMapperInterface.java index 572590a..533931f 100644 --- a/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationMapperInterface.java +++ b/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationMapperInterface.java @@ -17,5 +17,6 @@ public interface NotificationMapperInterface { UserNotificationEntity toEntity(UserNotificationDto userNotificationDto); Notification toDomain(NotificationEntity notificationEntity); FindNotificationDto toDto(Notification notification); + FindNotificationDto toDto(NotificationEntity notificationEntity); UserNotificationDto toDto(UserNotificationEntity userNotificationEntity); } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationPersistenceAdapter.java b/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationPersistenceAdapter.java index 3c523b2..59f6fed 100644 --- a/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationPersistenceAdapter.java +++ b/src/main/java/com/example/api/notification/adapter/out/persistence/NotificationPersistenceAdapter.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Optional; +import java.util.UUID; @Repository @RequiredArgsConstructor @@ -34,16 +35,16 @@ public UserNotificationEntity createUserNotification(UserNotificationEntity user @Override public Optional getNotificationById(Long notificationId) { - return notificationRepository.getNotificationEntityByNotificationId(notificationId); + return notificationRepository.getByNotificationId(notificationId); } @Override - public List getUserNotificationList(Long userId) { - return userNotificationRepository.getUserNotificationEntitiesByUserId(userId); + public List getUserNotificationList(UUID userId) { + return userNotificationRepository.getByUserId(userId); } @Override public UserNotificationEntity getUserNotificationEntity(UserNotificationEntity userNotificationEntity) { - return userNotificationRepository.getUserNotificationEntityByUserIdAndNotificationId(userNotificationEntity.getUserId(), userNotificationEntity.getNotificationId()).orElseThrow(); + return userNotificationRepository.getByUserIdAndNotificationId(userNotificationEntity.getUserId(), userNotificationEntity.getNotificationId()).orElseThrow(); } } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationEntity.java b/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationEntity.java index 9df3f57..ddd7579 100644 --- a/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationEntity.java +++ b/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationEntity.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.LocalDateTime; +import java.util.UUID; @Entity @EntityListeners(AuditingEntityListener.class) @@ -20,7 +21,7 @@ public class UserNotificationEntity { private Long sequenceId; @Column(nullable = false) - private Long userId; + private UUID userId; @Column(nullable = false) private Long notificationId; diff --git a/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationPK.java b/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationPK.java index 7a0ed87..e2f04d4 100644 --- a/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationPK.java +++ b/src/main/java/com/example/api/notification/adapter/out/persistence/UserNotificationPK.java @@ -4,10 +4,11 @@ import lombok.Data; import java.io.Serializable; +import java.util.UUID; @Data @AllArgsConstructor public class UserNotificationPK implements Serializable { - private Long userId; + private UUID userId; private Long notificationId; } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/application/port/in/FindNotificationUsecase.java b/src/main/java/com/example/api/notification/application/port/in/FindNotificationUsecase.java index 317915b..e2cfca5 100644 --- a/src/main/java/com/example/api/notification/application/port/in/FindNotificationUsecase.java +++ b/src/main/java/com/example/api/notification/application/port/in/FindNotificationUsecase.java @@ -7,5 +7,5 @@ public interface FindNotificationUsecase { Optional getNotificationById(Long notificationId); - List getUserNotificationList(Long userId); + List getUserNotificationList(); } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/application/port/out/FindNotificationPort.java b/src/main/java/com/example/api/notification/application/port/out/FindNotificationPort.java index 6c12114..5620ab0 100644 --- a/src/main/java/com/example/api/notification/application/port/out/FindNotificationPort.java +++ b/src/main/java/com/example/api/notification/application/port/out/FindNotificationPort.java @@ -5,8 +5,9 @@ import java.util.List; import java.util.Optional; +import java.util.UUID; public interface FindNotificationPort { Optional getNotificationById(Long notificationId); - List getUserNotificationList(Long userId); + List getUserNotificationList(UUID userId); } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/repository/NotificationRepository.java b/src/main/java/com/example/api/notification/repository/NotificationRepository.java index f7d9ff2..a764125 100644 --- a/src/main/java/com/example/api/notification/repository/NotificationRepository.java +++ b/src/main/java/com/example/api/notification/repository/NotificationRepository.java @@ -6,5 +6,5 @@ import java.util.Optional; public interface NotificationRepository extends JpaRepository { - Optional getNotificationEntityByNotificationId(Long notificationId); + Optional getByNotificationId(Long notificationId); } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/repository/UserNotificationRepository.java b/src/main/java/com/example/api/notification/repository/UserNotificationRepository.java index 4a13079..93db792 100644 --- a/src/main/java/com/example/api/notification/repository/UserNotificationRepository.java +++ b/src/main/java/com/example/api/notification/repository/UserNotificationRepository.java @@ -6,8 +6,9 @@ import java.util.List; import java.util.Optional; +import java.util.UUID; public interface UserNotificationRepository extends JpaRepository { - Optional getUserNotificationEntityByUserIdAndNotificationId(Long userId, Long notificationId); - List getUserNotificationEntitiesByUserId(Long userId); + Optional getByUserIdAndNotificationId(UUID userId, Long notificationId); + List getByUserId(UUID userId); } \ No newline at end of file diff --git a/src/main/java/com/example/api/notification/service/NotificationService.java b/src/main/java/com/example/api/notification/service/NotificationService.java index 139d215..7e889af 100644 --- a/src/main/java/com/example/api/notification/service/NotificationService.java +++ b/src/main/java/com/example/api/notification/service/NotificationService.java @@ -1,5 +1,7 @@ package com.example.api.notification.service; +import com.example.api.auth.domain.SecurityUser; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.notification.adapter.out.persistence.NotificationEntity; import com.example.api.notification.adapter.out.persistence.NotificationMapperInterface; import com.example.api.notification.adapter.out.persistence.UserNotificationEntity; @@ -11,6 +13,7 @@ import com.example.api.notification.dto.FindNotificationDto; import com.example.api.notification.dto.SaveNotificationDto; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,6 +22,7 @@ import java.util.Optional; @Service +@Slf4j @RequiredArgsConstructor @Transactional(readOnly = true) public class NotificationService implements SaveNotificationUsecase, FindNotificationUsecase { @@ -36,16 +40,21 @@ public FindNotificationDto createNotification(SaveNotificationDto saveNotificati @Override public Optional getNotificationById(Long notificationId) { return findNotificationPort.getNotificationById(notificationId) - .map(NotificationEntity::toDto); + .map(notificationMapper::toDto); } @Override - public List getUserNotificationList(Long userId) { - List notificationPairList = findNotificationPort.getUserNotificationList(userId); + public List getUserNotificationList() { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("NotificationService::getUserNotificationList: Authentication is needed."); + return new ArrayList<>(); + } + List notificationPairList = findNotificationPort.getUserNotificationList(securityUser.getUserId()); List userNotificationList = new ArrayList<>(); for (UserNotificationEntity notificationPair: notificationPairList) { NotificationEntity notificationEntity = findNotificationPort.getNotificationById(notificationPair.getNotificationId()).orElseThrow(); - userNotificationList.add(notificationEntity.toDto()); + userNotificationList.add(notificationMapper.toDto(notificationEntity)); } return userNotificationList; } diff --git a/src/main/java/com/example/api/preference/adapter/in/rest/PreferenceController.java b/src/main/java/com/example/api/preference/adapter/in/rest/PreferenceController.java index 7b59787..7129c85 100644 --- a/src/main/java/com/example/api/preference/adapter/in/rest/PreferenceController.java +++ b/src/main/java/com/example/api/preference/adapter/in/rest/PreferenceController.java @@ -54,13 +54,12 @@ public MatchingPreferenceDto createMatchingPreference(@RequestBody MatchingPrefe /** * 사용자 선호도 조회 - * @param userId (ID) * @return ComparePreferenceDto */ @Operation(summary = "Get user preference", description = "사용자 선호도를 조회한다.") - @GetMapping("/user/{userId}/preference") - public ComparePreferenceDto findUserPreference(@PathVariable String userId) { - return comparePreferenceUsecase.getUserPreference(userId); + @GetMapping("/user/preference") + public ComparePreferenceDto findUserPreference() { + return comparePreferenceUsecase.getUserPreference(); } /** @@ -76,14 +75,13 @@ public ComparePreferenceDto findMatchingPreference(@PathVariable Long matchingId /** * 사용자 선호도 변경 - * @param userId (ID) * @param savePreferenceDto (데이터) * @return SavePreferenceDto */ @Operation(summary = "Update user preference", description = "사용자 선호도를 변경한다.") - @PatchMapping("/user/{userId}/preference") - public FindPreferenceDto updateUserPreference(@PathVariable String userId, @RequestBody SavePreferenceDto savePreferenceDto) { - return userPreferenceUsecase.updateUserPreference(userId, savePreferenceDto); + @PatchMapping("/user/preference") + public FindPreferenceDto updateUserPreference(@RequestBody SavePreferenceDto savePreferenceDto) { + return userPreferenceUsecase.updateUserPreference(savePreferenceDto); } /** diff --git a/src/main/java/com/example/api/preference/application/port/in/ComparePreferenceUsecase.java b/src/main/java/com/example/api/preference/application/port/in/ComparePreferenceUsecase.java index b1be9f7..f554dd4 100644 --- a/src/main/java/com/example/api/preference/application/port/in/ComparePreferenceUsecase.java +++ b/src/main/java/com/example/api/preference/application/port/in/ComparePreferenceUsecase.java @@ -3,7 +3,6 @@ import com.example.api.preference.dto.ComparePreferenceDto; public interface ComparePreferenceUsecase { - ComparePreferenceDto getUserPreference(String userId); + ComparePreferenceDto getUserPreference(); ComparePreferenceDto getMatchingPreference(Long matchingId); - Integer getMatchingScore(String userId, Long matchingId); } \ No newline at end of file diff --git a/src/main/java/com/example/api/preference/application/port/in/UserPreferenceUsecase.java b/src/main/java/com/example/api/preference/application/port/in/UserPreferenceUsecase.java index 752049f..d63f7a6 100644 --- a/src/main/java/com/example/api/preference/application/port/in/UserPreferenceUsecase.java +++ b/src/main/java/com/example/api/preference/application/port/in/UserPreferenceUsecase.java @@ -6,5 +6,5 @@ public interface UserPreferenceUsecase { UserPreferenceDto createUserPreference(UserPreferenceDto userPreferenceDto); - FindPreferenceDto updateUserPreference(String userId, SavePreferenceDto savePreferenceDto); + FindPreferenceDto updateUserPreference(SavePreferenceDto savePreferenceDto); } \ No newline at end of file diff --git a/src/main/java/com/example/api/preference/service/PreferenceService.java b/src/main/java/com/example/api/preference/service/PreferenceService.java index 8109489..fec916b 100644 --- a/src/main/java/com/example/api/preference/service/PreferenceService.java +++ b/src/main/java/com/example/api/preference/service/PreferenceService.java @@ -1,5 +1,7 @@ package com.example.api.preference.service; +import com.example.api.auth.domain.SecurityUser; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.preference.adapter.out.persistence.PreferenceEntity; import com.example.api.preference.adapter.out.persistence.PreferenceMapperInterface; import com.example.api.preference.application.port.in.ComparePreferenceUsecase; @@ -19,7 +21,6 @@ import java.time.LocalDateTime; import java.util.Optional; -import java.util.UUID; @Service @Slf4j @@ -56,15 +57,20 @@ public FindPreferenceDto updatePreference(Long preferenceId, SavePreferenceDto p // ComparePreference @Override - public ComparePreferenceDto getUserPreference(String userId) { + public ComparePreferenceDto getUserPreference() { try { - Long preferenceId = comparePreferencePort.getUserPreferenceId(UUID.fromString(userId)); + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("PreferenceService::getUserPreference: Authentication is needed."); + throw new Exception(); + } + Long preferenceId = comparePreferencePort.getUserPreferenceId(securityUser.getUserId()); if (preferenceId == 0L) { - throw new IllegalArgumentException(); + throw new Exception(); } - return this.getByPreferenceId(preferenceId).orElseThrow(); - } catch (IllegalArgumentException e) { - log.warn("Preference data was not found. Used default preference settings."); + return this.getByPreferenceId(preferenceId).orElseThrow(Exception::new); + } catch (Exception e) { + log.warn("PreferenceService::getUserPreference: Preference data was not found. Used default preference settings."); return ComparePreferenceDto.builder() .preferenceId(0L) .alcoholAmount(2) @@ -88,9 +94,8 @@ public ComparePreferenceDto getMatchingPreference(Long matchingId) { return this.getByPreferenceId(preferenceId).orElseThrow(); } - @Override - public Integer getMatchingScore(String userId, Long matchingId) { - ComparePreferenceDto userPreference = this.getUserPreference(userId); + public Integer getMatchingScore(Long matchingId) { + ComparePreferenceDto userPreference = this.getUserPreference(); ComparePreferenceDto matchingPreference = this.getMatchingPreference(matchingId); Integer score = 0; score += Math.abs(userPreference.getAlcoholAmount() - matchingPreference.getAlcoholAmount()); diff --git a/src/main/java/com/example/api/preference/service/UserPreferenceService.java b/src/main/java/com/example/api/preference/service/UserPreferenceService.java index 0eb2be7..5ac2607 100644 --- a/src/main/java/com/example/api/preference/service/UserPreferenceService.java +++ b/src/main/java/com/example/api/preference/service/UserPreferenceService.java @@ -1,5 +1,7 @@ package com.example.api.preference.service; +import com.example.api.auth.domain.SecurityUser; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.preference.adapter.out.persistence.PreferenceMapperInterface; import com.example.api.preference.adapter.out.persistence.UserPreferenceEntity; import com.example.api.preference.application.port.in.UserPreferenceUsecase; @@ -14,8 +16,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.UUID; - @Service @Slf4j @RequiredArgsConstructor @@ -33,13 +33,13 @@ public UserPreferenceDto createUserPreference(UserPreferenceDto userPreferenceDt } @Override - public FindPreferenceDto updateUserPreference(String userId, SavePreferenceDto savePreferenceDto) { - try { - Long preferenceId = comparePreferencePort.getUserPreferenceId(UUID.fromString(userId)); - return preferenceMapper.toDto(savePreferencePort.updatePreference(preferenceId, preferenceMapper.toDomain(savePreferenceDto))); - } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + public FindPreferenceDto updateUserPreference(SavePreferenceDto savePreferenceDto) { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("UserPreferenceService::updateUserPreference: Authentication is needed"); return preferenceMapper.toDto(savePreferenceDto); } + Long preferenceId = comparePreferencePort.getUserPreferenceId(securityUser.getUserId()); + return preferenceMapper.toDto(savePreferencePort.updatePreference(preferenceId, preferenceMapper.toDomain(savePreferenceDto))); } } \ No newline at end of file diff --git a/src/main/java/com/example/api/sms/adapter/in/rest/SmsController.java b/src/main/java/com/example/api/sms/adapter/in/rest/SmsController.java index d990c54..9d4a065 100644 --- a/src/main/java/com/example/api/sms/adapter/in/rest/SmsController.java +++ b/src/main/java/com/example/api/sms/adapter/in/rest/SmsController.java @@ -10,26 +10,23 @@ @RestController @RequiredArgsConstructor -@RequestMapping("/sms") @Validated public class SmsController { private final SendCertificationCodeUsecase sendCertificationCodeUsecase; private final VerifyCodeUsecase verifyCodeUsecase; /** - * 핸드폰 인증에 사용 - * - * @param phone + * 휴대폰 인증에 사용 + * @param phone (전화번호) */ - @Operation(summary = "certification phone", description = "핸드폰 인증") - @GetMapping("/code/{phone}") + @Operation(summary = "Certificate phone", description = "휴대폰 인증") + @GetMapping("/sms/code/{phone}") public void sendCertificationPhone(@PathVariable String phone){ sendCertificationCodeUsecase.send(phone); } - @PostMapping("/code") + @PostMapping("/sms/code") public void certificatePhone(@RequestBody @Validated CheckSMSDto checkSMSDto){ verifyCodeUsecase.verifyCertificationCode(checkSMSDto); } - -} +} \ No newline at end of file diff --git a/src/main/java/com/example/api/user/adapter/in/rest/UserController.java b/src/main/java/com/example/api/user/adapter/in/rest/UserController.java index c431ce4..ada29a5 100644 --- a/src/main/java/com/example/api/user/adapter/in/rest/UserController.java +++ b/src/main/java/com/example/api/user/adapter/in/rest/UserController.java @@ -32,7 +32,6 @@ public class UserController { private final FindUserUsecase findUserUsecase; private final DeleteUserUsecase deleteUserUsecase; private final FindMatchingUsecase findMatchingUsecase; - private final RecommendedMatchingUsecase recommendedMatchingUsecase; private final MatchingApplicationUsecase matchingApplicationUsecase; private final CreateGenderValidator createGenderValidator; // enum validator @@ -78,77 +77,71 @@ public Optional getUserById(@PathVariable String userId) { /** * ID가 userId인 사용자의 추천 매칭 목록 조회 - * @param userId (ID) * @return List */ @Operation(summary = "Get recommended matching list of a user", description = "사용자의 추천 매칭 목록을 조회한다.") - @GetMapping("/user/{userId}/recommendedmatching") - public List getRecommendedMatchingList(@PathVariable String userId) { - return recommendedMatchingUsecase.getRecommendedMatchingList(userId); + @GetMapping("/user/recommendedmatching") + public List getRecommendedMatchingList() { + return findMatchingUsecase.getRecommendedMatchingList(); } /** - * ID가 userId인 사용자가 작성한 매칭 목록 조회 - * @param userId (ID) + * 사용자가 작성한 매칭 목록 조회 * @return List */ @Operation(summary = "Get own matching list of user", description = "사용자가 작성한 매칭 목록을 조회한다.") - @GetMapping("/user/{userId}/matching/own") - public List getOwnMatchingList(@PathVariable String userId) { - return findMatchingUsecase.getMatchingByWriterId(userId); + @GetMapping("/user/matching/own") + public List getOwnMatchingList() { + return findMatchingUsecase.getMatchingByWriterId(); } /** - * ID가 userId인 사용자가 대기 중인 매칭 목록 조회 - * @param userId (ID) + * 사용자가 대기 중인 매칭 목록 조회 * @return List */ @Operation(summary = "Get pending matching list of user", description = "사용자가 대기 중인 매칭 목록을 조회한다.") - @GetMapping("/user/{userId}/pending") - public List getPendingMatchingList(@PathVariable String userId) { - return matchingApplicationUsecase.getByUserIdIsAndStateEquals(userId, ApplicationStateEnum.Pending); + @GetMapping("/user/pending") + public List getPendingMatchingList() { + return matchingApplicationUsecase.getByUserIdIsAndStateEquals(ApplicationStateEnum.Pending); } /** - * ID가 userId인 사용자가 참가한 매칭 목록 조회 - * @param userId (ID) + * 사용자가 참가한 매칭 목록 조회 * @return List */ @Operation(summary = "Get approved matching list of user", description = "사용자가 참가한 매칭 목록을 조회한다.") - @GetMapping("/user/{userId}/approved") - public List getApprovedMatchingList(@PathVariable String userId) { - return matchingApplicationUsecase.getByUserIdIsAndStateEquals(userId, ApplicationStateEnum.Approved); + @GetMapping("/user/approved") + public List getApprovedMatchingList() { + return matchingApplicationUsecase.getByUserIdIsAndStateEquals(ApplicationStateEnum.Approved); } /** - * ID가 userId인 사용자 정보 수정 - * @param userId (ID) + * 현재 로그인한 사용자 정보 수정 * @param userDto (데이터) * @return FindUserDto */ @Operation(summary = "Update user information", description = "사용자 정보를 변경한다.") - @PatchMapping("/user/{userId}") - public FindUserDto updateUser(@PathVariable String userId, @RequestBody UpdateUserDto userDto) { - return saveUserUsecase.updateUser(userId, userDto); + @PatchMapping("/user") + public FindUserDto updateUser(@RequestBody UpdateUserDto userDto) { + return saveUserUsecase.updateUser(userDto); } /** * 전체 사용자 삭제 (비상시 외 사용 금지) */ @Operation(summary = "Delete all users", description = "모든 사용자를 삭제한다.") - @DeleteMapping("/user") + @DeleteMapping("/user/all") public void deleteAll() { deleteUserUsecase.deleteAll(); } /** - * ID가 userId인 사용자 삭제 - * @param userId (ID) + * 사용자 삭제 (회원탈퇴 시 사용) */ @Operation(summary = "Delete user", description = "ID가 userId인 사용자를 삭제한다.") - @DeleteMapping("/user/{userId}") - public void deleteUser(@PathVariable String userId) { - deleteUserUsecase.deleteUser(userId); + @DeleteMapping("/user") + public void deleteUser() { + deleteUserUsecase.deleteUser(); } diff --git a/src/main/java/com/example/api/user/application/port/in/DeleteUserUsecase.java b/src/main/java/com/example/api/user/application/port/in/DeleteUserUsecase.java index 5da807d..69305a9 100644 --- a/src/main/java/com/example/api/user/application/port/in/DeleteUserUsecase.java +++ b/src/main/java/com/example/api/user/application/port/in/DeleteUserUsecase.java @@ -2,5 +2,5 @@ public interface DeleteUserUsecase { void deleteAll(); - void deleteUser(String userId); + void deleteUser(); } \ No newline at end of file diff --git a/src/main/java/com/example/api/user/application/port/in/RecommendedMatchingUsecase.java b/src/main/java/com/example/api/user/application/port/in/RecommendedMatchingUsecase.java deleted file mode 100644 index 932cf97..0000000 --- a/src/main/java/com/example/api/user/application/port/in/RecommendedMatchingUsecase.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.api.user.application.port.in; - -import com.example.api.matching.dto.FindMatchingDto; - -import java.util.List; - -public interface RecommendedMatchingUsecase { - List getRecommendedMatchingList(String userId); -} \ No newline at end of file diff --git a/src/main/java/com/example/api/user/application/port/in/SaveUserUsecase.java b/src/main/java/com/example/api/user/application/port/in/SaveUserUsecase.java index 4e02856..dae3647 100644 --- a/src/main/java/com/example/api/user/application/port/in/SaveUserUsecase.java +++ b/src/main/java/com/example/api/user/application/port/in/SaveUserUsecase.java @@ -6,5 +6,5 @@ public interface SaveUserUsecase { void createUser(CreateUserDto userDto); - FindUserDto updateUser(String userId, UpdateUserDto userDto); + FindUserDto updateUser(UpdateUserDto userDto); } \ No newline at end of file diff --git a/src/main/java/com/example/api/user/service/UserService.java b/src/main/java/com/example/api/user/service/UserService.java index e5f0364..64d1cf8 100644 --- a/src/main/java/com/example/api/user/service/UserService.java +++ b/src/main/java/com/example/api/user/service/UserService.java @@ -1,9 +1,9 @@ package com.example.api.user.service; import com.example.api.auth.domain.SecurityUser; -import com.example.api.auth.dto.SecurityUserDto; import com.example.api.common.exception.CustomException; import com.example.api.common.type.ErrorCodeEnum; +import com.example.api.common.utils.AuthenticationUtils; import com.example.api.common.utils.CustomBase64Utils; import com.example.api.sms.application.port.out.CheckVerifiedPhonePort; import com.example.api.social.adapter.out.persistence.SocialEntity; @@ -19,12 +19,15 @@ import com.example.api.user.application.port.in.SaveUserUsecase; import com.example.api.user.dto.CreateUserDto; import com.example.api.user.dto.UpdateUserDto; +import com.example.api.user.type.UserGenderEnum; +import com.example.api.user.type.UserRoleEnum; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import com.example.api.user.dto.FindUserDto; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -41,6 +44,23 @@ public class UserService implements SaveUserUsecase, FindUserUsecase, DeleteUser private final DeleteUserPort deleteUserPort; private final FindSocialPort findSocialPort; private final CheckVerifiedPhonePort checkVerifiedPhonePort; + + public FindUserDto getDefaultUser() { + return FindUserDto.builder() + .username("Anonymous") + .gender(UserGenderEnum.None) + .age(30) + .phone("010-0000-0000") + .role(UserRoleEnum.User) + .blacklist(false) + .stateMessage("") + .mannerScore(60) + .createdAt(LocalDateTime.now()) + .updatedAt(LocalDateTime.now()) + .isActive(false) + .build(); + } + @Override @Transactional public void createUser(CreateUserDto userDto) { @@ -63,21 +83,21 @@ public Optional getUserById(String userId) { return findUserPort.getByUserId(UUID.fromString(userId)) .map(userMapper::toDto); } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + log.warn("UserService::getUserById: UUID transform failed."); return Optional.empty(); } } @Override @Transactional - public FindUserDto updateUser(String userId, UpdateUserDto userDto) { - try { - User user = saveUserPort.updateUser(UUID.fromString(userId), userMapper.toDomain(userDto)); - return userMapper.toDto(user); - } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + public FindUserDto updateUser(UpdateUserDto userDto) { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); + if (securityUser == null) { + log.error("UserService::updateUser: Authentication is needed."); return userMapper.toDto(userDto); } + User user = saveUserPort.updateUser(securityUser.getUserId(), userMapper.toDomain(userDto)); + return userMapper.toDto(user); } @Override @@ -88,11 +108,16 @@ public void deleteAll() { @Override @Transactional - public void deleteUser(String userId) { + public void deleteUser() { + SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication(); try { - deleteUserPort.deleteByUserId(UUID.fromString(userId)); - } catch (IllegalArgumentException e) { - log.warn("Invalid userId: UUID transform failed."); + if (securityUser == null) { + log.error("UserService::deleteUser: Authentication is needed."); + throw new Exception(); + } + deleteUserPort.deleteByUserId(securityUser.getUserId()); + } catch (Exception e) { + e.printStackTrace(); } } @@ -110,10 +135,9 @@ public SecurityUser findSocialUser(String id, String provider) { .instaId(user.getSocialId().getInstaId()) .role(user.getRole().getRole()) .build(); - } public Optional findUserSigned(Long id) { return findUserPort.findUserSigned(id); } -} +} \ No newline at end of file diff --git a/src/test/java/com/example/api/user/UserControllerTests.java b/src/test/java/com/example/api/user/UserControllerTests.java index 94038a0..d4f772f 100644 --- a/src/test/java/com/example/api/user/UserControllerTests.java +++ b/src/test/java/com/example/api/user/UserControllerTests.java @@ -4,7 +4,6 @@ import com.example.api.user.adapter.in.rest.UserController; import com.example.api.user.application.port.in.DeleteUserUsecase; import com.example.api.user.application.port.in.FindUserUsecase; -import com.example.api.user.application.port.in.RecommendedMatchingUsecase; import com.example.api.user.application.port.in.SaveUserUsecase; import com.example.api.user.type.UserGenderEnum; import com.example.api.user.type.UserRoleEnum; @@ -36,8 +35,6 @@ public class UserControllerTests { @MockBean private DeleteUserUsecase deleteUserUsecase; @MockBean - private RecommendedMatchingUsecase recommendedMatchingUsecase; - @MockBean private MatchingApplicationUsecase matchingApplicationUsecase; static Map user1 = new HashMap<>();