Skip to content

Commit

Permalink
Merge pull request #47 from KUSITMS-29th-TEAM-D/feature/#43/discover-…
Browse files Browse the repository at this point in the history
…persona

[feat] : 채팅 내역 조회 API 구현 (GET)
  • Loading branch information
bbbang105 authored May 19, 2024
2 parents 531d0a3 + 9727d09 commit b923ecf
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public ResponseEntity<ApiResponse<DiscoverPersonaDto.AnswerResponse>> getReactio

return ApiResponse.onSuccess(PersonaSuccessStatus.CREATED_REACTION_AND_SUMMARY, answerResponse);
}

// 돌아보기 페르소나 카테고리별 채팅 내역을 조회하는 API
@GetMapping("/discover/chattings")
public ResponseEntity<ApiResponse<DiscoverPersonaDto.ChattingResponse>> getChattings(
@RequestHeader("Authorization") String authorizationHeader,
@RequestParam("category") String category) {

DiscoverPersonaDto.ChattingResponse chattingResponse = discoverPersonaService.getChattings(authorizationHeader, category);

return ApiResponse.onSuccess(PersonaSuccessStatus.GET_CHATTINGS, chattingResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public interface DiscoverPersonaService {
DiscoverPersonaDto.QuestionResponse getNewQuestion(String authorizationHeader, String category);
DiscoverPersonaDto.AnswerResponse getReactionAndSummary(String authorizationHeader, DiscoverPersonaDto.AnswerRequest answerRequest);
DiscoverPersonaDto.ChattingResponse getChattings(String authorizationHeader, String category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -92,6 +93,21 @@ public DiscoverPersonaDto.AnswerResponse getReactionAndSummary(String authorizat
return DiscoverPersonaDto.AnswerResponse.of(reaction, summary);
}

// 카테고리별 채팅 내역을 반환하는 메서드
@Override
public DiscoverPersonaDto.ChattingResponse getChattings(String authorizationHeader, String category) {
String token = jwtUtil.getTokenFromHeader(authorizationHeader);
UUID userId = UUID.fromString(jwtUtil.getUserIdFromToken(token));
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new UserException(UserErrorResult.NOT_FOUND_USER));

DiscoverPersona discoverPersona = discoverPersonaRepository.findByUserAndCategory(user, category);
List<DiscoverPersonaChatting> chattings = discoverPersonaChattingRepository.findAllByDiscoverPersonaOrderByCreatedDateAsc(discoverPersona);
List<String> stageQuestions = createStageQuestions(category, chattings);

return DiscoverPersonaDto.ChattingResponse.of(stageQuestions, chattings);
}

// 질문 번호를 생성하는 메서드
private int createNewQuestionNumber(List<Integer> questionNumbers) {
int randomQuestionNumber = numberUtil.getRandomNumberNotInList(questionNumbers);
Expand All @@ -102,7 +118,6 @@ private int createNewQuestionNumber(List<Integer> questionNumbers) {

// Enum에서 질문 내용을 가져오는 메서드
private String getQuestionContent(String category, int number) {
// Question Enum에서 category와 number에 해당하는 content 찾기
for (Question question : Question.values()) {
if (question.getCategory().equals(category) && question.getNumber() == number) {
return question.getContent();
Expand All @@ -111,4 +126,14 @@ private String getQuestionContent(String category, int number) {

throw new PersonaException(PersonaErrorResult.NOT_FOUND_QUESTION);
}

// 질문 목록을 생성하는 메서드
private List<String> createStageQuestions(String category, List<DiscoverPersonaChatting> chattings) {
List<String> stageQuestions = new ArrayList<>();
for (DiscoverPersonaChatting discoverPersonaChatting : chattings) {
stageQuestions.add(getQuestionContent(category, discoverPersonaChatting.getQuestionNumber()));
}

return stageQuestions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public enum PersonaSuccessStatus implements BaseCode {
// 돌아보기 페르소나
CREATED_NEW_QUESTION(HttpStatus.CREATED, "201", "돌아보기 페르소나 질문 생성에 성공했습니다."),
CREATED_REACTION_AND_SUMMARY(HttpStatus.CREATED, "201", "돌아보기 페르소나 공감 & 요약 생성에 성공했습니다."),
GET_CHATTINGS(HttpStatus.OK, "200", "채팅 내역 조회에 성공했습니다."),
// 정의하기 페르소나
CREATED_DEFINE_PERSONA(HttpStatus.CREATED, "201", "정의하기 페르소나 생성에 성공했습니다."),
GET_DEFINE_PERSONA(HttpStatus.OK, "200", "정의하기 페르소나 조회에 성공했습니다."),
// 설계하기 페르소나
CREATED_DESIGN_PERSONA(HttpStatus.CREATED, "201", "설계하기 페르소나 생성에 성공했습니다."),
GET_DESIGN_PERSONA(HttpStatus.CREATED, "200", "설계하기 페르소나 조회에 성공했습니다.");
GET_DESIGN_PERSONA(HttpStatus.OK, "200", "설계하기 페르소나 조회에 성공했습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
public interface DiscoverPersonaChattingRepository extends JpaRepository<DiscoverPersonaChatting,Long> {
@Query("SELECT dc.questionNumber FROM DiscoverPersonaChatting dc WHERE dc.discoverPersona = :discoverPersona")
List<Integer> findQuestionNumbersByDiscoverPersona(DiscoverPersona discoverPersona);
List<DiscoverPersonaChatting> findAllByDiscoverPersonaOrderByCreatedDateAsc(DiscoverPersona discoverPersona);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface DiscoverPersonaRepository extends JpaRepository<DiscoverPersona
@Query("SELECT dp FROM DiscoverPersona dp WHERE dp.user = :user AND dp.category = :category ORDER BY dp.createdDate DESC")
DiscoverPersona findTopByUserAndCategoryOrderByCreateDateDesc(@Param("user") User user, @Param("category") String category);
boolean existsByUserAndCategory(User user, String category);
DiscoverPersona findByUserAndCategory(User user, String category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import kusitms.jangkku.domain.persona.domain.DiscoverPersonaChatting;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

public class DiscoverPersonaDto {
@Builder
@Getter
Expand Down Expand Up @@ -58,4 +61,44 @@ public static DiscoverPersonaDto.AnswerResponse of(String reaction, String summa
.build();
}
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class ChattingResponse {
private Stage stageOne;
private Stage stageTwo;
private Stage stageThree;

public static ChattingResponse of(List<String> questions, List<DiscoverPersonaChatting> chattings) {
return ChattingResponse.builder()
.stageOne(!questions.isEmpty() && !chattings.isEmpty() ? Stage.of(questions.get(0), chattings.get(0).getAnswer(), chattings.get(0).getReaction()) : null)
.stageTwo(questions.size() > 1 && chattings.size() > 1 ? Stage.of(questions.get(1), chattings.get(1).getAnswer(), chattings.get(1).getReaction()) : null)
.stageThree(questions.size() > 2 && chattings.size() > 2 ? Stage.of(questions.get(2), chattings.get(2).getAnswer(), chattings.get(2).getReaction()) : null)
.build();
}
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Stage {
private String question;
private String answer;
private String reaction;

public static DiscoverPersonaDto.Stage of(String question, String answer, String reaction) {
return DiscoverPersonaDto.Stage.builder()
.question(question)
.answer(answer)
.reaction(reaction)
.build();
}
}
}

0 comments on commit b923ecf

Please sign in to comment.