-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[User] 친구 신청 조회 API 구현 #174
Changes from 11 commits
a001c3b
d41c13a
6e61306
8347daf
a2da747
1bbcbd0
db0117c
8640c9d
08effd9
419bf37
1763648
c3d218a
e4ee4fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package kpring.user.dto.response | ||
|
||
data class GetFriendRequestResponse( | ||
val friendId: Long, | ||
val username: String, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package kpring.user.dto.response | ||
|
||
data class GetFriendRequestsResponse( | ||
val userId: Long, | ||
var friendRequests: MutableList<GetFriendRequestResponse>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. immutable한 타입을 사용하는것은 어떻게 생각하시낭요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 가져온 친구 신청 목록 담는 용도라 immutable한 타입이 좋겠네요!! 해당 코드 수정하겠습니다~! |
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
package kpring.user.repository | ||
|
||
import kpring.user.entity.Friend | ||
import kpring.user.entity.FriendRequestStatus | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface FriendRepository : JpaRepository<Friend, Long> { | ||
fun existsByUserIdAndFriendId( | ||
userId: Long, | ||
friendId: Long, | ||
): Boolean | ||
|
||
fun findAllByUserIdAndRequestStatus( | ||
userId: Long, | ||
requestStatus: FriendRequestStatus, | ||
): List<Friend> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package kpring.user.service | ||
|
||
import kpring.core.global.exception.ServiceException | ||
import kpring.user.dto.response.DeleteFriendResponse | ||
import kpring.user.dto.response.GetFriendsResponse | ||
import kpring.user.dto.result.AddFriendResponse | ||
import kpring.user.dto.response.* | ||
import kpring.user.entity.Friend | ||
import kpring.user.entity.FriendRequestStatus | ||
import kpring.user.entity.User | ||
import kpring.user.exception.UserErrorCode | ||
import kpring.user.repository.FriendRepository | ||
|
@@ -16,6 +16,19 @@ class FriendServiceImpl( | |
private val userServiceImpl: UserServiceImpl, | ||
private val friendRepository: FriendRepository, | ||
) : FriendService { | ||
override fun getFriendRequests(userId: Long): GetFriendRequestsResponse { | ||
val friendRelations: List<Friend> = | ||
friendRepository.findAllByUserIdAndRequestStatus(userId, FriendRequestStatus.RECEIVED) | ||
val friendRequests: MutableList<GetFriendRequestResponse> = mutableListOf() | ||
|
||
for (friendRelation in friendRelations) { | ||
val friend = friendRelation.friend | ||
friendRequests.add(GetFriendRequestResponse(friend.id!!, friend.username)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stream api 사용하면 좀 더 가독성 좋은 코드가 될 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 stream api 사용해 코드 수정해보겠습니다~! |
||
|
||
return GetFriendRequestsResponse(userId, friendRequests) | ||
} | ||
|
||
override fun getFriends(userId: Long): GetFriendsResponse { | ||
TODO("Not yet implemented") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,12 @@ import kpring.core.auth.enums.TokenType | |
import kpring.core.global.dto.response.ApiResponse | ||
import kpring.core.global.exception.ServiceException | ||
import kpring.test.restdoc.dsl.restDoc | ||
import kpring.test.restdoc.json.JsonDataType | ||
import kpring.test.restdoc.json.JsonDataType.Strings | ||
import kpring.user.dto.response.AddFriendResponse | ||
import kpring.user.dto.response.FailMessageResponse | ||
import kpring.user.dto.result.AddFriendResponse | ||
import kpring.user.dto.response.GetFriendRequestResponse | ||
import kpring.user.dto.response.GetFriendRequestsResponse | ||
import kpring.user.exception.UserErrorCode | ||
import kpring.user.global.AuthValidator | ||
import kpring.user.global.CommonTest | ||
|
@@ -194,4 +197,151 @@ internal class FriendControllerTest( | |
} | ||
} | ||
} | ||
|
||
describe("친구신청 조회 API") { | ||
it("친구신청 조회 성공") { | ||
// given | ||
val friendRequest = | ||
GetFriendRequestResponse( | ||
friendId = CommonTest.TEST_FRIEND_ID, | ||
username = CommonTest.TEST_FRIEND_USERNAME, | ||
) | ||
val data = | ||
GetFriendRequestsResponse( | ||
userId = CommonTest.TEST_USER_ID, | ||
friendRequests = mutableListOf(friendRequest), | ||
) | ||
val response = ApiResponse(data = data) | ||
|
||
every { authClient.getTokenInfo(any()) }.returns( | ||
ApiResponse(data = TokenInfo(TokenType.ACCESS, CommonTest.TEST_USER_ID.toString())), | ||
) | ||
every { | ||
authValidator.checkIfAccessTokenAndGetUserId(any()) | ||
} returns CommonTest.TEST_USER_ID.toString() | ||
every { authValidator.checkIfUserIsSelf(any(), any()) } returns Unit | ||
every { | ||
friendService.getFriendRequests(CommonTest.TEST_USER_ID) | ||
} returns data | ||
|
||
// when | ||
val result = | ||
webTestClient.get() | ||
.uri( | ||
"/api/v1/user/{userId}/requests", | ||
CommonTest.TEST_USER_ID, | ||
) | ||
.header("Authorization", CommonTest.TEST_TOKEN) | ||
.exchange() | ||
|
||
// then | ||
val docsRoot = | ||
result | ||
.expectStatus().isOk | ||
.expectBody().json(objectMapper.writeValueAsString(response)) | ||
|
||
// docs | ||
docsRoot | ||
.restDoc( | ||
identifier = "getFriendRequests200", | ||
description = "친구신청 조회 API", | ||
) { | ||
request { | ||
path { | ||
"userId" mean "사용자 아이디" | ||
} | ||
header { "Authorization" mean "Bearer token" } | ||
} | ||
response { | ||
body { | ||
"data.userId" type Strings mean "사용자 아이디" | ||
"data.friendRequests" type JsonDataType.Arrays mean "친구신청한 사용자 리스트" | ||
"data.friendRequests[].friendId" type Strings mean "친구신청한 사용자 아이디" | ||
"data.friendRequests[].username" type Strings mean "친구신청한 사용자 닉네임" | ||
} | ||
Comment on lines
+258
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. frinedRequest는 무슨의미로 사용한 건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. friendReuqests는 로그인한 사용자가 받은 친구신청 목록을 의미합니다! |
||
} | ||
} | ||
} | ||
it("친구신청 조회 실패 : 권한이 없는 토큰") { | ||
// given | ||
val response = | ||
FailMessageResponse.builder().message(UserErrorCode.NOT_ALLOWED.message()).build() | ||
every { authClient.getTokenInfo(any()) } throws ServiceException(UserErrorCode.NOT_ALLOWED) | ||
|
||
// when | ||
val result = | ||
webTestClient.get() | ||
.uri( | ||
"/api/v1/user/{userId}/requests", | ||
CommonTest.TEST_USER_ID, | ||
) | ||
.header("Authorization", CommonTest.TEST_TOKEN) | ||
.exchange() | ||
|
||
// then | ||
val docsRoot = | ||
result | ||
.expectStatus().isForbidden | ||
.expectBody().json(objectMapper.writeValueAsString(response)) | ||
|
||
// docs | ||
docsRoot | ||
.restDoc( | ||
identifier = "getFriendRequests403", | ||
description = "친구신청 조회 API", | ||
) { | ||
request { | ||
path { | ||
"userId" mean "사용자 아이디" | ||
} | ||
header { "Authorization" mean "Bearer token" } | ||
} | ||
response { | ||
body { | ||
"message" type Strings mean "에러 메시지" | ||
} | ||
} | ||
} | ||
} | ||
it("친구신청 조회 실패 : 서버 내부 오류") { | ||
// given | ||
val response = FailMessageResponse.serverError | ||
every { authClient.getTokenInfo(any()) } throws RuntimeException("서버 내부 오류") | ||
|
||
// when | ||
val result = | ||
webTestClient.get() | ||
.uri( | ||
"/api/v1/user/{userId}/requests", | ||
CommonTest.TEST_USER_ID, | ||
) | ||
.header("Authorization", CommonTest.TEST_TOKEN) | ||
.exchange() | ||
|
||
// then | ||
val docsRoot = | ||
result | ||
.expectStatus().isEqualTo(500) | ||
.expectBody().json(objectMapper.writeValueAsString(response)) | ||
|
||
// docs | ||
docsRoot | ||
.restDoc( | ||
identifier = "getFriendRequests500", | ||
description = "친구신청 조회 API", | ||
) { | ||
request { | ||
path { | ||
"userId" mean "사용자 아이디" | ||
} | ||
header { "Authorization" mean "Bearer token" } | ||
} | ||
response { | ||
body { | ||
"message" type Strings mean "에러 메시지" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 토큰에서 뽑을 수 있는 userId랑은 다른 정보일까요?! 제가 생각하기에는 요청하는 user의 id를 토큰에서 뽑아와서 사용하면 될 것 같은데..! 입력을 따로 받는 이유가 있나요?! 궁금합니다 😽
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
토큰에서 뽑을 수 있는 userId랑 같은 정보입니다! 기존에 작성되어 있던 코드들이 pathVariable로 userId를 받아와서 동일하게 코드 작성했었는데, user쪽 기능 구현 다 하면 수정해봐도 좋을 것 같아요! 추후에 수정하겠습니다~!