Skip to content

Commit

Permalink
Fix DIG-160: 카카오 소셜 친구목록 받아오기
Browse files Browse the repository at this point in the history
### 카카오 소셜 친구목록 받아오기
  • Loading branch information
Dongjin113 committed Nov 24, 2023
1 parent 3dfe9d0 commit bddd637
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ogjg.daitgym.user.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
@NoArgsConstructor
public class KaKaoFriendsRequest {

private List<KaKaoFriendsRequestDto> elements = new ArrayList<>();
private Integer total_count;

public KaKaoFriendsRequest(List<KaKaoFriendsRequestDto> elements) {
this.elements = elements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ogjg.daitgym.user.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
public class KaKaoFriendsRequestDto {

private Long id;
private String uuid;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ogjg.daitgym.user.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -9,15 +10,16 @@
@NoArgsConstructor(access = PROTECTED)
public class KaKaoFriendResponseDto {

private Long id;
private String uuid;
private String nickName;
private String profileImageURL;
private String imageUrl;
private String intro;
private int score;

public void putUserData(
String nickName, String profileImageURL
){
@Builder
public KaKaoFriendResponseDto(String nickName, String imageUrl, String intro, int score) {
this.nickName = nickName;
this.profileImageURL = profileImageURL;
this.imageUrl = imageUrl;
this.intro = intro;
this.score = score;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import java.util.ArrayList;
import java.util.List;

import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor
@NoArgsConstructor(access = PROTECTED)
public class KaKaoFriendsResponse {

private List<KaKaoFriendResponseDto> elements = new ArrayList<>();
private Integer total_count;

public KaKaoFriendsResponse(List<KaKaoFriendResponseDto> elements) {
this.elements = elements;
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/com/ogjg/daitgym/user/service/KakaoFriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.ogjg.daitgym.common.exception.user.ForbiddenKaKaoSocial;
import com.ogjg.daitgym.common.exception.user.NotFoundUser;
import com.ogjg.daitgym.common.exception.user.NotFoundUserAuthentication;
import com.ogjg.daitgym.domain.Inbody;
import com.ogjg.daitgym.domain.UserAuthentication;
import com.ogjg.daitgym.user.dto.request.KaKaoFriendsRequest;
import com.ogjg.daitgym.user.dto.response.KaKaoFriendResponseDto;
import com.ogjg.daitgym.user.dto.response.KaKaoFriendsResponse;
import com.ogjg.daitgym.user.repository.InbodyRepository;
import com.ogjg.daitgym.user.repository.UserAuthenticationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
Expand All @@ -13,16 +17,21 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class KakaoFriendService {

private final RestTemplate restTemplate;
private final UserAuthenticationRepository userAuthenticationRepository;
private final InbodyRepository inbodyRepository;

public KakaoFriendService(UserAuthenticationRepository userAuthenticationRepository) {
public KakaoFriendService(UserAuthenticationRepository userAuthenticationRepository, InbodyRepository inbodyRepository) {
this.restTemplate = new RestTemplate();
this.userAuthenticationRepository = userAuthenticationRepository;
this.inbodyRepository = inbodyRepository;
}


Expand All @@ -40,27 +49,35 @@ public KaKaoFriendsResponse requestKaKaoFriendsList(String email) {

HttpEntity<String> entity = new HttpEntity<>(headers);

ResponseEntity<KaKaoFriendsResponse> response = restTemplate.exchange(
ResponseEntity<KaKaoFriendsRequest> response = restTemplate.exchange(
"https://kapi.kakao.com/v1/api/talk/friends",
HttpMethod.GET,
entity,
KaKaoFriendsResponse.class
KaKaoFriendsRequest.class
);

KaKaoFriendsResponse kaKaoFriendsResponse = response.getBody();
KaKaoFriendsRequest kaKaoFriendsRequest = response.getBody();
List<KaKaoFriendResponseDto> responseDtoList = new ArrayList<>();

kaKaoFriendsResponse.getElements().forEach(
kaKaoFriendResponseDto -> {
UserAuthentication userAuthentication = userAuthenticationRepository.findByProviderId(kaKaoFriendResponseDto.getId())
kaKaoFriendsRequest.getElements().forEach(
kaKaoFriendsRequestDto -> {
UserAuthentication userAuthentication = userAuthenticationRepository.findByProviderId(kaKaoFriendsRequestDto.getId())
.orElseThrow(NotFoundUserAuthentication::new);

kaKaoFriendResponseDto.putUserData(
userAuthentication.getUser().getNickname(),
userAuthentication.getUser().getImageUrl()
responseDtoList.add(
new KaKaoFriendResponseDto(
userAuthentication.getUser().getNickname(),
userAuthentication.getUser().getImageUrl(),
userAuthentication.getUser().getIntroduction(),
inbodyRepository.findFirstByUserEmailOrderByCreatedAtDesc(
userAuthentication.getUser().getEmail())
.map(Inbody::getScore)
.orElse(0)
)
);
});

return kaKaoFriendsResponse;
return new KaKaoFriendsResponse(responseDtoList);
} catch (HttpClientErrorException.Forbidden e) {
log.error(e.getMessage());
throw new ForbiddenKaKaoSocial();
Expand Down

0 comments on commit bddd637

Please sign in to comment.