Skip to content

Commit ffcf88d

Browse files
committed
fix : 이미 가입 된 유저 또는 가입 신청을 했던 유저의 가입 신청 제한
관련 Service 코드 추가, Repository 함수 정의, 컨트롤러 수정
1 parent ab6d55b commit ffcf88d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/main/kotlin/com/example/lsa/lab/controller/LabController.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.example.lsa.lab.dto.LabDto
44
import com.example.lsa.lab.dto.LabMembershipRequestDto
55
import com.example.lsa.member.dto.UserInfoDto
66
import com.example.lsa.lab.service.LabService
7+
import org.springframework.http.HttpStatus
78
import org.springframework.http.ResponseEntity
89
import org.springframework.web.bind.annotation.*
910

@@ -15,8 +16,12 @@ class LabController(
1516

1617
@PostMapping("/request-membership")
1718
fun requestMembership(@RequestParam userId: Long, @RequestParam labId: Long): ResponseEntity<String> {
18-
labService.requestLabMembership(userId, labId)
19-
return ResponseEntity.ok("멤버쉽 요청 전송")
19+
return try {
20+
labService.requestLabMembership(userId, labId)
21+
ResponseEntity.ok("멤버쉽 요청이 성공적으로 처리되었습니다.")
22+
}catch(e: IllegalStateException){
23+
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.message)
24+
}
2025
}
2126

2227
@PostMapping("/respond-to-request")

src/main/kotlin/com/example/lsa/lab/repo/LabMembershipRequestRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import org.springframework.stereotype.Repository
88
interface LabMembershipRequestRepository : JpaRepository<LabMembershipRequest, Long> {
99
fun findAllByLab_Id(labId: Long): List<LabMembershipRequest>
1010
fun findAllByUser_Id(userId: Long): List<LabMembershipRequest>
11+
fun findByUserIdAndLabId(userId: Long, labId: Long): LabMembershipRequest?
1112
}

src/main/kotlin/com/example/lsa/lab/service/LabService.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ class LabService(
2424
val user = userRepository.findById(userId).orElseThrow { IllegalArgumentException("사용자를 찾을 수 없습니다") }
2525
val lab = labRepository.findById(labId).orElseThrow { IllegalArgumentException("연구실을 찾을 수 없습니다.") }
2626

27-
if (user.role == Role.STUDENT) {
27+
val existingUserLab = userLabRepository.findByUserIdAndLabId(userId, labId)
28+
29+
if(existingUserLab != null){
30+
throw IllegalStateException("이미 가입 중인 연구실입니다.")
31+
}
32+
33+
val existingRequest = requestRepository.findByUserIdAndLabId(userId, labId)
34+
35+
if (existingRequest == null) {
2836
val request = LabMembershipRequest(user = user, lab = lab)
2937
requestRepository.save(request)
3038
} else {
31-
throw IllegalStateException("학생만 요청 가능합니다.")
39+
throw IllegalStateException("이미 가입 요청이 있습니다: 사용자 ID = ${user.id}, 연구실 ID = ${lab.id}")
3240
}
3341
}
3442

0 commit comments

Comments
 (0)