Skip to content

Commit

Permalink
feat: 이미 조회한 유저 다시 조회 못하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
GGHDMS committed Dec 19, 2023
1 parent 6c04aec commit 068fb6f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.yourssu.ssudateserver.exception

import com.yourssu.ssudateserver.exception.logic.AllCanNotRegisterException
import com.yourssu.ssudateserver.exception.logic.DuplicateCodeException
import com.yourssu.ssudateserver.exception.logic.DuplicateFollowException
import com.yourssu.ssudateserver.exception.logic.InvalidRefreshTokenException
import com.yourssu.ssudateserver.exception.logic.NickNameDuplicateException
import com.yourssu.ssudateserver.exception.logic.RefreshTokenNotFoundException
Expand Down Expand Up @@ -89,10 +90,19 @@ class CommonExceptionHandler {

@ExceptionHandler(WebClientException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleDuplicateCodeException(
fun handleWebClientException(
exception: WebClientException,
request: HttpServletRequest,
): ErrorResponse {
return ErrorResponse(LocalDateTime.now(), HttpStatus.BAD_REQUEST, exception.message!!, request.requestURI)
}

@ExceptionHandler(DuplicateFollowException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleDuplicateFollowException(
exception: DuplicateFollowException,
request: HttpServletRequest,
): ErrorResponse {
return ErrorResponse(LocalDateTime.now(), HttpStatus.BAD_REQUEST, exception.message!!, request.requestURI)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.yourssu.ssudateserver.exception.logic

class DuplicateFollowException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import org.springframework.data.jpa.repository.JpaRepository
interface FollowRepository : JpaRepository<Follow, Long> {

fun findAllByFromUserId(fromUserId: Long): List<Follow>

fun findByFromUserIdAndToUserId(fromUserId: Long, toUserId: Long): Follow?
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.yourssu.ssudateserver.dto.response.SearchContactResponseDto
import com.yourssu.ssudateserver.dto.response.SearchResponseDto
import com.yourssu.ssudateserver.enums.Animals
import com.yourssu.ssudateserver.enums.Gender
import com.yourssu.ssudateserver.exception.logic.DuplicateFollowException
import com.yourssu.ssudateserver.exception.logic.SelfContactException
import com.yourssu.ssudateserver.exception.logic.UserNotFoundException
import com.yourssu.ssudateserver.repository.FollowRepository
Expand Down Expand Up @@ -92,11 +93,15 @@ class SSUDateService(
userRepository.findByOauthName(oauthName) ?: throw UserNotFoundException("해당 oauthName인 유저가 없습니다.")

if (fromUser.nickName == nickName) {
throw SelfContactException("본인의 nickName으로 Contact할 수 없습니다.")
throw SelfContactException("본인의 nickName으로 조회할 수 없습니다.")
}

val toUser = userRepository.findByNickName(nickName) ?: throw UserNotFoundException("nickName인 유저가 없습니다.")

followRepository.findByFromUserIdAndToUserId(fromUserId = fromUser.id!!, toUserId = toUser.id!!)?.run {
throw DuplicateFollowException("이미 조회한 유저입니다.")
}

followRepository.save(fromUser.contactTo(toUser))

return ContactResponseDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,33 @@ class ContactTest : BaseTest() {
nickName = "testNick1",
)

val user = userRepository.findByNickName("testNick1")!!
val test = mockMvc.post("/contact") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(requestDto)
}
test.andExpect {
jsonPath("message") { value("본인의 nickName으로 조회할 수 없습니다.") }
}
test.andDo {
print()
}
}

@Test
fun contactTestFailDuplicateContact() {
setPrincipal("oauthName2")

val requestDto = ContactRequestDto(
nickName = "testNick3",
)

val test = mockMvc.post("/contact") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(requestDto)
}

test.andExpect {
jsonPath("message") { value("본인의 nickName으로 Contact할 수 없습니다.") }
jsonPath("message") { value("이미 조회한 유저입니다.") }
}
test.andDo {
print()
Expand Down

0 comments on commit 068fb6f

Please sign in to comment.