diff --git a/src/main/java/com/cvsgo/controller/UserController.java b/src/main/java/com/cvsgo/controller/UserController.java index 71a98bc4..d1894fbd 100644 --- a/src/main/java/com/cvsgo/controller/UserController.java +++ b/src/main/java/com/cvsgo/controller/UserController.java @@ -77,6 +77,12 @@ public SuccessResponse deleteUserFollow(@LoginUser User user, @PathVariabl return SuccessResponse.create(); } + @GetMapping("/users/{userId}/tag-match-percentage") + public SuccessResponse readUserTagMatchPercentage( + @LoginUser User user, @PathVariable Long userId) { + return SuccessResponse.from(userService.readUserTagMatchPercentage(user, userId)); + } + @GetMapping("/users/{userId}/liked-products") public SuccessResponse> readLikedProductList( @PathVariable Long userId, @ModelAttribute ReadUserProductRequestDto request, diff --git a/src/main/java/com/cvsgo/repository/UserTagRepository.java b/src/main/java/com/cvsgo/repository/UserTagRepository.java index cbd3318f..0ae2da8c 100644 --- a/src/main/java/com/cvsgo/repository/UserTagRepository.java +++ b/src/main/java/com/cvsgo/repository/UserTagRepository.java @@ -1,5 +1,6 @@ package com.cvsgo.repository; +import com.cvsgo.entity.User; import com.cvsgo.entity.UserTag; import java.util.List; import org.springframework.data.jpa.repository.EntityGraph; @@ -10,4 +11,6 @@ public interface UserTagRepository extends JpaRepository { @EntityGraph(attributePaths = {"user", "tag"}) List findByUserIdIn(List userIds); + List findAllByUser(User user); + } diff --git a/src/main/java/com/cvsgo/service/UserService.java b/src/main/java/com/cvsgo/service/UserService.java index d51e6d8b..068b6da5 100644 --- a/src/main/java/com/cvsgo/service/UserService.java +++ b/src/main/java/com/cvsgo/service/UserService.java @@ -15,6 +15,7 @@ import com.cvsgo.entity.Tag; import com.cvsgo.entity.User; import com.cvsgo.entity.UserFollow; +import com.cvsgo.entity.UserTag; import com.cvsgo.exception.BadRequestException; import com.cvsgo.exception.DuplicateException; import com.cvsgo.exception.NotFoundException; @@ -22,6 +23,7 @@ import com.cvsgo.repository.TagRepository; import com.cvsgo.repository.UserFollowRepository; import com.cvsgo.repository.UserRepository; +import com.cvsgo.repository.UserTagRepository; import jakarta.persistence.EntityManager; import java.util.List; import lombok.RequiredArgsConstructor; @@ -36,6 +38,8 @@ @RequiredArgsConstructor public class UserService { + private final UserTagRepository userTagRepository; + private final UserRepository userRepository; private final TagRepository tagRepository; @@ -177,4 +181,31 @@ public void deleteUserFollow(User user, Long userId) { userFollowRepository.delete(userFollow); } + /** + * 태그 매칭률을 조회한다. + * + * @param user 로그인한 사용자 + * @param userId 태그 매칭률을 조회할 사용자 ID + * @return 태그 매칭률 + * @throws NotFoundException 해당하는 아이디를 가진 사용자가 없는 경우 + */ + @Transactional(readOnly = true) + public Integer readUserTagMatchPercentage(User user, Long userId) { + User targetUser = userRepository.findById(userId).orElseThrow(() -> NOT_FOUND_USER); + + List loginUserTag = userTagRepository.findAllByUser(user).stream().map(UserTag::getTag) + .toList(); + List targetUserTag = userTagRepository.findAllByUser(targetUser).stream().map( + UserTag::getTag).toList(); + + int matchingCount = 0; + for (Tag tag : loginUserTag) { + if (targetUserTag.contains(tag)) { + matchingCount++; + } + } + + return (int) (((double) matchingCount / loginUserTag.size()) * 100); + } + }