Skip to content

Commit

Permalink
Merge pull request #99 from mash-up-kr/junhyoung/fix-candidate-duplic…
Browse files Browse the repository at this point in the history
…ation

fix: 질문마다 후보자를 다르게 설정
  • Loading branch information
toychip authored Aug 17, 2024
2 parents 3e2adc3 + b117fb4 commit f955948
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 41 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ project(":entity") {
apply(plugin = "org.jetbrains.kotlin.plugin.jpa")

dependencies {
api(project(":common"))
api("org.springframework.boot:spring-boot-starter-data-jpa")
api("com.mysql:mysql-connector-j:${properties["mysqlConnectorVersion"]}")
runtimeOnly("com.h2database:h2:${properties["h2DatabaseVersion"]}") // todo : fade out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class DojoExceptionType(
QUESTION_NOT_EXIST("Question is not exist", "Q004_QUESTION_NOT_EXIST", 500),
QUESTION_SHEET_NOT_EXIST("QuestionSheet is not exist", "Q005_QUESTION_SHEET_NOT_EXIST", 500),
QUESTION_LACK_FOR_CREATE_QUESTION_SET("Question is lack for creation QSet", "Q006_QUESTION_LACK", 500),
QUESTION_INVALID_TYPE("Question Type is Invalid Type", "Q007_QUESTION_TYPE", 500),
QUESTION_NOT_FOUND("Question is not found", "Q005_QUESTION_NOT_EXIST", 404),

// friend
Expand Down
66 changes: 34 additions & 32 deletions service/src/main/kotlin/com/mashup/dojo/service/QuestionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ interface QuestionService {
endAt: LocalDateTime,
): QuestionSet

fun createQuestionSheetsForMember(
questionSet: QuestionSet,
fun saveQuestionSheets(allMemberQuestionSheets: List<QuestionSheet>): List<QuestionSheet>

fun createQuestionSheetForSingleQuestion(
questionSetId: QuestionSetId,
questionId: QuestionId,
resolver: MemberId,
candidatesOfFriend: List<MemberId>,
candidatesOfAccompany: List<MemberId>,
resolver: MemberId,
): List<QuestionSheet>
questionType: QuestionType,
): QuestionSheet

fun saveQuestionSheets(allMemberQuestionSheets: List<QuestionSheet>): List<QuestionSheet>
fun getQuestionType(questionId: QuestionId): Question
}

@Service
Expand Down Expand Up @@ -227,44 +231,42 @@ class DefaultQuestionService(
return questionSet
}

override fun getQuestionType(questionId: QuestionId): Question {
return questionRepository.findByIdOrNull(questionId.value)?.toQuestion()
?: throw DojoException.of(DojoExceptionType.QUESTION_NOT_EXIST)
}

@Transactional
override fun createQuestionSheetsForMember(
questionSet: QuestionSet,
override fun createQuestionSheetForSingleQuestion(
questionSetId: QuestionSetId,
questionId: QuestionId,
resolver: MemberId,
candidatesOfFriend: List<MemberId>,
candidatesOfAccompany: List<MemberId>,
resolver: MemberId,
): List<QuestionSheet> {
questionType: QuestionType,
): QuestionSheet {
/**
* ToDo 아래는 추후 캐시에 넣는 작업을 해야합니다.
* - cache put -> QuestionSet and return
* - Temporarily set to create for all members, discuss details later
* 질문의 타입에 따라 적절한 후보자 리스트를 사용
*/

val questionIds = questionSet.questionIds.map { questionOrder -> questionOrder.questionId.value }
val friendQuestionIds = questionRepository.findFriendQuestionsByIds(questionIds)
val accompanyQuestionIds = questionRepository.findAccompanyQuestionsByIds(questionIds)

val friendQuestionSheets =
friendQuestionIds.map { friendQuestionId ->
QuestionSheet.create(
questionSetId = questionSet.id,
questionId = QuestionId(friendQuestionId),
resolverId = resolver,
candidates = candidatesOfFriend
)
}

val accompanyQuestionSheets =
accompanyQuestionIds.map { friendQuestionId ->
QuestionSheet.create(
questionSetId = questionSet.id,
questionId = QuestionId(friendQuestionId),
resolverId = resolver,
candidates = candidatesOfAccompany
)
val candidates =
if (questionType == QuestionType.FRIEND) {
candidatesOfFriend
} else if (questionType == QuestionType.ACCOMPANY) {
candidatesOfAccompany
} else {
throw DojoException.of(DojoExceptionType.QUESTION_INVALID_TYPE)
}

return friendQuestionSheets + accompanyQuestionSheets
return QuestionSheet.create(
questionSetId = questionSetId,
questionId = questionId,
resolverId = resolver,
candidates = candidates
)
}

override fun saveQuestionSheets(allMemberQuestionSheets: List<QuestionSheet>): List<QuestionSheet> {
Expand Down
37 changes: 28 additions & 9 deletions service/src/main/kotlin/com/mashup/dojo/usecase/QuestionUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,38 @@ class DefaultQuestionUseCase(

@Transactional
override fun createQuestionSheet(): List<QuestionSheet> {
val currentQuestionSet = questionService.getNextOperatingQuestionSet() ?: throw DojoException.of(DojoExceptionType.QUESTION_SET_NOT_READY)
val currentQuestionSet =
questionService.getNextOperatingQuestionSet()
?: throw DojoException.of(DojoExceptionType.QUESTION_SET_NOT_READY)

val allMemberRecords = memberService.findAllMember()

val questions =
currentQuestionSet.questionIds.map { questionOrder ->
questionService.getQuestionType(questionOrder.questionId)
}

val allMemberQuestionSheets =
allMemberRecords.flatMap { member ->
val candidateOfFriend = memberRelationService.findCandidateOfFriend(member.id)
val candidateOfAccompany = memberRelationService.findCandidateOfAccompany(member.id)
questionService.createQuestionSheetsForMember(
questionSet = currentQuestionSet,
candidatesOfFriend = candidateOfFriend,
candidatesOfAccompany = candidateOfAccompany,
resolver = member.id
)
// 각 질문별로 후보자를 선택
val questionSheets =
questions.map { questionOrder ->
val candidatesOfFriend = memberRelationService.findCandidateOfFriend(member.id)
val candidatesOfAccompany = memberRelationService.findCandidateOfAccompany(member.id)

val questionType = questionOrder.type

// 질문의 타입에 따라 다른 후보자 리스트를 전달
questionService.createQuestionSheetForSingleQuestion(
questionSetId = currentQuestionSet.id,
questionId = questionOrder.id,
questionType = questionType,
resolver = member.id,
candidatesOfFriend = candidatesOfFriend,
candidatesOfAccompany = candidatesOfAccompany
)
}
questionSheets
}

return questionService.saveQuestionSheets(allMemberQuestionSheets)
Expand Down

0 comments on commit f955948

Please sign in to comment.