Skip to content

Commit

Permalink
[BE] Refactor/#390 지도 및 핀 상세 조회 API에 수정 권한 여부 필드 추가 (#392)
Browse files Browse the repository at this point in the history
* feat: 토픽 상세 조회 내용에 수정 권한 추가

* refactor: 토픽 상세 조회 DTO 정적 팩터리 메서드명 수정

* feat: 핀 상세 조회 내용에 수정 권한 추가

* fix: 토픽 RestDocs 깨지는 문제 수정, API 목차 순서 조정

- 목록 조회 API 기준으로 순서 조정, API 네이밍 보완

* refactor: 토픽, 핀 수정 권한 여부 필드명 직관적으로 수정

- hasUpdatePermission -> canUpdate

* refactor: Guest 전용 토픽 상세조회 DTO 정적 팩터리 메서드 정의

* refactor: 메서드 순서 정리, Guest 전용 토픽 조회 DTO 정적 팩터리 메서드 정의

* refactor: Guest 전용 토픽 List 조회 DTO 정적 팩터리 메서드 정의
  • Loading branch information
yoondgu authored Sep 12, 2023
1 parent 8c90435 commit 8e56ec8
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 57 deletions.
23 changes: 13 additions & 10 deletions backend/src/docs/asciidoc/topic.adoc
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
== 토픽

=== 토픽 목록 조회
=== 토픽 전체 목록 조회

operation::topic-controller-test/find-all[snippets='http-request,http-response']

=== 토픽 인기 목록 조회
=== 최신 토픽 목록 조회

operation::location-controller-test/find-nearby-topics-sorted-by-pin-count[snippets='http-request,http-response']
operation::topic-controller-test/find-all-by-order-by-updated-at-desc[snippets='http-request,http-response']

=== 인기 급상승 토픽 목록 조회 (즐겨찾기 기준)

operation::topic-controller-test/find-all-best-topics[snippets='http-request,http-response']

=== 멤버별 토픽 목록 조회

operation::topic-controller-test/find-all-topics-by-member-id[snippets='http-request,http-response']

=== 주변 인기 토픽 목록 조회 (핀 개수 기준)

operation::location-controller-test/find-nearby-topics-sorted-by-pin-count[snippets='http-request,http-response']

=== 토픽 상세 조회

operation::topic-controller-test/find-by-id[snippets='http-request,http-response']

=== 최신 토픽 목록 조회
operation::topic-controller-test/find-all-by-order-by-updated-at-desc[snippets='http-request,http-response']

=== 토픽 생성

operation::topic-controller-test/create[snippets='http-request,http-response']
Expand All @@ -27,7 +32,8 @@ operation::topic-controller-test/create[snippets='http-request,http-response']

operation::topic-controller-test/merge-and-create[snippets='http-request,http-response']

== 토픽에 핀 추가
=== 토픽에 핀 추가

operation::topic-controller-test/copy-pin[snippets='http-request,http-response']

=== 토픽 수정
Expand All @@ -38,6 +44,3 @@ operation::topic-controller-test/update[snippets='http-request,http-response']

operation::topic-controller-test/delete[snippets='http-request,http-response']

=== 인기 급상승 토픽 조회

operation::topic-controller-test/find-all-best-topics[snippets='http-request,http-response']
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PinDetailResponse findDetailById(AuthMember member, Long pinId) {
.orElseThrow(() -> new PinNotFoundException(PIN_NOT_FOUND, pinId));
validateReadAuth(member, pin.getTopic());

return PinDetailResponse.from(pin);
return PinDetailResponse.of(pin, member.canPinCreateOrUpdate(pin.getTopic()));
}

private void validateReadAuth(AuthMember member, Topic topic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public record PinDetailResponse(
String creator,
double latitude,
double longitude,
Boolean canUpdate,
LocalDateTime updatedAt,
List<PinImageResponse> images
) {

public static PinDetailResponse from(Pin pin) {
public static PinDetailResponse of(Pin pin, Boolean canUpdate) {
PinInfo pinInfo = pin.getPinInfo();

return new PinDetailResponse(
Expand All @@ -28,6 +29,7 @@ public static PinDetailResponse from(Pin pin) {
pin.getCreator().getMemberInfo().getNickName(),
pin.getLatitude(),
pin.getLongitude(),
canUpdate,
pin.getUpdatedAt(),
PinImageResponse.from(pin.getPinImages())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private List<TopicResponse> getGuestTopicResponses(AuthMember authMember) {
return topicRepository.findAll()
.stream()
.filter(authMember::canRead)
.map(topic -> TopicResponse.from(topic, Boolean.FALSE, Boolean.FALSE))
.map(TopicResponse::fromGuestQuery)
.toList();
}

Expand Down Expand Up @@ -99,24 +99,24 @@ private boolean isBookMarked(List<Topic> bookMarkedTopics, Topic topic) {
return bookMarkedTopics.contains(topic);
}


public TopicDetailResponse findDetailById(AuthMember authMember, Long topicId) {
Topic topic = findTopic(topicId);
validateReadableTopic(authMember, topic);

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

Member member = findMemberById(authMember.getMemberId());

List<Topic> topicsInAtlas = findTopicsInAtlas(member);
List<Topic> topicsInBookMark = findBookMarkedTopics(member);

return TopicDetailResponse.from(
return TopicDetailResponse.of(
topic,
isInAtlas(topicsInAtlas, topic),
isBookMarked(topicsInBookMark, topic)
isBookMarked(topicsInBookMark, topic),
authMember.canTopicUpdate(topic)
);
}

Expand All @@ -140,29 +140,24 @@ public List<TopicDetailResponse> findDetailsByIds(AuthMember authMember, List<Lo
validateReadableTopics(authMember, topics);

if (Objects.isNull(authMember.getMemberId())) {
return getGuestTopicDetailResponses(topics);
return TopicDetailResponse.ofGuestQueries(topics);
}

return getUserTopicDetailResponses(authMember, topics);
}

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

private List<TopicDetailResponse> getUserTopicDetailResponses(AuthMember authMember, List<Topic> topics) {
Member member = findMemberById(authMember.getMemberId());

List<Topic> topicsInAtlas = findTopicsInAtlas(member);
List<Topic> topicsInBookMark = findBookMarkedTopics(member);

return topics.stream()
.map(topic -> TopicDetailResponse.from(
.map(topic -> TopicDetailResponse.of(
topic,
isInAtlas(topicsInAtlas, topic),
isBookMarked(topicsInBookMark, topic)
isBookMarked(topicsInBookMark, topic),
authMember.canTopicUpdate(topic)
))
.toList();
}
Expand Down Expand Up @@ -194,7 +189,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::fromGuestQuery)
.toList();
}

Expand Down Expand Up @@ -247,11 +242,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::fromGuestQuery)
.toList();
}

public List<TopicResponse> findAllBestTopics(AuthMember authMember) {
Expand All @@ -266,11 +258,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::fromGuestQuery)
.toList();
}

private List<TopicResponse> getUserBestTopicResponse(AuthMember authMember) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ public record TopicDetailResponse(
Boolean isInAtlas,
Integer bookmarkCount,
Boolean isBookmarked,
Boolean canUpdate,
LocalDateTime updatedAt,
List<PinResponse> pins
) {
public static TopicDetailResponse from(Topic topic, Boolean isInAtlas, Boolean isBookmarked) {

public static TopicDetailResponse of(
Topic topic,
Boolean isInAtlas,
Boolean isBookmarked,
Boolean canUpdate
) {
List<PinResponse> pinResponses = topic.getPins().stream()
.map(PinResponse::from)
.toList();
Expand All @@ -36,8 +43,39 @@ public static TopicDetailResponse from(Topic topic, Boolean isInAtlas, Boolean i
isInAtlas,
topic.countBookmarks(),
isBookmarked,
canUpdate,
topic.getUpdatedAt(),
pinResponses
);
}

public static TopicDetailResponse fromGuestQuery(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
);
}

public static List<TopicDetailResponse> ofGuestQueries(List<Topic> topics) {

return topics.stream()
.map(TopicDetailResponse::fromGuestQuery)
.toList();
}
}
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 fromGuestQuery(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()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void setUp() {

@Test
@DisplayName("현재 위치를 기준 토픽의 핀 개수로 나열한다.")
void findNearbyTopicsSortedByPinCount_Success() throws Exception {
void findNearbyTopicsSortedByPinCount() throws Exception {
//given
double latitude = 37;
double longitude = 127;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void saveIfExistLocation_Success() {
assertThat(savedLocation.getId()).isEqualTo(location.getId());
assertThat(actual).usingRecursiveComparison()
.ignoringFields("updatedAt")
.isEqualTo(PinDetailResponse.from(pin));
.isEqualTo(PinDetailResponse.of(pin, Boolean.TRUE));
}

@Test
Expand Down Expand Up @@ -124,7 +124,7 @@ void saveIfNotExistLocation_Success() {
assertThat(savedLocation.getId()).isNotEqualTo(location.getId());
assertThat(actual).usingRecursiveComparison()
.ignoringFields("updatedAt")
.isEqualTo(PinDetailResponse.from(pin));
.isEqualTo(PinDetailResponse.of(pin, Boolean.TRUE));

List<Location> locations = locationRepository.findAll();
assertThat(locations).hasSize(2);
Expand Down Expand Up @@ -224,10 +224,10 @@ void removeImageById_Success() {

// then
pinImageRepository.findById(pinImageId)
.ifPresentOrElse(
found -> assertThat(found.isDeleted()).isTrue(),
Assertions::fail
);
.ifPresentOrElse(
found -> assertThat(found.isDeleted()).isTrue(),
Assertions::fail
);
}

private long savePinImageAndGetId(long pinId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void findDetailById_Success() {
// then
assertThat(actual).usingRecursiveComparison()
.ignoringFields("updatedAt")
.isEqualTo(PinDetailResponse.from(pin));
.isEqualTo(PinDetailResponse.of(pin, Boolean.TRUE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void findById() throws Exception {
"매튜",
37,
127,
Boolean.FALSE,
LocalDateTime.now(),
List.of(new PinImageResponse(1L, BASE_IMAGES.get(0)))
);
Expand Down
Loading

0 comments on commit 8e56ec8

Please sign in to comment.