Skip to content

Commit

Permalink
Merge pull request #54 from mash-up-kr/feature/voting
Browse files Browse the repository at this point in the history
feat: Voting process
  • Loading branch information
210-reverof committed Aug 2, 2024
2 parents f255828 + 65aa1cb commit 18dfbaa
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package com.mashup.pic.vote.applicationservice

import com.mashup.pic.domain.event.EventService
import com.mashup.pic.domain.vote.VoteService
import com.mashup.pic.vote.applicationservice.dto.VoteServiceRequest
import com.mashup.pic.vote.controller.dto.VoteOptionItem
import com.mashup.pic.vote.controller.dto.VoteOptionResponse
import com.mashup.pic.vote.controller.dto.VoteResponse
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PathVariable

@Service
@Transactional(readOnly = true)
class VoteApplicationService(
private val voteService: VoteService
private val voteService: VoteService,
private val eventService: EventService
) {
fun getVoteOptions(
@PathVariable eventId: Long
): VoteOptionResponse {
fun getVoteOptions(eventId: Long): VoteOptionResponse {
val options = voteService.getVoteOptions(eventId).map { VoteOptionItem(it.id, it.imageUrl) }
return VoteOptionResponse(options)
}

@Transactional
fun vote(request: VoteServiceRequest): VoteResponse {
voteService.vote(
userId = request.userId,
eventId = request.eventId,
likedOptionIds = request.likedOptionIds
)
if (voteService.hasEveryoneVoted(request.eventId)) {
eventService.endEventVoting(request.eventId)
}
return VoteResponse(request.eventId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mashup.pic.vote.applicationservice.dto

data class VoteServiceRequest(
val userId: Long,
val eventId: Long,
val likedOptionIds: List<Long>
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.mashup.pic.vote.controller

import com.mashup.pic.common.ApiResponse
import com.mashup.pic.security.authentication.UserInfo
import com.mashup.pic.vote.applicationservice.VoteApplicationService
import com.mashup.pic.vote.controller.dto.VoteOptionResponse
import com.mashup.pic.vote.controller.dto.VoteRequest
import com.mashup.pic.vote.controller.dto.VoteResponse
import com.mashup.pic.vote.controller.dto.toServiceRequest
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

Expand All @@ -25,4 +32,15 @@ class VoteController(
voteApplicationService.getVoteOptions(eventId)
)
}

@Operation(summary = "투표하기 (좋아요한 이미지 목록 보내기)")
@PostMapping
fun getVoteOptions(
@AuthenticationPrincipal user: UserInfo,
@RequestBody request: VoteRequest
): ApiResponse<VoteResponse> {
return ApiResponse.success(
voteApplicationService.vote(request.toServiceRequest(user.id))
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mashup.pic.vote.controller.dto

import com.mashup.pic.vote.applicationservice.dto.VoteServiceRequest
import jakarta.validation.constraints.Size

data class VoteRequest(
val eventId: Long,
@field:Size(min = 1) val likedOptionIds: List<Long>
)

fun VoteRequest.toServiceRequest(userId: Long): VoteServiceRequest = VoteServiceRequest(userId, this.eventId, this.likedOptionIds)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.mashup.pic.vote.controller.dto

data class VoteResponse(
val eventId: Long
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.mashup.pic.domain.vote

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface VoteRepository : JpaRepository<Vote, Long> {
fun existsByEventJoinId(id: Long): Boolean

@Query("SELECT COUNT(DISTINCT v.eventJoinId) FROM Vote v")
fun countDistinctEventJoinIds(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ class VoteService(
return eventImageOptionRepository.findAllByEventJoinIdIn(eventJoinIds).map { it.toDto() }
}

@Transactional
fun vote(
userId: Long,
eventId: Long,
likedOptionIds: List<Long>
) {
val eventJoin = getEventJoinByUserIdAndEventId(userId, eventId)
val likedOptions = likedOptionIds.map { Vote(eventJoin.id, it) }
voteRepository.saveAll(likedOptions)
}

fun hasEveryoneVoted(eventId: Long): Boolean {
val eventJoins = eventJoinRepository.findAllByEventId(eventId)
val votedJoinCnt = voteRepository.countDistinctEventJoinIds()

return eventJoins.size == votedJoinCnt
}

private fun validateEvent(eventId: Long) {
if (!eventRepository.existsById(eventId)) {
throw PicException.of(PicExceptionType.NOT_EXIST, "없는 이벤트")
Expand Down

0 comments on commit 18dfbaa

Please sign in to comment.