Skip to content

Commit

Permalink
refactor: Feed 단건조회 반환값 변경
Browse files Browse the repository at this point in the history
- 이 부분 안드로이드랑 논의가 필요함

#822
  • Loading branch information
hong-sile committed Nov 6, 2023
1 parent fb3a7bb commit f1a7d6e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 98 deletions.
52 changes: 29 additions & 23 deletions backend/emm-sale/src/documentTest/java/com/emmsale/FeedApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.emmsale.feed.application.dto.FeedDetailResponse;
import com.emmsale.feed.application.dto.FeedDetailResponse.WriterProfileResponse;
import com.emmsale.feed.application.dto.FeedResponseRefactor;
import com.emmsale.feed.application.dto.FeedSimpleResponse;
import com.emmsale.feed.application.dto.FeedUpdateRequest;
Expand All @@ -37,6 +35,14 @@

class FeedApiTest extends MockMvcTestHelper {

private static final MemberReferenceResponse MEMBER_REFERENCE_RESPONSE = new MemberReferenceResponse(
2L,
"멤버",
"멤버 설명",
"멤버 이미지url",
"멤버 깃허브 url"
);

@Test
@DisplayName("이벤트의 피드 목록을 성공적으로 반환하면 200 OK를 반환한다.")
void findAllFeedsTest() throws Exception {
Expand All @@ -61,24 +67,17 @@ void findAllFeedsTest() throws Exception {
);

final long eventId = 11L;
final MemberReferenceResponse memberReferenceResponse = new MemberReferenceResponse(
2L,
"멤버",
"멤버 설명",
"멤버 이미지url",
"멤버 깃허브 url"
);
final List<FeedResponseRefactor> response = List.of(
new FeedResponseRefactor(
34L, eventId, "피드 1 제목", "피드 1 내용",
memberReferenceResponse,
MEMBER_REFERENCE_RESPONSE,
Collections.emptyList(),
2L,
LocalDateTime.of(2023, 7, 13, 0, 0), LocalDateTime.of(2023, 7, 13, 0, 0)
),
new FeedResponseRefactor(
35L, eventId, "피드 2 제목", "피드 2 내용",
memberReferenceResponse,
MEMBER_REFERENCE_RESPONSE,
Collections.emptyList(),
2L,
LocalDateTime.of(2023, 7, 13, 0, 0), LocalDateTime.of(2023, 7, 13, 0, 0)
Expand All @@ -101,26 +100,33 @@ void findDetailFeedTest() throws Exception {
//given
final ResponseFieldsSnippet responseFields = responseFields(
fieldWithPath("id").type(JsonFieldType.NUMBER).description("피드 id"),
fieldWithPath("eventId").type(JsonFieldType.NUMBER).description("이벤트 id"),
fieldWithPath("writer").type(JsonFieldType.OBJECT).description("작성자"),
fieldWithPath("writer.memberId").type(JsonFieldType.NUMBER).description("작성자 id"),
fieldWithPath("writer.name").type(JsonFieldType.STRING).description("작성자명"),
fieldWithPath("writer.imageUrl").type(JsonFieldType.STRING).description("작성자 이미지 url"),
fieldWithPath("eventId").type(JsonFieldType.NUMBER).description("피드가 작성된 부모 이벤트 id"),
fieldWithPath("title").type(JsonFieldType.STRING).description("피드 제목"),
fieldWithPath("content").type(JsonFieldType.STRING).description("피드 내용"),
fieldWithPath("images").type(JsonFieldType.ARRAY).description("피드 이미지 url 리스트"),
fieldWithPath("writer.id").type(JsonFieldType.NUMBER).description("writer의 식별자"),
fieldWithPath("writer.name").type(JsonFieldType.STRING).description("writer의 이름"),
fieldWithPath("writer.description").type(JsonFieldType.STRING)
.description("writer의 한줄 자기소개"),
fieldWithPath("writer.imageUrl").type(JsonFieldType.STRING)
.description("writer의 이미지 url"),
fieldWithPath("writer.githubUrl").type(JsonFieldType.STRING)
.description("writer의 github Url"),
fieldWithPath("commentCount").type(JsonFieldType.NUMBER).description("피드의 댓글 개수"),
fieldWithPath("createdAt").type(JsonFieldType.STRING).description("피드 생성 일시"),
fieldWithPath("updatedAt").type(JsonFieldType.STRING).description("피드 업데이트 일시")
);

final long eventId = 11L;
final long feedId = 34L;
final WriterProfileResponse writer = new WriterProfileResponse(8L, "작성자명",
"https://member-image.com");
final FeedDetailResponse response = new FeedDetailResponse(feedId, eventId, writer, "피드 제목",
"피드 상세 내용", List.of("https://image1.url", "https://image2.url"),
LocalDateTime.of(LocalDate.of(2023, 7, 22), LocalTime.of(23, 54, 49)),
LocalDateTime.of(LocalDate.of(2023, 7, 22), LocalTime.of(23, 54, 49)));

final FeedResponseRefactor response =
new FeedResponseRefactor(
34L, eventId, "피드 1 제목", "피드 1 내용",
MEMBER_REFERENCE_RESPONSE,
Collections.emptyList(),
2L,
LocalDateTime.of(2023, 7, 13, 0, 0), LocalDateTime.of(2023, 7, 13, 0, 0)
);

when(feedQueryService.findFeed(any(), any())).thenReturn(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<FeedResponseRefactor> findAllFeeds(
}

@GetMapping("/{id}")
public FeedDetailResponse findFeed(final Member member, @PathVariable final Long id) {
public FeedResponseRefactor findFeed(final Member member, @PathVariable final Long id) {
return feedQueryService.findFeed(member, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.emmsale.event.domain.repository.EventRepository;
import com.emmsale.event.exception.EventException;
import com.emmsale.event.exception.EventExceptionType;
import com.emmsale.feed.application.dto.FeedDetailResponse;
import com.emmsale.feed.application.dto.FeedResponseRefactor;
import com.emmsale.feed.application.dto.FeedSimpleResponse;
import com.emmsale.feed.domain.Feed;
Expand Down Expand Up @@ -42,30 +41,6 @@ public class FeedQueryService {
private final ImageRepository imageRepository;
private final CommentDao commentDao;

// public FeedListResponse findAllFeeds(final Member member, final Long eventId) {
// validateEvent(eventId);
//
// final List<Feed> feeds = feedRepository.findAllByEventIdAndNotDeleted(eventId);
// final List<Feed> filteredFeeds = excludeBlockedMembersFeed(member, feeds);
// final List<Long> feedIds = filteredFeeds.stream()
// .map(Feed::getId)
// .collect(Collectors.toList());
//
// final Map<Long, Long> feedCommentCounts = getFeedIdCommentCountMap(feedIds);
// final Map<Long, List<Image>> feedImages = getFeedImagesMap(feedIds);
//
// final List<FeedSimpleResponse> feedSimpleResponses = filteredFeeds.stream()
// .map(feed -> {
// final List<Image> images = feedImages.getOrDefault(feed.getId(), Collections.emptyList());
// final Long commentCount = feedCommentCounts.getOrDefault(feed.getId(),
// DEFAULT_COMMENT_COUNT);
// return FeedSimpleResponse.from(feed, images, commentCount);
// })
// .collect(Collectors.toList());
//
// return new FeedListResponse(eventId, feedSimpleResponses);
// }

public List<FeedResponseRefactor> findAllFeeds(final Member member, final Long eventId) {
validateEvent(eventId);

Expand All @@ -89,12 +64,13 @@ public List<FeedResponseRefactor> findAllFeeds(final Member member, final Long e
}

private Map<Long, List<Image>> getFeedImagesMap(final List<Long> feedIds) {
final Map<Long, List<Image>> feedImagesMap = imageRepository.findAllByFeedIdIn(feedIds).stream()
final Map<Long, List<Image>> feedImagesMap = imageRepository.findAllByFeedIdIn(feedIds)
.stream()
.collect(Collectors.groupingBy(
Image::getContentId,
Collectors.mapping(
Function.identity(),
Collectors.toUnmodifiableList()
Collectors.toList()
)
));

Expand Down Expand Up @@ -129,16 +105,16 @@ private List<Long> getBlockedMemberIds(final Member member) {
.collect(Collectors.toList());
}

public FeedDetailResponse findFeed(final Member member, final Long id) {
final Feed feed = feedRepository.findById(id)
.orElseThrow(() -> new FeedException(FeedExceptionType.NOT_FOUND_FEED));
public FeedResponseRefactor findFeed(final Member member, final Long id) {
final Feed feed = feedRepository.getByIdOrThrow(id);
final List<Image> images = imageRepository.findAllByFeedId(feed.getId());
images.sort(Comparator.comparing(Image::getOrder));

validateBlockedMemberFeed(member, feed);
validateDeletedFeed(feed);

return FeedDetailResponse.from(feed, images);
//이 부분은 안드분들과 이야기를 해봐야할 듯 실질적으로 쓰지 않는 값
return FeedResponseRefactor.of(feed, images, 0L);
}

private void validateBlockedMemberFeed(final Member member, final Feed feed) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class Feed extends BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.emmsale.feed.domain.repository;

import static com.emmsale.feed.exception.FeedExceptionType.NOT_FOUND_FEED;

import com.emmsale.feed.domain.Feed;
import com.emmsale.feed.exception.FeedException;
import com.emmsale.member.domain.Member;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -10,9 +13,17 @@
@Repository
public interface FeedRepository extends JpaRepository<Feed, Long> {

@Query("select f from Feed f where f.event.id = :eventId and f.isDeleted = false")
List<Feed> findAllByEventIdAndNotDeleted(Long eventId);
@Query("select f "
+ "from Feed f "
+ "join fetch f.writer w "
+ "where f.event.id = :eventId "
+ "and f.isDeleted = false")
List<Feed> findAllByEventIdAndNotDeleted(final Long eventId);

@Query("select f from Feed f where f.writer = :member and f.isDeleted = false")
List<Feed> findByMember(Member member);

default Feed getByIdOrThrow(final Long id) {
return findById(id).orElseThrow(() -> new FeedException(NOT_FOUND_FEED));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import com.emmsale.event.domain.repository.EventRepository;
import com.emmsale.event.exception.EventException;
import com.emmsale.event.exception.EventExceptionType;
import com.emmsale.feed.application.dto.FeedDetailResponse;
import com.emmsale.feed.application.dto.FeedListResponse;
import com.emmsale.feed.application.dto.FeedResponseRefactor;
import com.emmsale.feed.application.dto.FeedSimpleResponse;
import com.emmsale.feed.domain.Feed;
import com.emmsale.feed.domain.repository.FeedRepository;
Expand Down Expand Up @@ -73,15 +72,13 @@ void findAllFeedsTest() {
//given
final Long eventId = event.getId();

final List<FeedSimpleResponse> feedSimpleResponses = List.of(
FeedSimpleResponse.from(feed1, Collections.emptyList(), 0L),
FeedSimpleResponse.from(feed2, Collections.emptyList(), 0L)
final List<FeedResponseRefactor> expect = List.of(
FeedResponseRefactor.of(feed1, Collections.emptyList(), 0L),
FeedResponseRefactor.of(feed2, Collections.emptyList(), 0L)
);

final FeedListResponse expect = new FeedListResponse(eventId, feedSimpleResponses);

//when
final FeedListResponse actual = feedQueryService.findAllFeeds(writer, eventId);
final List<FeedResponseRefactor> actual = feedQueryService.findAllFeeds(writer, eventId);

//then
assertThat(actual)
Expand All @@ -97,13 +94,12 @@ void findAllFeedsWithWithDeletedFeedTest() {
feedRepository.save(feed1);

final Long eventId = event.getId();
final List<FeedSimpleResponse> feedSimpleResponses = List.of(
FeedSimpleResponse.from(feed2, Collections.emptyList(), 0L)
final List<FeedResponseRefactor> expect = List.of(
FeedResponseRefactor.of(feed2, Collections.emptyList(), 0L)
);
final FeedListResponse expect = new FeedListResponse(eventId, feedSimpleResponses);

//when
final FeedListResponse actual = feedQueryService.findAllFeeds(writer, eventId);
final List<FeedResponseRefactor> actual = feedQueryService.findAllFeeds(writer, eventId);

//then
assertThat(actual)
Expand Down Expand Up @@ -135,13 +131,12 @@ void findAllFeedsWithBlockedMember() {
final Feed feed3 = feedRepository.save(new Feed(event, reader, "피드3 제목", "피드3 내용"));
blockRepository.save(new Block(reader.getId(), writer.getId()));

final List<FeedSimpleResponse> feedSimpleResponses = List.of(
FeedSimpleResponse.from(feed3, Collections.emptyList(), 0L)
final List<FeedResponseRefactor> expect = List.of(
FeedResponseRefactor.of(feed3, Collections.emptyList(), 0L)
);
final FeedListResponse expect = new FeedListResponse(event.getId(), feedSimpleResponses);

//when
final FeedListResponse actual = feedQueryService.findAllFeeds(reader, event.getId());
final List<FeedResponseRefactor> actual = feedQueryService
.findAllFeeds(reader, event.getId());

//then
assertThat(actual)
Expand All @@ -155,11 +150,11 @@ void findAllFeedsWithNotExistFeed() {
//given
final Event noFeedEvent = eventRepository.save(EventFixture.구름톤());

final FeedListResponse expect = new FeedListResponse(noFeedEvent.getId(),
Collections.emptyList());
final List<FeedResponseRefactor> expect = Collections.emptyList();

//when
final FeedListResponse actual = feedQueryService.findAllFeeds(writer, noFeedEvent.getId());
final List<FeedResponseRefactor> actual
= feedQueryService.findAllFeeds(writer, noFeedEvent.getId());

//then
assertThat(actual)
Expand All @@ -179,10 +174,11 @@ void findFeedTest() {
final Feed feed = feed1;
final Long feedId = feed.getId();

final FeedDetailResponse expect = FeedDetailResponse.from(feed, Collections.emptyList());
final FeedResponseRefactor expect = FeedResponseRefactor.of(feed, Collections.emptyList(),
0L);

//when
final FeedDetailResponse actual = feedQueryService.findFeed(writer, feedId);
final FeedResponseRefactor actual = feedQueryService.findFeed(writer, feedId);

//then
assertThat(actual)
Expand Down Expand Up @@ -298,15 +294,13 @@ void findAllFeedsWithImages() {
//given
final Long eventId = event.getId();

final List<FeedSimpleResponse> feedSimpleResponses = List.of(
final List<FeedSimpleResponse> expect = List.of(
FeedSimpleResponse.from(feed1, images, 0L),
FeedSimpleResponse.from(feed2, Collections.emptyList(), 0L)
);

final FeedListResponse expect = new FeedListResponse(eventId, feedSimpleResponses);

//when
final FeedListResponse actual = feedQueryService.findAllFeeds(writer, eventId);
final List<FeedSimpleResponse> actual = feedQueryService.findAllMyFeeds(writer);

//then
assertThat(actual)
Expand All @@ -318,10 +312,10 @@ void findAllFeedsWithImages() {
@DisplayName("피드에 이미지가 있을 경우 피드 목록에서 이미지 리스트를 order순으로 정렬하여 함께 반환한다.")
void findDetailFeedWithImages() {
//given
final FeedDetailResponse expect = FeedDetailResponse.from(feed1, images);
final FeedResponseRefactor expect = FeedResponseRefactor.of(feed1, images, 0L);

//when
final FeedDetailResponse actual = feedQueryService.findFeed(writer, feed1.getId());
final FeedResponseRefactor actual = feedQueryService.findFeed(writer, feed1.getId());

//then
assertThat(actual)
Expand Down

0 comments on commit f1a7d6e

Please sign in to comment.