-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement process related with Result
- Loading branch information
1 parent
2b6b59c
commit 4033afa
Showing
5 changed files
with
90 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
pic-domain/src/main/kotlin/com/mashup/pic/domain/event/EventImageOptionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
package com.mashup.pic.domain.event | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
|
||
interface EventImageOptionRepository : JpaRepository<EventImageOption, Long> { | ||
fun existsByEventJoinId(id: Long): Boolean | ||
|
||
fun findAllByEventJoinIdIn(eventJoinIds: List<Long>): List<EventImageOption> | ||
|
||
@Query("SELECT COUNT(DISTINCT e.eventJoinId) FROM EventImageOption e") | ||
fun countDistinctEventJoinIds(): Int | ||
} |
7 changes: 7 additions & 0 deletions
7
pic-domain/src/main/kotlin/com/mashup/pic/domain/result/ResultRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mashup.pic.domain.result | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface ResultRepository : JpaRepository<Result, Long> { | ||
fun findAllByEventIdOrderByImageOrderAsc(eventId: Long): List<Result> | ||
} |
80 changes: 77 additions & 3 deletions
80
pic-domain/src/main/kotlin/com/mashup/pic/domain/result/ResultService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,87 @@ | ||
package com.mashup.pic.domain.result | ||
|
||
import com.mashup.pic.common.exception.PicException | ||
import com.mashup.pic.common.exception.PicExceptionType | ||
import com.mashup.pic.domain.event.EventImageOption | ||
import com.mashup.pic.domain.event.EventImageOptionRepository | ||
import com.mashup.pic.domain.event.EventJoin | ||
import com.mashup.pic.domain.event.EventJoinRepository | ||
import com.mashup.pic.domain.vote.Vote | ||
import com.mashup.pic.domain.vote.VoteRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import kotlin.random.Random | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
class ResultService { | ||
class ResultService( | ||
private val eventJoinRepository: EventJoinRepository, | ||
private val eventImageOptionRepository: EventImageOptionRepository, | ||
private val voteRepository: VoteRepository, | ||
private val resultRepository: ResultRepository | ||
) { | ||
@Transactional | ||
fun generateResult(eventId: Long) { | ||
val eventJoinIds = getEvenJoinsByEventId(eventId).map { it.id } | ||
val imageOptionIds = getImageOptionIdsByEventJoins(eventJoinIds).map { it.id } | ||
val votes = getVotesByImageOptionIds(imageOptionIds) | ||
val frequencyMap = votes.groupingBy { it.eventImageOptionId }.eachCount() | ||
val voteResults = | ||
imageOptionIds.map { imageOptionId -> | ||
Pair(imageOptionId, frequencyMap[imageOptionId] ?: 0) | ||
} | ||
val resultOptions = | ||
voteResults.sortedWith(compareByDescending<Pair<Long, Int>> { it.second }.thenBy { it.first }) | ||
.take(4) | ||
|
||
val resultsToSave = | ||
resultOptions.mapIndexed { index, (imageOptionId) -> | ||
Result( | ||
eventId = eventId, | ||
eventImageOptionId = imageOptionId, | ||
frame = getRandomFrame(), | ||
imageOrder = index + 1 | ||
) | ||
} | ||
resultRepository.saveAll(resultsToSave) | ||
} | ||
|
||
fun getResultOfEvent(eventId: Long): ResultDto { | ||
// TODO: 투표 결과가 가장 높은 4개 순서대로 리스트 반환 | ||
return ResultDto(ArrayList()) | ||
return ResultDto( | ||
getResultsByEventId(eventId).map { result -> | ||
val imageUrl = getImageOptionById(result.eventImageOptionId).imageUrl | ||
ResultItem( | ||
imageUrl = imageUrl, | ||
frame = result.frame | ||
) | ||
} | ||
) | ||
} | ||
|
||
private fun getImageOptionById(imageOptionId: Long): EventImageOption { | ||
return eventImageOptionRepository.findByIdOrNull(imageOptionId) | ||
?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID, "없는 이미지 항목") | ||
} | ||
|
||
private fun getResultsByEventId(eventId: Long): List<Result> { | ||
return resultRepository.findAllByEventIdOrderByImageOrderAsc(eventId) | ||
} | ||
|
||
private fun getVotesByImageOptionIds(imageOptionIds: List<Long>): List<Vote> { | ||
return voteRepository.findAllByEventImageOptionIdIn(imageOptionIds) | ||
} | ||
|
||
private fun getEvenJoinsByEventId(eventId: Long): List<EventJoin> { | ||
return eventJoinRepository.findAllByEventId(eventId) | ||
} | ||
|
||
private fun getImageOptionIdsByEventJoins(eventJoinIds: List<Long>): List<EventImageOption> { | ||
return eventImageOptionRepository.findAllByEventJoinIdIn(eventJoinIds) | ||
} | ||
|
||
fun getRandomFrame(): Frame { | ||
val frames = Frame.entries.toTypedArray() | ||
return frames[Random.nextInt(frames.size)] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters