Skip to content

Commit

Permalink
feat: 유저의 친구 조회 기능을 개발한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Jan 3, 2024
1 parent efbcd30 commit 40cb649
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.teumteum.core.context.LoginContext;
import net.teumteum.core.error.ErrorResponse;
import net.teumteum.user.domain.request.UserUpdateRequest;
import net.teumteum.user.domain.response.FriendsResponse;
import net.teumteum.user.domain.response.UserGetResponse;
import net.teumteum.user.domain.response.UsersGetByIdResponse;
import net.teumteum.user.service.UserService;
Expand Down Expand Up @@ -56,6 +57,11 @@ public void addFriend(@PathVariable("friendId") Long friendId) {
userService.addFriends(loginContext.getUserId(), friendId);
}

@GetMapping("/{userId}/friends")
@ResponseStatus(HttpStatus.OK)
public FriendsResponse findFriends(@PathVariable("userId") Long userId) {
return userService.findFriendsByUserId(userId);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserRepository;
import net.teumteum.user.domain.request.UserUpdateRequest;
import net.teumteum.user.domain.response.FriendsResponse;
import net.teumteum.user.domain.response.UserGetResponse;
import net.teumteum.user.domain.response.UsersGetByIdResponse;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -52,6 +53,13 @@ public void addFriends(Long myId, Long friendId) {
me.addFriend(friend);
}

public FriendsResponse findFriendsByUserId(Long userId) {
var user = getUser(userId);
var friends = userRepository.findAllById(user.getFriends());

return FriendsResponse.of(friends);
}

private User getUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("userId에 해당하는 user를 찾을 수 없습니다. \"" + userId + "\""));
Expand Down

0 comments on commit 40cb649

Please sign in to comment.