Skip to content

Commit

Permalink
feat: 태그 매칭률 조회 API 추가 #103
Browse files Browse the repository at this point in the history
  • Loading branch information
chaewss committed Sep 14, 2023
1 parent e377016 commit 20af823
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/cvsgo/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public SuccessResponse<Void> deleteUserFollow(@LoginUser User user, @PathVariabl
return SuccessResponse.create();
}

@GetMapping("/users/{userId}/tag-match-percentage")
public SuccessResponse<Integer> readUserTagMatchPercentage(
@LoginUser User user, @PathVariable Long userId) {
return SuccessResponse.from(userService.readUserTagMatchPercentage(user, userId));
}

@GetMapping("/users/{userId}/liked-products")
public SuccessResponse<Page<ReadProductResponseDto>> readLikedProductList(
@PathVariable Long userId, @ModelAttribute ReadUserProductRequestDto request,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/cvsgo/repository/UserTagRepository.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,4 +11,6 @@ public interface UserTagRepository extends JpaRepository<UserTag, Long> {
@EntityGraph(attributePaths = {"user", "tag"})
List<UserTag> findByUserIdIn(List<Long> userIds);

List<UserTag> findAllByUser(User user);

}
31 changes: 31 additions & 0 deletions src/main/java/com/cvsgo/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
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;
import com.cvsgo.repository.ReviewRepository;
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;
Expand All @@ -36,6 +38,8 @@
@RequiredArgsConstructor
public class UserService {

private final UserTagRepository userTagRepository;

private final UserRepository userRepository;

private final TagRepository tagRepository;
Expand Down Expand Up @@ -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<Tag> loginUserTag = userTagRepository.findAllByUser(user).stream().map(UserTag::getTag)
.toList();
List<Tag> 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);
}

}

0 comments on commit 20af823

Please sign in to comment.