Skip to content
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

[Feat]: 친구 리스트 조회 API 구현 #50

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum SuccessCode {

// friend
ADD_FRIEND_SUCCESS(HttpStatus.OK, "공유 요청에 성공했습니다."),
GET_FRIEND_LIST_SUCCESS(HttpStatus.OK, "친구 리스트 조회에 성공했습니다."),
HANDLE_FRIEND_REQUEST_SUCCESS(HttpStatus.OK, "공유 응답에 성공했습니다."),

// notice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.sobok.SobokSobok.friend.infrastructure.SendFriendRepository;
import io.sobok.SobokSobok.friend.ui.dto.AddFriendRequest;
import io.sobok.SobokSobok.friend.ui.dto.AddFriendResponse;
import io.sobok.SobokSobok.friend.ui.dto.FriendListResponse;
import io.sobok.SobokSobok.friend.ui.dto.HandleFriendRequest;
import io.sobok.SobokSobok.friend.ui.dto.HandleFriendRequestResponse;
import io.sobok.SobokSobok.notice.domain.Notice;
Expand All @@ -21,6 +22,8 @@
import io.sobok.SobokSobok.notice.infrastructure.NoticeQueryRepository;
import io.sobok.SobokSobok.notice.infrastructure.NoticeRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -80,6 +83,20 @@ public AddFriendResponse addFriend(Long userId, AddFriendRequest request) {
.build();
}

@Transactional(readOnly = true)
public List<FriendListResponse> getFriendList(Long userId) {
UserServiceUtil.existsUserById(userRepository, userId);

return friendRepository.findAllBySenderId(userId)
.stream().map(friend ->
FriendListResponse.builder()
.friendId(friend.getId())
.memberId(friend.getReceiverId())
.friendName(friend.getFriendName())
.build()
).collect(Collectors.toList());
}

@Transactional(noRollbackFor = {ConflictException.class})
public HandleFriendRequestResponse updateNoticeStatus(Long userId, Long noticeId,
HandleFriendRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.sobok.SobokSobok.friend.infrastructure;

import io.sobok.SobokSobok.friend.domain.Friend;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FriendRepository extends JpaRepository<Friend, Long> {

List<Friend> findAllBySenderId(Long senderId);

Integer countBySenderId(Long senderId);
}
19 changes: 19 additions & 0 deletions src/main/java/io/sobok/SobokSobok/friend/ui/FriendController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import io.sobok.SobokSobok.friend.application.FriendService;
import io.sobok.SobokSobok.friend.ui.dto.AddFriendRequest;
import io.sobok.SobokSobok.friend.ui.dto.AddFriendResponse;
import io.sobok.SobokSobok.friend.ui.dto.FriendListResponse;
import io.sobok.SobokSobok.friend.ui.dto.HandleFriendRequest;
import io.sobok.SobokSobok.friend.ui.dto.HandleFriendRequestResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand Down Expand Up @@ -64,4 +67,20 @@ public ResponseEntity<ApiResponse<HandleFriendRequestResponse>> handleFriendRequ
friendService.updateNoticeStatus(user.getId(), noticeId, request)
));
}

@GetMapping("")
@Operation(
summary = "친구 리스트 조회 API 메서드",
description = "친구 리스트를 조회하는 메서드입니다."
)
public ResponseEntity<ApiResponse<List<FriendListResponse>>> getFriendList(
@AuthenticationPrincipal User user
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(ApiResponse.success(
SuccessCode.GET_FRIEND_LIST_SUCCESS,
friendService.getFriendList(user.getId())
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.sobok.SobokSobok.friend.ui.dto;

import lombok.Builder;

@Builder
public record FriendListResponse(
Long friendId,
Long memberId,
String friendName
) {

}