Skip to content

Commit

Permalink
update :: StudentActivityService infix 함수 적용 find is null throw 로직을 단…
Browse files Browse the repository at this point in the history
…순화 시킵니다
  • Loading branch information
esperar committed Nov 13, 2023
1 parent 7a0b44b commit d8a7e28
Showing 1 changed file with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.springframework.transaction.annotation.Transactional
import team.msg.common.enums.ApproveStatus
import team.msg.common.util.UserUtil
import team.msg.domain.bbozzak.model.Bbozzak
import team.msg.domain.club.model.Club
import team.msg.domain.company.model.CompanyInstructor
import team.msg.domain.government.model.Government
import team.msg.domain.professor.model.Professor
Expand All @@ -28,6 +29,7 @@ import team.msg.domain.student.repository.StudentRepository
import team.msg.domain.teacher.exception.TeacherNotFoundException
import team.msg.domain.teacher.model.Teacher
import team.msg.domain.teacher.repository.TeacherRepository
import team.msg.domain.user.model.User
import java.util.*

@Service
Expand All @@ -47,11 +49,9 @@ class StudentActivityServiceImpl(
override fun createStudentActivity(request: CreateStudentActivityRequest) {
val user = userUtil.queryCurrentUser()

val student = studentRepository.findByUser(user)
?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ userId = ${user.id} ]")
val student = studentRepository findByUser user

val teacher = teacherRepository.findByClub(student.club)
?: throw TeacherNotFoundException("취업 동아리 선생님을 찾을 수 없습니다.")
val teacher = teacherRepository findByClub student.club

val studentActivity = StudentActivity(
id = UUID.randomUUID(),
Expand All @@ -77,11 +77,9 @@ class StudentActivityServiceImpl(
override fun updateStudentActivity(id: UUID, request: UpdateStudentActivityRequest) {
val user = userUtil.queryCurrentUser()

val student = studentRepository.findByUser(user)
?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ userId = ${user.id} ]")
val student = studentRepository findByUser user

val studentActivity = studentActivityRepository.findByIdOrNull(id)
?: throw StudentActivityNotFoundException("학생 활동을 찾을 수 없습니다. info : [ studentActivityId = $id ]")
val studentActivity = studentActivityRepository findById id

if(student.id != studentActivity.student.id)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ studentId = ${student.id} ]")
Expand All @@ -108,11 +106,9 @@ class StudentActivityServiceImpl(
override fun deleteStudentActivity(id: UUID) {
val user = userUtil.queryCurrentUser()

val student = studentRepository.findByUser(user)
?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ userId = ${user.id} ]")
val student = studentRepository findByUser user

val studentActivity = studentActivityRepository.findByIdOrNull(id)
?: throw StudentActivityNotFoundException("학생 활동을 찾을 수 없습니다. info : [ studentActivityId = $id ]")
val studentActivity = studentActivityRepository findById id

if(student != studentActivity.student)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ studentId = ${student.id} ]")
Expand All @@ -128,11 +124,9 @@ class StudentActivityServiceImpl(
override fun rejectStudentActivity(id: UUID) {
val user = userUtil.queryCurrentUser()

val teacher = teacherRepository.findByUser(user)
?: throw TeacherNotFoundException("취업 동아리 선생님을 찾을 수 없습니다. info : [ userId = ${user.id} ]")
val teacher = teacherRepository findByUser user

val studentActivity = studentActivityRepository.findByIdOrNull(id)
?: throw StudentActivityNotFoundException("학생 활동을 찾을 수 없습니다. info : [ studentActivityId = $id ]")
val studentActivity = studentActivityRepository findById id

if(teacher != studentActivity.teacher)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ teacherId = ${teacher.id} ]")
Expand All @@ -148,11 +142,9 @@ class StudentActivityServiceImpl(
override fun approveStudentActivity(id: UUID) {
val user = userUtil.queryCurrentUser()

val teacher = teacherRepository.findByUser(user)
?: throw TeacherNotFoundException("취업 동아리 선생님을 찾을 수 없습니다. info : [ userId = ${user.id} ]")
val teacher = teacherRepository findByUser user

val studentActivity = studentActivityRepository.findByIdOrNull(id)
?: throw StudentActivityNotFoundException("학생 활동을 찾을 수 없습니다. info : [ studentActivityId = $id ]")
val studentActivity = studentActivityRepository findById id

if(teacher != studentActivity.teacher)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ teacherId = ${teacher.id} ]")
Expand Down Expand Up @@ -196,11 +188,9 @@ class StudentActivityServiceImpl(
override fun queryStudentActivitiesByStudent(studentId: UUID, pageable: Pageable): StudentActivitiesResponse {
val user = userUtil.queryCurrentUser()

val student = studentRepository.findStudentById(studentId)
?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ studentId = $studentId ]")
val student = studentRepository findByUser user

val teacher = teacherRepository.findByUser(user)
?: throw TeacherNotFoundException("취업 동아리 선생님을 찾을 수 없습니다. info : [ userId = ${user.id} ]")
val teacher = teacherRepository findByUser user

if(student.club != teacher.club)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ teacherId = ${teacher.id} ]")
Expand All @@ -222,8 +212,7 @@ class StudentActivityServiceImpl(
override fun queryMyStudentActivities(pageable: Pageable): StudentActivitiesResponse {
val user = userUtil.queryCurrentUser()

val student = studentRepository.findByUser(user)
?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ userId = ${user.id}, username = ${user.name} ]")
val student = studentRepository findByUser user

val studentActivities = studentActivityRepository.findAllByStudent(student, pageable)

Expand All @@ -244,8 +233,7 @@ class StudentActivityServiceImpl(

val entity = userUtil.getAuthorityEntityAndOrganization(user).first

val studentActivity = studentActivityRepository.findByIdOrNull(id)
?: throw StudentActivityNotFoundException("학생 활동을 찾을 수 없습니다. info : [ studentActivityId = $id ]")
val studentActivity = studentActivityRepository findById id

when(entity) {
is Student -> {
Expand All @@ -266,4 +254,20 @@ class StudentActivityServiceImpl(

return response
}

/**
* find ?: throw 로직을 단순화시킨 infix 함수들입니다.
*/
private infix fun StudentRepository.findByUser(user: User): Student =
this.findByUser(user) ?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ userId = ${user.id} ]")

private infix fun StudentActivityRepository.findById(id: UUID): StudentActivity =
this.findByIdOrNull(id) ?: throw StudentActivityNotFoundException("학생 활동을 찾을 수 없습니다. info : [ studentActivityId = $id ]")

private infix fun TeacherRepository.findByUser(user: User): Teacher =
this.findByUser(user) ?: throw TeacherNotFoundException("취업 동아리 선생님을 찾을 수 없습니다. info : [ userId = ${user.id} ]")

private infix fun TeacherRepository.findByClub(club: Club): Teacher =
this.findByClub(club) ?: throw TeacherNotFoundException("관련 동아리의 취업 동아리 선생님을 찾을 수 없습니다. info : [ clubId = ${club.id} ]")

}

0 comments on commit d8a7e28

Please sign in to comment.