Skip to content

Commit

Permalink
feat: Implement process related with Result
Browse files Browse the repository at this point in the history
  • Loading branch information
210-reverof committed Aug 4, 2024
1 parent 2b6b59c commit 4033afa
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mashup.pic.vote.applicationservice

import com.mashup.pic.domain.event.EventService
import com.mashup.pic.domain.result.ResultService
import com.mashup.pic.domain.vote.VoteService
import com.mashup.pic.vote.applicationservice.dto.VoteServiceRequest
import com.mashup.pic.vote.controller.dto.VoteOptionItem
Expand All @@ -13,7 +14,8 @@ import org.springframework.transaction.annotation.Transactional
@Transactional(readOnly = true)
class VoteApplicationService(
private val voteService: VoteService,
private val eventService: EventService
private val eventService: EventService,
private val resultService: ResultService
) {
fun getVoteOptions(eventId: Long): VoteOptionResponse {
val options = voteService.getVoteOptions(eventId).map { VoteOptionItem(it.id, it.imageUrl) }
Expand All @@ -31,6 +33,7 @@ class VoteApplicationService(

if (voteService.hasEveryoneVoted(request.eventId)) {
eventService.endEventVoting(request.eventId)
resultService.generateResult(10)
}
return VoteResponse(request.eventId)
}
Expand Down
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
}
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>
}
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)]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface VoteRepository : JpaRepository<Vote, Long> {

@Query("SELECT COUNT(DISTINCT v.eventJoinId) FROM Vote v")
fun countDistinctEventJoinIds(): Int

fun findAllByEventImageOptionIdIn(imageOptionIds: List<Long>): List<Vote>
}

0 comments on commit 4033afa

Please sign in to comment.