Skip to content

Commit

Permalink
[refactor] 불변성 보장으로 변경 (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Nov 19, 2024
1 parent 9660f7b commit e15328b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
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 Down Expand Up @@ -219,23 +220,23 @@ public List<EmploymentPostResponse> getPlaygroundEmploymentPostWithMemberInfo(St
return getPostsWithMemberInfo(playgroundToken, employmentPosts);
}

private <T extends PostWithMemberInfo> void addMemberInfoToPost(T post, PlayGroundPostDetailResponse postDetail) {
private <T extends PostWithMemberInfo> T addMemberInfoToPost(T post, PlayGroundPostDetailResponse postDetail) {
if (postDetail.member() != null) {
post.setProfileImage(postDetail.member().profileImage());
post.setName(postDetail.member().name());
return (T) post.withMemberDetail(postDetail.member().name(), postDetail.member().profileImage());
} else if (postDetail.anonymousProfile() != null) {
post.setProfileImage(postDetail.anonymousProfile().profileImgUrl());
post.setName(postDetail.anonymousProfile().nickname());
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);
addMemberInfoToPost(post, postDetail);
mutablePosts.add(addMemberInfoToPost(post, postDetail));
}
return posts;
return mutablePosts;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sopt.app.application.playground.dto;

public interface PostWithMemberInfo {
void setProfileImage(String profileImage);
void setName(String name);
Object withMemberDetail(String name, String profileImage);
Long getId();
}
Original file line number Diff line number Diff line change
@@ -1,21 +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
@AllArgsConstructor
@NoArgsConstructor
public class EmploymentPostResponse implements PostWithMemberInfo {
private final Long id;
private final String categoryName;
private Long id;
private String categoryName;
private String profileImage;
private String name;
private final String title;
private final String content;
private final List<String> images;
private String title;
private String content;
private List<String> images;

public static EmploymentPostResponse of(EmploymentPost employmentPost) {
return EmploymentPostResponse.builder()
Expand All @@ -27,13 +31,15 @@ public static EmploymentPostResponse of(EmploymentPost employmentPost) {
.build();
}

@Override
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}

@Override
public void setName(String name) {
this.name = name;
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 @@ -4,24 +4,23 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.sopt.app.application.playground.dto.PlaygroundPostInfo.PlaygroundPostResponse;
import org.sopt.app.application.playground.dto.PostWithMemberInfo;

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



public static RecentPostsResponse of(PlaygroundPostResponse playgroundPostResponse) {
return RecentPostsResponse.builder()
.id(playgroundPostResponse.postId())
Expand All @@ -32,13 +31,16 @@ public static RecentPostsResponse of(PlaygroundPostResponse playgroundPostRespon
.build();
}

@Override
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
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();
}

@Override
public void setName(String name) {
this.name = name;
}
}

0 comments on commit e15328b

Please sign in to comment.