Skip to content

Commit

Permalink
delete :: 검증로직 함수로 분리 롤백
Browse files Browse the repository at this point in the history
  • Loading branch information
KimTaeO committed Oct 24, 2023
1 parent 1294d12 commit f52c51a
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ class StudentActivityServiceImpl(
*/
@Transactional(rollbackFor = [Exception::class])
override fun updateStudentActivity(id: UUID, request: UpdateStudentActivityRequest) {
val studentActivity = validateStudent(id)
val user = userUtil.queryCurrentUser()

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

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

if(student.id != studentActivity.student.id)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ studentId = ${student.id} ]")

val updatedStudentActivity = StudentActivity(
id = studentActivity.id,
Expand All @@ -87,8 +96,17 @@ class StudentActivityServiceImpl(
*/
@Transactional(rollbackFor = [Exception::class])
override fun deleteStudentActivity(id: UUID) {
val studentActivity = validateStudent(id)

val user = userUtil.queryCurrentUser()

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

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

if(student.id != studentActivity.student.id)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ studentId = ${student.id} ]")

studentActivityRepository.delete(studentActivity)
}

Expand Down Expand Up @@ -117,16 +135,7 @@ class StudentActivityServiceImpl(
* @param 학생활동을 검증하기 위한 id
*/
private fun validateStudent(id: UUID): StudentActivity {
val user = userUtil.queryCurrentUser()

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

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

if(student.id != studentActivity.student.id)
throw ForbiddenStudentActivityException("해당 학생 활동에 대한 권한이 없습니다. info : [ studentId = ${student.id} ]")

return studentActivity
}
Expand Down

0 comments on commit f52c51a

Please sign in to comment.