Skip to content

Commit

Permalink
Merge pull request #125 from TelePigeon/fix/124
Browse files Browse the repository at this point in the history
[fix] 질문 생성 시 랜덤 키워드 선택 수정
  • Loading branch information
minwoo0419 committed Aug 26, 2024
2 parents b6324e2 + 1faf9bf commit a062aec
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import com.telepigeon.server.service.openAi.OpenAiService;
import com.telepigeon.server.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

@Slf4j
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
Expand Down Expand Up @@ -67,9 +69,13 @@ public ResponseEntity<String> testOpenAi(
@RequestParam(required = false) String ageRange,
@RequestParam boolean easyMode
) {
return ResponseEntity.ok(openAiService.createQuestion(relation, keyword, gender, ageRange, easyMode));
String question = openAiService.createQuestion(relation, keyword, gender, ageRange, easyMode);
log.info("\n질문 생성 완료.\n입력 정보 : 관계 - {} / 키워드 - {} / 성별 - {} / 연령대 - {} / 쉬운 사용 모드 - {}\n내용 : {}", relation, keyword, gender, ageRange, easyMode, question);
return ResponseEntity.ok(question);
}



@GetMapping("/test/emotion")
public ResponseEntity<ConfidenceDto> testEmotion(
@RequestBody ConfidenceCreateDto text
Expand Down
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 @@ -111,7 +111,7 @@ public Answer create(
roomId
)
);
log.info("답장 생성 완료.\n내용 : {}\n감정 점수 : {}\n이전 평균 감정 점수 : {}\n현재 평균 감정 점수 : {}", answer.getContent(), answer.getEmotion(), preEmotion, profile.getEmotion());
log.info("\n답장 생성 완료.\n내용 : {}\n감정 점수 : {}\n이전 평균 감정 점수 : {}\n현재 평균 감정 점수 : {}", answer.getContent(), answer.getEmotion(), preEmotion, profile.getEmotion());
return answer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void send(
Message message = createMessage(fcmToken, fcmMessageDto);
try{
FirebaseMessaging.getInstance().send(message);
log.info("{} 사용자 -> {} 사용자 푸시 알림 성공.\n알림 유형 : {}\n내용 : {}", fcmMessageDto.senderName(), fcmMessageDto.receiverName(), fcmMessageDto.type(), fcmMessageDto.body());
log.info("\n{} 사용자 -> {} 사용자 푸시 알림 성공.\n알림 유형 : {}\n내용 : {}", fcmMessageDto.senderName(), fcmMessageDto.receiverName(), fcmMessageDto.type(), fcmMessageDto.body());
} catch (FirebaseMessagingException e){
log.error("Failed to send message. fcm Token : {}", fcmToken);
}
Expand Down
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 a062aec

Please sign in to comment.