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

[REFACTOR] 플레이그라운드 게시물 반환 기능 + 채용 정보 조회 사용자 정보 추가 - #434 #453

Merged
merged 8 commits into from
Nov 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.sopt.app.application.playground.PlaygroundHeaderCreator.createDefaultHeader;

import io.jsonwebtoken.ExpiredJwtException;
import jakarta.persistence.EntityNotFoundException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -19,6 +20,7 @@
import org.sopt.app.application.auth.dto.PlaygroundAuthTokenInfo.RefreshedToken;
import org.sopt.app.application.playground.dto.PlayGroundEmploymentResponse;
import org.sopt.app.application.playground.dto.PlayGroundPostCategory;
import org.sopt.app.application.playground.dto.PlayGroundPostDetailResponse;
import org.sopt.app.application.playground.dto.PlaygroundPostInfo.PlaygroundPost;
import org.sopt.app.application.playground.dto.PlaygroundPostInfo.PlaygroundPostResponse;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.ActiveUserIds;
Expand All @@ -29,6 +31,7 @@
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.PlaygroundMain;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.PlaygroundProfile;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.UserActiveInfo;
import org.sopt.app.application.playground.dto.PostWithMemberInfo;
import org.sopt.app.common.exception.BadRequestException;
import org.sopt.app.common.exception.UnauthorizedException;
import org.sopt.app.common.response.ErrorCode;
Expand Down Expand Up @@ -199,12 +202,41 @@ public List<RecentPostsResponse> getRecentPosts(String playgroundToken) {
.join();
}
}

public List<RecentPostsResponse> getRecentPostsWithMemberInfo(String playgroundToken) {
List<RecentPostsResponse> recentPosts = getRecentPosts(playgroundToken);
return getPostsWithMemberInfo(playgroundToken, recentPosts);
}

public List<EmploymentPostResponse> getPlaygroundEmploymentPost(String accessToken) {
Map<String, String> requestHeader = createAuthorizationHeaderByUserPlaygroundToken(accessToken);
PlayGroundEmploymentResponse postInfo = playgroundClient.getPlaygroundEmploymentPost(requestHeader,16,10,0);
return postInfo.posts().stream()
.map(EmploymentPostResponse::of)
.collect(Collectors.toList());
.map(EmploymentPostResponse::of).toList();
}

public List<EmploymentPostResponse> getPlaygroundEmploymentPostWithMemberInfo(String playgroundToken) {
List<EmploymentPostResponse> employmentPosts = getPlaygroundEmploymentPost(playgroundToken);
return getPostsWithMemberInfo(playgroundToken, employmentPosts);
}

private <T extends PostWithMemberInfo> T addMemberInfoToPost(T post, PlayGroundPostDetailResponse postDetail) {
if (postDetail.member() != null) {
return (T) post.withMemberDetail(postDetail.member().name(), postDetail.member().profileImage());
} else if (postDetail.anonymousProfile() != null) {
return (T) post.withMemberDetail(postDetail.anonymousProfile().nickname(), postDetail.anonymousProfile().profileImgUrl());
}
throw new EntityNotFoundException("Member not found");
}

private <T extends PostWithMemberInfo> List<T> getPostsWithMemberInfo(String playgroundToken, List<T> posts) {
final Map<String, String> accessToken = createAuthorizationHeaderByUserPlaygroundToken(playgroundToken);
List<T> mutablePosts = new ArrayList<>();
for (T post : posts) {
Long postId = post.getId();
PlayGroundPostDetailResponse postDetail = playgroundClient.getPlayGroundPostDetail(accessToken, postId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 플그에게서 채용탭 post를 모두 받고, 그 포스트를 이용해서 하나씩 다시 postDetail을 받는다는 로직이 너무 비용이 크다고 생각해요. 한 번에 채용탭과 함께 받아오기는 어려운걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존의 로직대로 하면 한번에 받아올수있는데, username과 프로필 이미지가 추가되어서 다시받아오는걸로 결정했습니다! 필드 변경에 띠라서 API 재요청을 드리는것보다 후에 이러한 작업을 하나로 묶는것이 일단 마감 Date때문에 이렇게 결정했습니다!

mutablePosts.add(addMemberInfoToPost(post, postDetail));
}
return mutablePosts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import org.sopt.app.application.auth.dto.PlaygroundAuthTokenInfo.RefreshedToken;
import org.sopt.app.application.playground.dto.PlayGroundEmploymentResponse;
import org.sopt.app.application.playground.dto.PlayGroundPostDetailResponse;
import org.sopt.app.application.playground.dto.PlaygroundPostInfo.PlaygroundPostResponse;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.ActiveUserIds;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.OwnPlaygroundProfile;
Expand Down Expand Up @@ -61,4 +62,8 @@ PlayGroundEmploymentResponse getPlaygroundEmploymentPost(@HeaderMap Map<String,
@Param int categoryId,
@Param int limit,
@Param int cursor);

@RequestLine("GET /api/v1/community/posts/{postId}")
PlayGroundPostDetailResponse getPlayGroundPostDetail(@HeaderMap Map<String, String> headers,
@Param Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.sopt.app.application.playground.dto;

public record PlayGroundPostDetailResponse(
Member member, AnonymousProfile anonymousProfile
) {
public record Member(
String name,
String profileImage
) {
}

public record AnonymousProfile(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q. 이름이 AnonymousProfile인 이유가 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인해보니 포스트의 프로필을 설정하지 않고 null인 유저가 있었습니다.
유저가 null인경우 플그쪽에서 해당이름으로 유저를 보여주지않는 방식으로 내려주어 해당이름으로 정했습니다!

String nickname,
String profileImgUrl
) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.sopt.app.application.playground.dto;

public interface PostWithMemberInfo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. PostWithMemberInfo interface를 override하는 방법이 저로서는 현재 이점이 명확하지 않은 것 같아요!
어떤 이점이 있다 판단하여 PostWithMemberInfo라는 interface를 도입한 것인지 궁금해요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최근 게시물 반환, 채용탭 10개 반환에서 똑같은 로직으로 멤버의 이름과 프로필 이미지를 받아와 추가하는데
같은 메소드를 사용하고 명시적으로 다른 타입의 같은 메소드를 사용할때 어떤 것을 추가해야 할지 명시적으로 결정해주기 위해 다음과 같이 정했습니다!

Object withMemberDetail(String name, String profileImage);
Long getId();
}
11 changes: 2 additions & 9 deletions src/main/java/org/sopt/app/facade/HomeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,11 @@ private boolean isServiceVisibleToUser(AppServiceInfo appServiceInfo, User user)
}

public List<RecentPostsResponse> getRecentPosts(User user) {
return playgroundAuthService.getRecentPosts(user.getPlaygroundToken()).stream()
.map(post -> RecentPostsResponse.builder()
.id(post.getId())
.title(post.getTitle())
.category(post.getCategory())
.isHotPost(post.isHotPost())
.build()
).toList();
return playgroundAuthService.getRecentPostsWithMemberInfo(user.getPlaygroundToken());
}

public List<EmploymentPostResponse> getHomeEmploymentPost(User user) {
return playgroundAuthService.getPlaygroundEmploymentPost(user.getPlaygroundToken());
return playgroundAuthService.getPlaygroundEmploymentPostWithMemberInfo(user.getPlaygroundToken());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ResponseEntity<List<AppServiceEntryStatusResponse>> getAppService(
}


@Operation(summary = "최근 채용탭 10개 조회")
@Operation(summary = "최근 게시물 카테고리별 조회")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "401", description = "token error", content = @Content),
Expand All @@ -70,6 +70,7 @@ public ResponseEntity<List<RecentPostsResponse>> getRecentPost(
homeFacade.getRecentPosts(user));
}

@Operation(summary = "최근 채용탭 10개 조회")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "401", description = "token error", content = @Content),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package org.sopt.app.presentation.home.response;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.sopt.app.application.playground.dto.PlayGroundEmploymentResponse.EmploymentPost;
import org.sopt.app.application.playground.dto.PostWithMemberInfo;

@Getter
@Builder
public class EmploymentPostResponse {
private final Long id;
private final String categoryName;
private final String title;
private final String content;
private final List<String> images;
@AllArgsConstructor
@NoArgsConstructor
public class EmploymentPostResponse implements PostWithMemberInfo {
private Long id;
private String categoryName;
private String profileImage;
private String name;
private String title;
private String content;
private List<String> images;

public static EmploymentPostResponse of(EmploymentPost employmentPost) {
return EmploymentPostResponse.builder()
Expand All @@ -23,4 +30,16 @@ public static EmploymentPostResponse of(EmploymentPost employmentPost) {
.images(employmentPost.images())
.build();
}

public EmploymentPostResponse withMemberDetail(String name, String profileImage) {
return EmploymentPostResponse.builder()
.id(this.id)
.categoryName(this.categoryName)
.name(name)
.profileImage(profileImage)
.title(this.title)
.content(this.content)
.images(this.images)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.sopt.app.application.playground.dto.PlaygroundPostInfo.PlaygroundPostResponse;
import org.sopt.app.application.playground.dto.PostWithMemberInfo;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RecentPostsResponse {
@Getter
@NoArgsConstructor
public class RecentPostsResponse implements PostWithMemberInfo {
private Long id;
private String title;
private String profileImage;
private String name;
private String category;
private String content;
private boolean isHotPost;

private Boolean isHotPost;


public static RecentPostsResponse of(PlaygroundPostResponse playgroundPostResponse) {
return RecentPostsResponse.builder()
.id(playgroundPostResponse.postId())
Expand All @@ -26,4 +30,17 @@ public static RecentPostsResponse of(PlaygroundPostResponse playgroundPostRespon
.isHotPost(true)
.build();
}

public RecentPostsResponse withMemberDetail(String name, String profileImage) {
return RecentPostsResponse.builder()
.id(this.id)
.title(this.title)
.profileImage(profileImage)
.name(name)
.category(this.category)
.content(this.content)
.isHotPost(this.isHotPost)
.build();
}

}
Loading