Skip to content

Commit

Permalink
Merge pull request #69 from mash-up-kr/fix/group-result-update
Browse files Browse the repository at this point in the history
fix: Add auto result creating
  • Loading branch information
210-reverof authored Aug 15, 2024
2 parents 38cc474 + 99ceaf8 commit dd95727
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mashup.pic.group.applicationservice
import com.mashup.pic.common.exception.PicException
import com.mashup.pic.common.exception.PicExceptionType
import com.mashup.pic.domain.event.EventDto
import com.mashup.pic.domain.event.EventJoinRepository
import com.mashup.pic.domain.event.EventService
import com.mashup.pic.domain.event.EventStatus
import com.mashup.pic.domain.event.UploadService
Expand Down Expand Up @@ -41,7 +42,8 @@ class GroupApplicationService(
private val eventService: EventService,
private val uploadService: UploadService,
private val voteService: VoteService,
private val resultService: ResultService
private val resultService: ResultService,
private val join: EventJoinRepository
) {
@Transactional
fun create(request: CreateGroupServiceRequest): CreateGroupResponse {
Expand All @@ -59,6 +61,10 @@ class GroupApplicationService(
userId = request.userId,
groupId = groupId
)
val event = eventService.getLastEvent(groupId)
if (event != null && event.eventStatus != EventStatus.COMPLETE) {
eventService.joinEvent(request.userId, event.id)
}
return JoinGroupResponse(groupId)
}

Expand Down
30 changes: 21 additions & 9 deletions pic-api/src/main/kotlin/com/mashup/pic/util/InviteCodeUtil.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package com.mashup.pic.util

import java.nio.charset.StandardCharsets
import java.util.Base64

object InviteCodeUtil {
private const val BASE62 =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
private const val CODE_LENGTH = 8

fun generateInviteCode(id: Long): String {
val idString = String.format("%08d", id)
val encodedBytes = Base64.getUrlEncoder().withoutPadding().encode(idString.toByteArray(StandardCharsets.UTF_8))
return String(encodedBytes, StandardCharsets.UTF_8)
var number = id
val result = StringBuilder()

repeat(CODE_LENGTH) {
val index = (number % 62).toInt()
result.insert(0, BASE62[index])
number /= 62
}

return result.toString()
}

fun getIdFromInviteCode(inviteCode: String): Long {
val decodedBytes = Base64.getUrlDecoder().decode(inviteCode)
val idString = String(decodedBytes, StandardCharsets.UTF_8)
return idString.toLong()
var result = 0L
for (char in inviteCode) {
val index = BASE62.indexOf(char)
result = result * 62 + index
}

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import org.junit.jupiter.api.Test

class InviteCodeUtilTest {
@Test
fun testGenerateAndGetId() {
val originalId: Long = 12345678

val inviteCode = InviteCodeUtil.generateInviteCode(originalId)
fun testGenerateAndGetIdFromInviteCode() {
val id = 12345434L
val inviteCode = InviteCodeUtil.generateInviteCode(id)
val decodedId = InviteCodeUtil.getIdFromInviteCode(inviteCode)

assertEquals(originalId, decodedId)
assertEquals(id, decodedId)
}

@Test
fun testGenerateInviteCodeWithDifferentIds() {
val ids = listOf(1L, 123L, 123456L, 99999999L)
fun testDifferentIds() {
val id1 = 1L
val id2 = 2L
val inviteCode1 = InviteCodeUtil.generateInviteCode(id1)
val inviteCode2 = InviteCodeUtil.generateInviteCode(id2)
assert(inviteCode1 != inviteCode2)
}

for (id in ids) {
val inviteCode = InviteCodeUtil.generateInviteCode(id)
val decodedId = InviteCodeUtil.getIdFromInviteCode(inviteCode)
assertEquals(id, decodedId)
}
@Test
fun testEncodeDecodeConsistency() {
val id = 9872441L
val encoded = InviteCodeUtil.generateInviteCode(id)
val decoded = InviteCodeUtil.getIdFromInviteCode(encoded)
assertEquals(id, decoded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ class EventService(
eventJoin.isVisited = true
}

@Transactional
fun joinEvent(
userId: Long,
eventId: Long
) {
eventJoinRepository.save(
EventJoin(
userId = userId,
eventId = eventId
)
)
}

private fun createEventJoinsByGroup(
eventId: Long,
groupId: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mashup.pic.common.exception.PicException
import com.mashup.pic.common.exception.PicExceptionType
import com.mashup.pic.domain.event.EventService
import com.mashup.pic.domain.event.EventStatus
import com.mashup.pic.domain.result.ResultService
import org.springframework.data.redis.connection.Message
import org.springframework.data.redis.connection.MessageListener
import org.springframework.stereotype.Component
Expand All @@ -13,7 +14,8 @@ import org.springframework.stereotype.Component
*/
@Component
class RedisMessageListener(
private val eventService: EventService
private val eventService: EventService,
private val resultService: ResultService
) : MessageListener {
override fun onMessage(
message: Message,
Expand All @@ -22,7 +24,10 @@ class RedisMessageListener(
val eventInfo = ExpiredEventInfo.parseMessage(message)
when (ChannelTopic.from(eventInfo.topic)) {
ChannelTopic.EVENT_OPEN -> eventService.endEventUploading(eventInfo.eventId)
ChannelTopic.VOTE_OPEN -> eventService.endEventVoting(eventInfo.eventId)
ChannelTopic.VOTE_OPEN -> {
eventService.endEventVoting(eventInfo.eventId)
resultService.generateResult(eventInfo.eventId)
}
}
}
}
Expand Down

0 comments on commit dd95727

Please sign in to comment.