-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from GSM-MSG/318-membership-userroll-authoriz…
…ation-student-revision 🔀 :: 회원가입 수락시에 UserRole 추가
- Loading branch information
Showing
3 changed files
with
23 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 22 additions & 16 deletions
38
src/main/kotlin/com/msg/gauth/domain/user/service/AcceptUserSignUpService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,43 @@ | ||
package com.msg.gauth.domain.user.service | ||
|
||
import com.msg.gauth.domain.client.exception.BadUserRoleRequestException | ||
import com.msg.gauth.domain.user.User | ||
import com.msg.gauth.domain.user.UserRole | ||
import com.msg.gauth.domain.user.enums.UserRoleType | ||
import com.msg.gauth.domain.user.enums.UserState | ||
import com.msg.gauth.domain.user.exception.UserNotFoundException | ||
import com.msg.gauth.domain.user.presentation.dto.request.AcceptUserReqDto | ||
import com.msg.gauth.domain.user.repository.UserRepository | ||
import com.msg.gauth.domain.user.repository.UserRoleRepository | ||
import com.msg.gauth.global.annotation.service.TransactionalService | ||
|
||
@TransactionalService | ||
class AcceptUserSignUpService( | ||
private val userRepository: UserRepository | ||
private val userRepository: UserRepository, | ||
private val userRoleRepository: UserRoleRepository | ||
) { | ||
|
||
fun execute(id: Long, acceptUserReqDto: AcceptUserReqDto) = | ||
when(acceptUserReqDto.userRoleType){ | ||
UserRoleType.ROLE_STUDENT -> acceptStudent(id, acceptUserReqDto) | ||
UserRoleType.ROLE_TEACHER -> acceptTeacher(id, acceptUserReqDto) | ||
UserRoleType.ROLE_GRADUATE -> acceptGraduate(id, acceptUserReqDto) | ||
else -> throw BadUserRoleRequestException() | ||
} | ||
fun execute(id: Long, acceptUserReqDto: AcceptUserReqDto) { | ||
val user = acceptUserReqDto.toEntity(getUser(id)) | ||
saveUser(user, acceptUserReqDto) | ||
} | ||
|
||
private fun getUser(id: Long) = | ||
userRepository.findByIdAndState(id, UserState.PENDING) | ||
?: throw UserNotFoundException() | ||
|
||
private fun acceptStudent(id: Long, acceptUserReqDto: AcceptUserReqDto)= | ||
userRepository.save(acceptUserReqDto.toStudentEntity(getUser(id))) | ||
private fun saveUser(user: User, acceptUserReqDto: AcceptUserReqDto) { | ||
saveUserRole(user, acceptUserReqDto.userRoleType) | ||
userRepository.save(user) | ||
} | ||
|
||
private fun saveUserRole(user: User, userRoleType: UserRoleType) { | ||
val userRole = UserRole( | ||
user = user, | ||
userRoleType = userRoleType | ||
) | ||
userRoleRepository.save(userRole) | ||
} | ||
|
||
private fun acceptTeacher(id: Long, acceptUserReqDto: AcceptUserReqDto) = | ||
userRepository.save(acceptUserReqDto.toTeacherEntity(getUser(id))) | ||
|
||
private fun acceptGraduate(id: Long, acceptUserReqDto: AcceptUserReqDto) = | ||
userRepository.save(acceptUserReqDto.toGraduateEntity(getUser(id))) | ||
} | ||
|
||
} |