From d33bb4c6cebaa4bfc22363b2c0d085f24707ccbd Mon Sep 17 00:00:00 2001 From: minwoo0419 Date: Mon, 26 Aug 2024 20:24:32 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20#124=20=EB=9E=9C=EB=8D=A4=20=ED=82=A4?= =?UTF-8?q?=EC=9B=8C=EB=93=9C=20=EC=84=A0=ED=83=9D=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/repository/QuestionRepository.java | 7 ------- .../service/question/QuestionRetriever.java | 14 -------------- .../service/question/QuestionService.java | 17 ++++++++--------- 3 files changed, 8 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/telepigeon/server/repository/QuestionRepository.java b/src/main/java/com/telepigeon/server/repository/QuestionRepository.java index 25f47bf..8aa99c6 100644 --- a/src/main/java/com/telepigeon/server/repository/QuestionRepository.java +++ b/src/main/java/com/telepigeon/server/repository/QuestionRepository.java @@ -3,18 +3,11 @@ import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Question; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import java.util.List; import java.util.Optional; public interface QuestionRepository extends JpaRepository { Optional findFirstByProfileOrderByCreatedAtDesc(Profile profile); boolean existsByProfile(Profile profile); - - List findAllByProfile(Profile profile); - - @Query(value="select q.keyword from question q where q.profile_id = :profileId group by q.keyword order by max(q.created_at) desc limit :count", nativeQuery=true) - List findKeywordsByProfileOrderByCreatedAtDesc(Long profileId, int count); } diff --git a/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java b/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java index 95db695..b486808 100644 --- a/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java +++ b/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java @@ -8,9 +8,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import java.util.List; - - @Component @RequiredArgsConstructor public class QuestionRetriever { @@ -32,15 +29,4 @@ public Question findById(final Long questionId){ public boolean existsByProfile(final Profile profile){ return questionRepository.existsByProfile(profile); } - - public List findAllByProfile(final Profile profile) { - return questionRepository.findAllByProfile(profile); - } - - public List findKeywordsByProfile( - final Long profileId, - final int count - ){ - return questionRepository.findKeywordsByProfileOrderByCreatedAtDesc(profileId, count); - } } diff --git a/src/main/java/com/telepigeon/server/service/question/QuestionService.java b/src/main/java/com/telepigeon/server/service/question/QuestionService.java index 0a6785e..5037aec 100644 --- a/src/main/java/com/telepigeon/server/service/question/QuestionService.java +++ b/src/main/java/com/telepigeon/server/service/question/QuestionService.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.Random; import static java.time.temporal.ChronoUnit.DAYS; @@ -116,15 +117,13 @@ private String getRandomKeyword(final Profile profile){ return "기분"; } List keywords = Arrays.stream(profile.getKeywords().split(",")).toList(); - List alreadyKeywords = questionRetriever.findKeywordsByProfile( - profile.getId(), - keywords.size() - ); - int i = 0; - for ( ; i < keywords.size(); i++) { - if (!alreadyKeywords.contains(keywords.get(i)) || i == keywords.size() - 1) { - break; - } + if (keywords.size() == 1) + return keywords.get(0); + String alreadyKeyword = questionRetriever.findFirstByProfile(profile).getKeyword(); + Random random = new Random(); + int i = random.nextInt(keywords.size()); + while(alreadyKeyword.equals(keywords.get(i))){ + i = random.nextInt(keywords.size()); } return keywords.get(i); }