Skip to content

Commit

Permalink
refactor: Guest 전용 토픽 상세조회 DTO 정적 팩터리 메서드 정의
Browse files Browse the repository at this point in the history
  • Loading branch information
yoondgu committed Sep 12, 2023
1 parent 09d515c commit e7c12a0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TopicQueryService(

private static List<TopicDetailResponse> getGuestTopicDetailResponses(List<Topic> topics) {
return topics.stream()
.map(topic -> TopicDetailResponse.of(topic, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE))
.map(TopicDetailResponse::ofGuestQuery)
.toList();
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public TopicDetailResponse findDetailById(AuthMember authMember, Long topicId) {
validateReadableTopic(authMember, topic);

if (Objects.isNull(authMember.getMemberId())) {
return TopicDetailResponse.of(topic, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE);
return TopicDetailResponse.ofGuestQuery(topic);
}

Member member = findMemberById(authMember.getMemberId());
Expand Down Expand Up @@ -195,7 +195,7 @@ public List<TopicResponse> findAllTopicsByMemberId(AuthMember authMember, Long m
return topicRepository.findByCreatorId(memberId)
.stream()
.filter(authMember::canRead)
.map(topic -> TopicResponse.from(topic, Boolean.FALSE, Boolean.FALSE))
.map(TopicResponse::ofGuestQuery)
.toList();
}

Expand Down Expand Up @@ -248,11 +248,8 @@ private List<TopicResponse> getGuestNewestTopicResponse(AuthMember authMember) {
.map(Pin::getTopic)
.distinct()
.filter(authMember::canRead)
.map(topic -> TopicResponse.from(
topic,
Boolean.FALSE,
Boolean.FALSE
)).toList();
.map(TopicResponse::ofGuestQuery)
.toList();
}

public List<TopicResponse> findAllBestTopics(AuthMember authMember) {
Expand All @@ -267,11 +264,8 @@ private List<TopicResponse> getGuestBestTopicResponse(AuthMember authMember) {
.stream()
.filter(authMember::canRead)
.sorted(Comparator.comparing(Topic::countBookmarks).reversed())
.map(topic -> TopicResponse.from(
topic,
Boolean.FALSE,
Boolean.FALSE
)).toList();
.map(TopicResponse::ofGuestQuery)
.toList();
}

private List<TopicResponse> getUserBestTopicResponse(AuthMember authMember) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public record TopicDetailResponse(
LocalDateTime updatedAt,
List<PinResponse> pins
) {

public static TopicDetailResponse of(
Topic topic,
Boolean isInAtlas,
Expand Down Expand Up @@ -47,4 +48,27 @@ public static TopicDetailResponse of(
pinResponses
);
}

public static TopicDetailResponse ofGuestQuery(Topic topic) {
List<PinResponse> pinResponses = topic.getPins().stream()
.map(PinResponse::from)
.toList();

TopicInfo topicInfo = topic.getTopicInfo();

return new TopicDetailResponse(
topic.getId(),
topicInfo.getName(),
topicInfo.getDescription(),
topicInfo.getImageUrl(),
topic.getCreator().getMemberInfo().getNickName(),
topic.countPins(),
Boolean.FALSE,
topic.countBookmarks(),
Boolean.FALSE,
Boolean.FALSE,
topic.getUpdatedAt(),
pinResponses
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public record TopicResponse(
Boolean isBookmarked,
LocalDateTime updatedAt
) {

public static TopicResponse from(Topic topic, Boolean isInAtlas, Boolean isBookmarked) {
TopicInfo topicInfo = topic.getTopicInfo();

Expand All @@ -31,4 +32,20 @@ public static TopicResponse from(Topic topic, Boolean isInAtlas, Boolean isBookm
);
}

public static TopicResponse ofGuestQuery(Topic topic) {
TopicInfo topicInfo = topic.getTopicInfo();

return new TopicResponse(
topic.getId(),
topicInfo.getName(),
topicInfo.getImageUrl(),
topic.getCreator().getMemberInfo().getNickName(),
topic.countPins(),
Boolean.FALSE,
topic.countBookmarks(),
Boolean.FALSE,
topic.getUpdatedAt()
);
}

}

0 comments on commit e7c12a0

Please sign in to comment.