Skip to content

Commit

Permalink
refac: join code
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 30, 2024
1 parent 55d45f5 commit 303093d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,57 @@ import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.auth.model.AuthUser
import com.hero.alignlab.domain.group.domain.Group
import com.hero.alignlab.domain.group.domain.GroupUser
import com.hero.alignlab.domain.group.model.CreateGroupContext
import com.hero.alignlab.domain.group.model.request.CreateGroupRequest
import com.hero.alignlab.domain.group.model.response.CreateGroupResponse
import com.hero.alignlab.domain.group.model.response.GetGroupResponse
import com.hero.alignlab.domain.group.model.response.JoinGroupResponse
import com.hero.alignlab.event.model.CreateGroupEvent
import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.InvalidRequestException
import com.hero.alignlab.exception.NotFoundException
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import java.util.*

@Service
class GroupFacade(
private val groupService: GroupService,
private val groupUserService: GroupUserService,
private val txTemplates: TransactionTemplates,
private val publisher: ApplicationEventPublisher,
) {
suspend fun createGroup(user: AuthUser, request: CreateGroupRequest): CreateGroupResponse {
return parZip(
{ groupService.existsByName(request.name) },
{ groupUserService.existsByUid(user.uid) }
) { existsByName, existsByUid ->
if (existsByName) {
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_NAME_ERROR)
}

if (existsByUid) {
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_JOIN_ERROR)
}

val group = CreateGroupContext(user, request).create()

val createdGroup = createGroup(user, group)

CreateGroupResponse.from(createdGroup)
}
}

fun createGroup(user: AuthUser, group: Group): Group {
return txTemplates.writer.executes {
val createdGroup = groupService.saveSync(group)

publisher.publishEvent(CreateGroupEvent(createdGroup))

createdGroup
}
}

suspend fun withdraw(user: AuthUser, groupId: Long) {
val group = groupService.findByIdOrThrow(groupId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,26 @@ import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.auth.model.AuthUser
import com.hero.alignlab.domain.group.domain.Group
import com.hero.alignlab.domain.group.infrastructure.GroupRepository
import com.hero.alignlab.domain.group.infrastructure.GroupUserRepository
import com.hero.alignlab.domain.group.model.request.CreateGroupRequest
import com.hero.alignlab.domain.group.model.UpdateGroupContext
import com.hero.alignlab.domain.group.model.request.UpdateGroupRequest
import com.hero.alignlab.domain.group.model.response.CreateGroupResponse
import com.hero.alignlab.domain.group.model.response.UpdateGroupResponse
import com.hero.alignlab.event.model.CreateGroupEvent
import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.InvalidRequestException
import com.hero.alignlab.exception.NotFoundException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import java.util.*

@Service
class GroupService(
private val groupRepository: GroupRepository,
private val groupUserRepository: GroupUserRepository,
private val txTemplates: TransactionTemplates,
private val publisher: ApplicationEventPublisher,
) {
suspend fun createGroup(user: AuthUser, request: CreateGroupRequest): CreateGroupResponse {
if (existsByName(request.name)) {
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_NAME_ERROR)
}

if (withContext(Dispatchers.IO) { groupUserRepository.findAllByUid(user.uid) }.isNotEmpty()) {
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_JOIN_ERROR)
}

val createdGroup = txTemplates.writer.executes {
val group = groupRepository.save(
Group(
name = request.name,
description = request.description,
ownerUid = user.uid,
isHidden = request.isHidden,
joinCode = request.joinCode ?: UUID.randomUUID().toString().replace("-", "")
)
)

publisher.publishEvent(CreateGroupEvent(group))

group
}

return CreateGroupResponse.from(createdGroup)
}

suspend fun updateGroup(user: AuthUser, id: Long, request: UpdateGroupRequest): UpdateGroupResponse {
val group = findByIdAndOwnerUidOrThrow(id, user.uid)

val updatedGroup = txTemplates.writer.executes {
group.apply {
this.name = request.name
this.description = request.description
this.isHidden = request.isHidden
this.joinCode = request.joinCode ?: UUID.randomUUID().toString().replace("-", "")
}.run { groupRepository.save(this) }
groupRepository.save(UpdateGroupContext(group, request).update())
}

return UpdateGroupResponse.from(updatedGroup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import org.springframework.transaction.annotation.Transactional
class GroupUserService(
private val groupUserRepository: GroupUserRepository,
) {
suspend fun existsByUid(uid: Long): Boolean {
return withContext(Dispatchers.IO) {
groupUserRepository.existsByUid(uid)
}
}

suspend fun findAllByUid(uid: Long): List<GroupUser> {
return withContext(Dispatchers.IO) {
findAllByUid(uid)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hero.alignlab.domain.group.application

import java.util.*

object JoinCodeGenerator {
fun joinCode(code: String? = null): String {
return code ?: UUID.randomUUID().toString().replace("-", "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ interface GroupUserRepository : JpaRepository<GroupUser, Long> {
fun existsByGroupIdAndUid(groupId: Long, uid: Long): Boolean

fun findAllByGroupId(groupId: Long, pageable: Pageable): Page<GroupUser>

fun existsByUid(uid: Long): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hero.alignlab.domain.group.model

import com.hero.alignlab.domain.auth.model.AuthUser
import com.hero.alignlab.domain.group.application.JoinCodeGenerator
import com.hero.alignlab.domain.group.domain.Group
import com.hero.alignlab.domain.group.model.request.CreateGroupRequest

class CreateGroupContext(
private val user: AuthUser,
private val request: CreateGroupRequest,
) {
fun create(): Group {
return Group(
name = request.name,
description = request.description,
ownerUid = user.uid,
isHidden = request.isHidden,
joinCode = JoinCodeGenerator.joinCode(request.joinCode)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.hero.alignlab.domain.group.model

import com.hero.alignlab.domain.group.application.JoinCodeGenerator
import com.hero.alignlab.domain.group.domain.Group
import com.hero.alignlab.domain.group.model.request.UpdateGroupRequest

class UpdateGroupContext(
private val before: Group,
private val request: UpdateGroupRequest,
) {
fun update(): Group {
return this.before.apply {
this.name = request.name
this.description = request.description
this.isHidden = request.isHidden
this.joinCode = JoinCodeGenerator.joinCode(request.joinCode)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GroupResource(
suspend fun createGroup(
user: AuthUser,
@RequestBody request: CreateGroupRequest
) = groupService.createGroup(user, request).wrapCreated()
) = groupFacade.createGroup(user, request).wrapCreated()

@Operation(summary = "그룹 수정하기")
@PutMapping("/api/v1/groups/{id}")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ hero:
password:
hikari:
minimum-idle: 10
maximum-pool-size: 20
maximum-pool-size: 15
jpa:
properties:
hibernate.hbm2ddl.auto: none
Expand Down

0 comments on commit 303093d

Please sign in to comment.