Skip to content

Commit

Permalink
refactor: 미팅팀 서비스 리팩토링
Browse files Browse the repository at this point in the history
- userDao 삭제 후 UserService 참고
- getUser 하나의 함수로 통일
- 함수가 SRP를 지킬 수 있도록 수정
  • Loading branch information
seogwoojin committed Nov 24, 2024
1 parent 76c2ce6 commit 2bdb276
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uoslife.servermeeting.meetingteam.service.impl

import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uoslife.servermeeting.meetingteam.dao.UserTeamDao
Expand All @@ -20,41 +20,42 @@ import uoslife.servermeeting.meetingteam.repository.UserTeamRepository
import uoslife.servermeeting.meetingteam.service.BaseMeetingService
import uoslife.servermeeting.meetingteam.service.util.MeetingServiceUtils
import uoslife.servermeeting.meetingteam.util.Validator
import uoslife.servermeeting.payment.dao.PaymentDao
import uoslife.servermeeting.payment.service.PaymentService
import uoslife.servermeeting.user.entity.User
import uoslife.servermeeting.user.exception.UserNotFoundException
import uoslife.servermeeting.user.repository.UserRepository
import uoslife.servermeeting.user.service.UserService

@Service
@Transactional(readOnly = true)
@Qualifier("singleMeetingService")
class SingleMeetingService(
private val userRepository: UserRepository,
private val userService: UserService,
private val meetingTeamRepository: MeetingTeamRepository,
private val meetingServiceUtils: MeetingServiceUtils,
private val preferenceRepository: PreferenceRepository,
private val validator: Validator,
private val userTeamRepository: UserTeamRepository,
private val userTeamDao: UserTeamDao,
private val paymentDao: PaymentDao,
@Qualifier("PortOneService") private val paymentService: PaymentService,
@Value("\${app.season}") private val season: Int,
) : BaseMeetingService {
companion object {
private val logger = LoggerFactory.getLogger(SingleMeetingService::class.java)
}

@Transactional
override fun createMeetingTeam(
userId: Long,
name: String?,
): MeetingTeamCodeResponse {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)
validator.isUserAlreadyHaveSingleTeam(user)

val meetingTeam = createDefaultMeetingTeam(leader = user, teamType = TeamType.SINGLE)

val newUserTeam = UserTeam.createUserTeam(meetingTeam, user, true)

userTeamRepository.save(newUserTeam)
logger.info("[유저 팀 생성] User ID : $userId Single Team ID : ${meetingTeam.id}")
return MeetingTeamCodeResponse(code = null)
}

Expand All @@ -81,7 +82,7 @@ class SingleMeetingService(
validator.isMessageLengthIsValid(meetingTeamInfoUpdateRequest.message)
val validMBTI = validator.setValidMBTI(meetingTeamInfoUpdateRequest.mbti)

val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)
val meetingTeam: MeetingTeam = getUserSingleMeetingTeam(user)

val newPreference = meetingTeamInfoUpdateRequest.toSinglePreference(validMBTI, meetingTeam)
Expand All @@ -92,7 +93,7 @@ class SingleMeetingService(
}

override fun getMeetingTeamInformation(userId: Long): MeetingTeamInformationGetResponse {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)
val meetingTeam: MeetingTeam = getUserSingleMeetingTeam(user)

val preference = meetingTeam.preference ?: throw PreferenceNotFoundException()
Expand All @@ -109,16 +110,17 @@ class SingleMeetingService(

@Transactional
override fun deleteMeetingTeam(userId: Long) {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)
val meetingTeam: MeetingTeam = getUserSingleMeetingTeam(user)

checkTeamPayment(user)
meetingTeamRepository.delete(meetingTeam)
logger.info("[1:1 미팅 팀 삭제] User ID : $userId")
}

private fun checkTeamPayment(user: User) {
val successPayment =
paymentDao.getSuccessPaymentFromUserIdAndTeamType(user.id!!, TeamType.SINGLE)
paymentService.getSuccessPayment(userId = user.id!!, teamType = TeamType.SINGLE)

if (successPayment != null) {
paymentService.refundPaymentByToken(user.id!!, TeamType.SINGLE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uoslife.servermeeting.meetingteam.service.impl

import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uoslife.servermeeting.meetingteam.dao.UserTeamDao
Expand All @@ -23,35 +23,36 @@ import uoslife.servermeeting.meetingteam.service.BaseMeetingService
import uoslife.servermeeting.meetingteam.service.util.MeetingServiceUtils
import uoslife.servermeeting.meetingteam.util.UniqueCodeGenerator
import uoslife.servermeeting.meetingteam.util.Validator
import uoslife.servermeeting.payment.dao.PaymentDao
import uoslife.servermeeting.payment.service.PaymentService
import uoslife.servermeeting.user.entity.User
import uoslife.servermeeting.user.exception.UserNotFoundException
import uoslife.servermeeting.user.repository.UserRepository
import uoslife.servermeeting.user.service.UserService

@Service
@Transactional(readOnly = true)
@Qualifier("tripleMeetingService")
class TripleMeetingService(
private val userRepository: UserRepository,
private val userService: UserService,
private val userTeamDao: UserTeamDao,
private val preferenceRepository: PreferenceRepository,
private val uniqueCodeGenerator: UniqueCodeGenerator,
private val meetingTeamRepository: MeetingTeamRepository,
private val validator: Validator,
private val userTeamRepository: UserTeamRepository,
private val meetingServiceUtils: MeetingServiceUtils,
private val paymentDao: PaymentDao,
@Qualifier("PortOneService") private val paymentService: PaymentService,
@Value("\${app.season}") private val season: Int,
) : BaseMeetingService {

companion object {
private val logger = LoggerFactory.getLogger(TripleMeetingService::class.java)
}

@Transactional
override fun createMeetingTeam(
userId: Long,
name: String?,
): MeetingTeamCodeResponse {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)

validator.isUserAlreadyHaveTripleTeam(user)
validator.isTeamNameInvalid(name)
Expand All @@ -60,7 +61,7 @@ class TripleMeetingService(
val meetingTeam = createDefaultMeetingTeam(user, name, code, TeamType.TRIPLE)

userTeamDao.saveUserTeam(meetingTeam, user, true)

logger.info("[유저 팀 생성] User ID : $userId Triple Team ID : ${meetingTeam.id}")
return MeetingTeamCodeResponse(code)
}

Expand All @@ -70,7 +71,7 @@ class TripleMeetingService(
code: String,
isJoin: Boolean
): MeetingTeamUserListGetResponse? {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)

validator.isTeamCodeValid(code)
validator.isUserAlreadyHaveTripleTeam(user)
Expand All @@ -96,7 +97,6 @@ class TripleMeetingService(
userId: Long,
code: String
): MeetingTeamUserListGetResponse {
userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
validator.isTeamCodeValid(code)

val meetingTeam =
Expand All @@ -121,18 +121,20 @@ class TripleMeetingService(
) {
validator.isMessageLengthIsValid(meetingTeamInfoUpdateRequest.message)

val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val meetingTeam: MeetingTeam = getUserTripleMeetingUserTeam(user).team
val user = userService.getUser(userId)
val meetingUserTeam = getUserTripleMeetingUserTeam(user)
if (!meetingUserTeam.isLeader) throw OnlyTeamLeaderCanUpdateTeamInformationException()

val newPreference = meetingTeamInfoUpdateRequest.toTriplePreference(meetingTeam)
val meetingTeam = meetingUserTeam.team

val newPreference = meetingTeamInfoUpdateRequest.toTriplePreference(meetingTeam)
meetingTeam.preference?.let { preferenceRepository.delete(it) }
meetingTeam.preference = newPreference
meetingTeam.message = meetingTeamInfoUpdateRequest.message
}

override fun getMeetingTeamInformation(userId: Long): MeetingTeamInformationGetResponse {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)
val meetingTeam = getUserTripleMeetingUserTeam(user).team

val preference = meetingTeam.preference ?: throw PreferenceNotFoundException()
Expand All @@ -149,7 +151,7 @@ class TripleMeetingService(

@Transactional
override fun deleteMeetingTeam(userId: Long) {
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userService.getUser(userId)
val meetingUserTeam = getUserTripleMeetingUserTeam(user)

if (!meetingUserTeam.isLeader) throw OnlyTeamLeaderCanDeleteTeamException()
Expand All @@ -159,11 +161,11 @@ class TripleMeetingService(

meetingTeam.userTeams.forEach { userTeamRepository.delete(it) }
meetingTeamRepository.delete(meetingTeam)
logger.info("[3:3 미팅 팀 삭제] User ID : $userId")
}

private fun checkTeamPayment(user: User) {
val successPayment =
paymentDao.getSuccessPaymentFromUserIdAndTeamType(user.id!!, TeamType.SINGLE)
val successPayment = paymentService.getSuccessPayment(user.id!!, teamType = TeamType.TRIPLE)

if (successPayment != null) {
paymentService.refundPaymentByToken(user.id!!, TeamType.SINGLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uoslife.servermeeting.payment.service
import uoslife.servermeeting.meetingteam.entity.enums.TeamType
import uoslife.servermeeting.payment.dto.request.PaymentRequestDto
import uoslife.servermeeting.payment.dto.response.PaymentResponseDto
import uoslife.servermeeting.payment.entity.Payment
import uoslife.servermeeting.user.entity.User

interface PaymentService {
Expand All @@ -24,4 +25,5 @@ interface PaymentService {
fun verifyPayment(userId: Long, teamType: TeamType): PaymentResponseDto.PaymentRequestResponse
fun deleteUserPayment(user: User)
fun synchronizePayment(paymentWebhookResponse: PaymentResponseDto.PaymentWebhookResponse)
fun getSuccessPayment(userId: Long, teamType: TeamType): Payment?
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class PortOneService(
val phoneNumber = user.phoneNumber ?: throw PhoneNumberNotFoundException()

checkUserIsLeader(userTeam)
checkSuccessPayment(userId, teamType)
checkPendingPayment(userId, teamType) // todo : DB 접속 너무 많음

getSuccessPayment(userId, teamType)?.let { throw UserAlreadyHavePaymentException() }
getPendingPayment(userId, teamType)?.let { throw UserAlreadyHavePaymentException() }
// todo : DB 접속 너무 많음
val payment =
Payment.createPayment(
UUID.randomUUID().toString(),
Expand Down Expand Up @@ -96,20 +96,16 @@ class PortOneService(
)
}

private fun checkPendingPayment(userId: Long, teamType: TeamType) {
paymentDao.getPendingPaymentFromUserIdAndTeamType(userId, teamType)?.let {
throw UserAlreadyHavePaymentException()
}
fun getPendingPayment(userId: Long, teamType: TeamType): Payment? {
return paymentDao.getPendingPaymentFromUserIdAndTeamType(userId, teamType)
}

private fun checkUserIsLeader(userTeam: UserTeam) {
if (!userTeam.isLeader) throw OnlyTeamLeaderCanCreatePaymentException()
}

private fun checkSuccessPayment(userId: Long, teamType: TeamType) {
paymentDao.getSuccessPaymentFromUserIdAndTeamType(userId, teamType)?.let {
throw UserAlreadyHavePaymentException()
}
override fun getSuccessPayment(userId: Long, teamType: TeamType): Payment? {
return paymentDao.getSuccessPaymentFromUserIdAndTeamType(userId, teamType)
}

@Transactional
Expand Down
37 changes: 18 additions & 19 deletions src/main/kotlin/uoslife/servermeeting/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uoslife.servermeeting.user.service

import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Lazy
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -11,7 +12,6 @@ import uoslife.servermeeting.meetingteam.entity.enums.TeamType
import uoslife.servermeeting.meetingteam.repository.UserTeamRepository
import uoslife.servermeeting.meetingteam.service.BaseMeetingService
import uoslife.servermeeting.meetingteam.util.Validator
import uoslife.servermeeting.payment.dao.PaymentDao
import uoslife.servermeeting.payment.service.PaymentService
import uoslife.servermeeting.user.command.TeamBranch
import uoslife.servermeeting.user.command.UserCommand
Expand All @@ -30,10 +30,9 @@ class UserService(
private val userTeamRepository: UserTeamRepository,
private val userDao: UserDao,
private val userTeamDao: UserTeamDao,
private val paymentDao: PaymentDao,
private val validator: Validator,
@Qualifier("singleMeetingService") private val singleMeetingService: BaseMeetingService,
@Qualifier("tripleMeetingService") private val tripleMeetingService: BaseMeetingService
@Lazy @Qualifier("singleMeetingService") private val singleMeetingService: BaseMeetingService,
@Lazy @Qualifier("tripleMeetingService") private val tripleMeetingService: BaseMeetingService
) {
companion object {
private val logger = LoggerFactory.getLogger(UserService::class.java)
Expand Down Expand Up @@ -115,7 +114,7 @@ class UserService(
}

fun getUserMeetingTeamBranch(userId: Long): UserBranchResponse {
val user: User = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()

val userTeams =
userTeamDao.findUserTeamWithMeetingTeam(user)
Expand All @@ -124,30 +123,30 @@ class UserService(
tripleTeamBranch = TeamBranch.NOT_CREATED
)

return getMeetingTeamStatus(userId, userTeams)
return determineMeetingTeamStatus(userId, userTeams)
}

private fun getMeetingTeamStatus(userId: Long, userTeams: List<UserTeam>): UserBranchResponse {
private fun determineMeetingTeamStatus(
userId: Long,
userTeams: List<UserTeam>
): UserBranchResponse {
var singleTeamBranch = TeamBranch.NOT_CREATED
var tripleTeamBranch = TeamBranch.NOT_CREATED

userTeams.forEach { userTeam ->
when (userTeam.team.type) {
TeamType.SINGLE -> {
singleTeamBranch = TeamBranch.JUST_CREATED
paymentDao
.getSuccessPaymentFromUserIdAndTeamType(userId, TeamType.SINGLE)
?.let { singleTeamBranch = TeamBranch.COMPLETED }
}
TeamType.TRIPLE -> {
tripleTeamBranch = TeamBranch.JUST_CREATED
paymentDao
.getSuccessPaymentFromUserIdAndTeamType(userId, TeamType.TRIPLE)
?.let { tripleTeamBranch = TeamBranch.COMPLETED }
}
TeamType.SINGLE -> singleTeamBranch = determineTeamBranch(userId, TeamType.SINGLE)
TeamType.TRIPLE -> tripleTeamBranch = determineTeamBranch(userId, TeamType.TRIPLE)
}
}

return UserBranchResponse(singleTeamBranch, tripleTeamBranch)
}

private fun determineTeamBranch(userId: Long, teamType: TeamType): TeamBranch {
paymentService.getSuccessPayment(userId, teamType)?.let {
return TeamBranch.COMPLETED
}
return TeamBranch.JUST_CREATED
}
}

0 comments on commit 2bdb276

Please sign in to comment.