Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAi API의 답변을 출력할 때 SSE 적용 #61

Merged
merged 9 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.springfox:springfox-boot-starter:3.0.0'


}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

@Service
public interface QuestionClassifyByCategoryService {
Map<QuestionCategory, List<Question>> classifyQuestionByCategoryOrdered(List<? extends Question> questions);
Map<QuestionCategory, List<Question>> classifyQuestionByCategoryOrdered(List<? extends Question> questions);
}

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private Optional<File> convert(MultipartFile file) throws IOException {
}
return Optional.of(convertFile);
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

@Service
public interface LicenseQuestionGetService {
Map<QuestionCategory, List<Question>> getClassifiedLicenseMultipleChoiceQuestion(
Long sessionId);
Map<QuestionCategory, List<Question>> getClassifiedLicenseMultipleChoiceQuestion(
Long sessionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
@RequiredArgsConstructor
public class AdminLicenseQuestionGetService implements LicenseQuestionGetService {

private final LicenseMultipleChoiceQuestionDslRepository licenseMultipleChoiceQuestionDslRepository;
private final QuestionClassifyByCategoryService questionClassifyByCategoryService;
private final LicenseMultipleChoiceQuestionDslRepository licenseMultipleChoiceQuestionDslRepository;
private final QuestionClassifyByCategoryService questionClassifyByCategoryService;

/**
* 관리자 조회시 허용 여부와 관련 없이 모든 문제를 가져온다.
* 문제지도 섞지 않는다.
* 허용 여부 순대로 섞는다
*/
@Override
public Map<QuestionCategory, List<Question>> getClassifiedLicenseMultipleChoiceQuestion(
Long sessionId) {
List<LicenseMultipleChoiceQuestion> licenseMultipleChoiceQuestions = licenseMultipleChoiceQuestionDslRepository
.findAllWhereLicenseSessionId(sessionId);
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(licenseMultipleChoiceQuestions);
}
/**
* 관리자 조회시 허용 여부와 관련 없이 모든 문제를 가져온다.
* 문제지도 섞지 않는다.
* 허용 여부 순대로 섞는다
*/
@Override
public Map<QuestionCategory, List<Question>> getClassifiedLicenseMultipleChoiceQuestion(
Long sessionId) {
List<LicenseMultipleChoiceQuestion> licenseMultipleChoiceQuestions = licenseMultipleChoiceQuestionDslRepository
.findAllWhereLicenseSessionId(sessionId);
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(licenseMultipleChoiceQuestions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;

public interface AdminMajorQuestionClassifiedGetService {
Map<QuestionCategory, List<Question>> getClassifiedAllMajorMultipleChoiceQuestions();
Map<QuestionCategory, List<Question>> getClassifiedAllMajorMultipleChoiceQuestions();

Map<QuestionCategory, List<Question>> getClassifiedAllMajorDescriptiveQuestions();
Map<QuestionCategory, List<Question>> getClassifiedAllMajorDescriptiveQuestions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

@Service
public interface MajorQuestionClassifiedGetService<T extends Question> {
/**
* 분류별로 나누어 문제를 반환
*/
Map<QuestionCategory, List<T>> getApprovedClassifiedMajorMultipleChoiceQuestions(
RequestGetQuestionByCategoryAndLevelDto requestGetQuestionByCategoryAndLevelDto);
/**
* 분류별로 나누어 문제를 반환
*/
Map<QuestionCategory, List<T>> getApprovedClassifiedMajorMultipleChoiceQuestions(
RequestGetQuestionByCategoryAndLevelDto requestGetQuestionByCategoryAndLevelDto);


Map<QuestionCategory, List<T>> getApprovedClassifiedDescriptiveQuestions(
RequestGetQuestionByCategoryAndLevelDto requestGetQuestionByCategoryAndLevelDto
);
Map<QuestionCategory, List<T>> getApprovedClassifiedDescriptiveQuestions(
RequestGetQuestionByCategoryAndLevelDto requestGetQuestionByCategoryAndLevelDto
);

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@
@RequiredArgsConstructor
@Transactional
public class BasicAdminMajorQuestionClassifiedGetService implements AdminMajorQuestionClassifiedGetService {
private final QuestionClassifyByCategoryService questionClassifyByCategoryService;
private final MajorDescriptiveDslRepository majorDescriptiveQuestionChooseRepository;
private final MajorMultipleChoiceQuestionDslRepository majorMultipleChoiceQuestionDslRepository;
private final QuestionClassifyByCategoryService questionClassifyByCategoryService;
private final MajorDescriptiveDslRepository majorDescriptiveQuestionChooseRepository;
private final MajorMultipleChoiceQuestionDslRepository majorMultipleChoiceQuestionDslRepository;

/**
* 관리자가 조회시 Approve됐는지 기준으로 정렬되며(false인 것부터),
* 이후엔 객관식 -> 주관식으로 정렬된다.
*
* @return
*/
@Override
public Map<QuestionCategory, List<Question>> getClassifiedAllMajorMultipleChoiceQuestions() {
List<MajorMultipleChoiceQuestion> majorMultipleChoiceQuestions = majorMultipleChoiceQuestionDslRepository
.findAllOrderByIfApprovedAsc();
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorMultipleChoiceQuestions);
}
/**
* 관리자가 조회시 Approve됐는지 기준으로 정렬되며(false인 것부터),
* 이후엔 객관식 -> 주관식으로 정렬된다.
*
* @return
*/
@Override
public Map<QuestionCategory, List<Question>> getClassifiedAllMajorMultipleChoiceQuestions() {
List<MajorMultipleChoiceQuestion> majorMultipleChoiceQuestions = majorMultipleChoiceQuestionDslRepository
.findAllOrderByIfApprovedAsc();
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorMultipleChoiceQuestions);
}

@Override
public Map<QuestionCategory, List<Question>> getClassifiedAllMajorDescriptiveQuestions() {
List<MajorDescriptiveQuestion> majorDescriptiveQuestions =
majorDescriptiveQuestionChooseRepository.findAllOrderByIfApprovedAsc();
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorDescriptiveQuestions);
}
@Override
public Map<QuestionCategory, List<Question>> getClassifiedAllMajorDescriptiveQuestions() {
List<MajorDescriptiveQuestion> majorDescriptiveQuestions =
majorDescriptiveQuestionChooseRepository.findAllOrderByIfApprovedAsc();
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorDescriptiveQuestions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,74 +22,74 @@
@Transactional
public class BasicAdminMajorQuestionMakeService implements AdminMajorQuestionMakeService {

private final QuestionRepository questionRepository;
private final MajorMultipleChoiceQuestionRepository majorMultipleChoiceQuestionRepository;
private final QuestionChoiceService questionChoiceService;
private final DuplicateQuestionDetector duplicateQuestionDetector;
private final QuestionRepository questionRepository;
private final MajorMultipleChoiceQuestionRepository majorMultipleChoiceQuestionRepository;
private final QuestionChoiceService questionChoiceService;
private final DuplicateQuestionDetector duplicateQuestionDetector;

/**
* 리스트로 생성
*/
@Override
public List<MajorMultipleChoiceQuestion> makeMultipleChoiceQuestions(
List<RequestMakeMultipleChoiceQuestionDto> requestDtos) {
// 중복되지 않은 질문을 필터링하여 저장
return requestDtos.stream()
.filter(this::isNotDuplicateQuestion)
.map(this::saveMajorMultipleChoiceQuestion)
.collect(Collectors.toList());
}
/**
* 리스트로 생성
*/
@Override
public List<MajorMultipleChoiceQuestion> makeMultipleChoiceQuestions(
List<RequestMakeMultipleChoiceQuestionDto> requestDtos) {
// 중복되지 않은 질문을 필터링하여 저장
return requestDtos.stream()
.filter(this::isNotDuplicateQuestion)
.map(this::saveMajorMultipleChoiceQuestion)
.collect(Collectors.toList());
}

/**
* 단일 생성
*/
@Override
public MajorMultipleChoiceQuestion makeMultipleChoiceQuestion(
RequestMakeMultipleChoiceQuestionDto requestDto) throws DuplicateQuestionContentException {
if (!isNotDuplicateQuestion(requestDto)) {
throw new DuplicateQuestionContentException();
}
return saveMajorMultipleChoiceQuestion(requestDto);
}
/**
* 단일 생성
*/
@Override
public MajorMultipleChoiceQuestion makeMultipleChoiceQuestion(
RequestMakeMultipleChoiceQuestionDto requestDto) throws DuplicateQuestionContentException {
if (!isNotDuplicateQuestion(requestDto)) {
throw new DuplicateQuestionContentException();
}
return saveMajorMultipleChoiceQuestion(requestDto);
}

@Override
public List<MajorDescriptiveQuestion> makeDescriptiveQuestions(
List<RequestMakeMajorDescriptiveQuestionDto> requestNormalQuestionDto) {
@Override
public List<MajorDescriptiveQuestion> makeDescriptiveQuestions(
List<RequestMakeMajorDescriptiveQuestionDto> requestNormalQuestionDto) {

return requestNormalQuestionDto.stream()
.map(this::saveMajorDescriptiveQuestion)
.collect(Collectors.toList());
}
return requestNormalQuestionDto.stream()
.map(this::saveMajorDescriptiveQuestion)
.collect(Collectors.toList());
}

/**
* 중복되지 않은 질문인지 확인하는 메서드
* 매번 DB에서 새롭게 조회 후 검증한다.(DTO 자체의 중복된 데이터)
*/
private boolean isNotDuplicateQuestion(RequestMakeMultipleChoiceQuestionDto requestDto) {
return majorMultipleChoiceQuestionRepository.findAll().stream()
.noneMatch(existingQuestion -> duplicateQuestionDetector.isQuestionDuplicate(
existingQuestion.getContent(), requestDto.getContent()));
}
/**
* 중복되지 않은 질문인지 확인하는 메서드
* 매번 DB에서 새롭게 조회 후 검증한다.(DTO 자체의 중복된 데이터)
*/
private boolean isNotDuplicateQuestion(RequestMakeMultipleChoiceQuestionDto requestDto) {
return majorMultipleChoiceQuestionRepository.findAll().stream()
.noneMatch(existingQuestion -> duplicateQuestionDetector.isQuestionDuplicate(
existingQuestion.getContent(), requestDto.getContent()));
}

/**
* 새로운 질문을 저장하고 선택지를 저장하는 메서드
*/
private MajorMultipleChoiceQuestion saveMajorMultipleChoiceQuestion(
RequestMakeMultipleChoiceQuestionDto requestDto) {
MajorMultipleChoiceQuestion question = MajorMultipleChoiceQuestion.makeWithDto(requestDto);
questionRepository.save(question);
questionChoiceService.saveWith(requestDto, question);
return question;
}
/**
* 새로운 질문을 저장하고 선택지를 저장하는 메서드
*/
private MajorMultipleChoiceQuestion saveMajorMultipleChoiceQuestion(
RequestMakeMultipleChoiceQuestionDto requestDto) {
MajorMultipleChoiceQuestion question = MajorMultipleChoiceQuestion.makeWithDto(requestDto);
questionRepository.save(question);
questionChoiceService.saveWith(requestDto, question);
return question;
}

private MajorDescriptiveQuestion saveMajorDescriptiveQuestion(
RequestMakeMajorDescriptiveQuestionDto requestDto
) {
MajorDescriptiveQuestion question = MajorDescriptiveQuestion.makeWithDto(
requestDto
);
questionRepository.save(question);
return question;
}
private MajorDescriptiveQuestion saveMajorDescriptiveQuestion(
RequestMakeMajorDescriptiveQuestionDto requestDto
) {
MajorDescriptiveQuestion question = MajorDescriptiveQuestion.makeWithDto(
requestDto
);
questionRepository.save(question);
return question;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@
@Service
@RequiredArgsConstructor
public class UserMajorQuestionClassifiedGetService implements MajorQuestionClassifiedGetService<Question> {
private final MajorMultipleChoiceQuestionDslRepository majorMultipleChoiceQuestionDslRepository;
private final QuestionClassifyByCategoryService questionClassifyByCategoryService;
private final MajorDescriptiveDslRepository majorDescriptiveQuestionDslRepository;
private final MajorMultipleChoiceQuestionDslRepository majorMultipleChoiceQuestionDslRepository;
private final QuestionClassifyByCategoryService questionClassifyByCategoryService;
private final MajorDescriptiveDslRepository majorDescriptiveQuestionDslRepository;

/**
* 분야, 난이도 파라미터로 문제를 조회하는 경우 - 객관식.
* 문제 선택지들을 섞어준다.
*/
@Override
public Map<QuestionCategory, List<Question>> getApprovedClassifiedMajorMultipleChoiceQuestions(
RequestGetQuestionByCategoryAndLevelDto requestGetQuestionByCategoryAndLevelDto) {
List<MajorMultipleChoiceQuestion> majorMultipleChoiceQuestions = majorMultipleChoiceQuestionDslRepository
.findAllCategoriesAndLevelsAndIfApproved(
requestGetQuestionByCategoryAndLevelDto.getQuestionCategories(),
requestGetQuestionByCategoryAndLevelDto.getQuestionLevels(),
true);
for (MajorMultipleChoiceQuestion question : majorMultipleChoiceQuestions) {
Collections.shuffle(question.getQuestionChoices());
}
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorMultipleChoiceQuestions);
}
/**
* 분야, 난이도 파라미터로 문제를 조회하는 경우 - 객관식.
* 문제 선택지들을 섞어준다.
*/
@Override
public Map<QuestionCategory, List<Question>> getApprovedClassifiedMajorMultipleChoiceQuestions(
RequestGetQuestionByCategoryAndLevelDto requestGetQuestionByCategoryAndLevelDto) {
List<MajorMultipleChoiceQuestion> majorMultipleChoiceQuestions = majorMultipleChoiceQuestionDslRepository
.findAllCategoriesAndLevelsAndIfApproved(
requestGetQuestionByCategoryAndLevelDto.getQuestionCategories(),
requestGetQuestionByCategoryAndLevelDto.getQuestionLevels(),
true);
for (MajorMultipleChoiceQuestion question : majorMultipleChoiceQuestions) {
Collections.shuffle(question.getQuestionChoices());
}
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorMultipleChoiceQuestions);
}


/**
* 분야, 난이도 파라미터로 문제를 조회하는 경우 - 서술형
*/
@Override
public Map<QuestionCategory, List<Question>> getApprovedClassifiedDescriptiveQuestions(
RequestGetQuestionByCategoryAndLevelDto dto) {
List<MajorDescriptiveQuestion> majorDescriptiveQuestions = majorDescriptiveQuestionDslRepository
.findAllCategoriesAndLevelsAndIfApproved(
dto.getQuestionCategories(),
dto.getQuestionLevels(),
true
);
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorDescriptiveQuestions);
}
/**
* 분야, 난이도 파라미터로 문제를 조회하는 경우 - 서술형
*/
@Override
public Map<QuestionCategory, List<Question>> getApprovedClassifiedDescriptiveQuestions(
RequestGetQuestionByCategoryAndLevelDto dto) {
List<MajorDescriptiveQuestion> majorDescriptiveQuestions = majorDescriptiveQuestionDslRepository
.findAllCategoriesAndLevelsAndIfApproved(
dto.getQuestionCategories(),
dto.getQuestionLevels(),
true
);
return questionClassifyByCategoryService.classifyQuestionByCategoryOrdered(majorDescriptiveQuestions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void forLicenseQuestion(LicenseSession licenseSession, Model model) {
public void forMajor(Set<QuestionCategory> questionCategories, boolean isMultipleChoice, Model model) {
if (isMultipleChoice) {
addTagToModel(forMajorMultiple(questionCategories), model);
return;
}
addTagToModel(forMajorDescriptive(questionCategories), model);
}
Expand Down
Loading
Loading