Skip to content

Commit

Permalink
[fix] #124 랜덤 키워드 선택 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo0419 committed Aug 26, 2024
1 parent b6324e2 commit d33bb4c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question, Long> {
Optional<Question> findFirstByProfileOrderByCreatedAtDesc(Profile profile);

boolean existsByProfile(Profile profile);

List<Question> 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<String> findKeywordsByProfileOrderByCreatedAtDesc(Long profileId, int count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RequiredArgsConstructor
public class QuestionRetriever {
Expand All @@ -32,15 +29,4 @@ public Question findById(final Long questionId){
public boolean existsByProfile(final Profile profile){
return questionRepository.existsByProfile(profile);
}

public List<Question> findAllByProfile(final Profile profile) {
return questionRepository.findAllByProfile(profile);
}

public List<String> findKeywordsByProfile(
final Long profileId,
final int count
){
return questionRepository.findKeywordsByProfileOrderByCreatedAtDesc(profileId, count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -116,15 +117,13 @@ private String getRandomKeyword(final Profile profile){
return "기분";
}
List<String> keywords = Arrays.stream(profile.getKeywords().split(",")).toList();
List<String> 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);
}
Expand Down

0 comments on commit d33bb4c

Please sign in to comment.