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/#803 행사 및 함께해요 관련 api 명세 수정 #808

Conversation

amaran-th
Copy link
Collaborator

#️⃣ 연관된 이슈

close : #803

📝 작업 내용

행사 및 함께해요 조회 API 명세 수정 및 자잘한 변경사항 반영

  • API 명세 순서 수정
  • Event Service 리팩토링

예상 소요 시간 및 실제 소요 시간 (일 / 시간 / 분)

예상 소요 시간 : 10/30
실제 소요 시간 : 10/30

amaran-th added 4 commits October 30, 2023 19:28
- EventService를 Command와 Query로 분리
- AllImagesOfContent 일급컬렉션 구현

#803
- MemberReferenceResponse dto를 포함하도록 구현

#803
@amaran-th amaran-th added Backend 백엔드 관련 이슈 리팩터링 테스트 코드의 검증 값이 변환하지 않고 코드 변경 High Priority 리뷰 우선순위가 높은 PR labels Oct 30, 2023
@amaran-th amaran-th added this to the 레벨 5 milestone Oct 30, 2023
@amaran-th amaran-th self-assigned this Oct 30, 2023
Copy link
Collaborator

@hong-sile hong-sile left a comment

Choose a reason for hiding this comment

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

일단 간단하게 한 번 훑어봤어요 RC 날릴겡

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 클래스 이름을 EvnetResponse로 바꾸는 건 어때요? Detail보단, 그게 나을 것 같아서요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했습니당

for (Image image : images) {
imageUrlPerEventId.put(image.getContentId(), image.getName());

Map<Long, AllImagesOfContent> imageUrlsPerEventId = new HashMap<>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

stream으로 풀어볼수도 있지 않을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

전에 어디선가 코드리뷰 받으면서 알게된 건데 stream.forEach()로 원소를 순회하게 되면, 도중에 예외가 발생했을 때 순회가 중단되지 않고 순회를 마친 후 예외가 던져진다고 하더라구요.
그래서 저는 반복문 내에 레파지토리를 조회하는 로직이 포함된 경우 for문으로 풀어서 작성하고 있습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

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

그 stream.forEach()로 원소순회 시 예외 발생하면 순회를 마친 후 예외가 던져진다.
는 아마 내가 알려줬던 것 같은데, 기억하고 있네

나는 반복문 내에 Repository에서 조회 로직이 필요하도록 짰다면 다른 방식을 한번 고민해볼것 같아.
지금 그러면 쿼리가 event 수만큼 나가고 있는 거잖아.
내 생각엔 findAllByEventIds(eventIds)를 하고, 그 다음에 grouping을 할 수도 있을 것 같은데, 어떻게 생각해?
그러면 쿼리가 훨씬 줄 것 같아서

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

아아 그런 방법이 있었네
바로 반영했어
하는김에 중복 코드들을 ImageQueryService를 만들어서 옮겼습니당

for (Image image : images) {
imageUrlPerEventId.put(image.getContentId(), image.getName());

Map<Long, AllImagesOfContent> imageUrlsPerEventId = new HashMap<>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

final 붙여주세용

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했습니다!

final List<String> informationImageUrls = extractInformationImages(imageUrls);
.collect(toList()));
final String thumbnailImageUrl = images.extractThumbnailImage();
final List<String> informationImageUrls = images.extractInformationImages();
return EventDetailResponse.from(event, thumbnailImageUrl, informationImageUrls);
Copy link
Collaborator

Choose a reason for hiding this comment

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

아예 파라미터로 Images를 넘겨도 좋을 것 같네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

좋은 아이디어 감사합니다~~ 반영했습니다!

return images.stream()
.filter(Image::isThumbnail)
.findFirst()
.orElseThrow()
Copy link
Collaborator

Choose a reason for hiding this comment

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

orElseThrow에서 Exception을 지정하고 테스트를 짜보면 좋을 것 같아

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

헐 커스텀 예외 만들려고 생각했었는데 잊어버렸나보다...
알려줘서 고마워 바로 반영할게

final Map<Long, AllImagesOfContent> imagesPerEventId) {
return events.stream()
.map(event -> {
final AllImagesOfContent allImageUrls = imagesPerEventId.get(event.getId());
Copy link
Collaborator

Choose a reason for hiding this comment

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

이정도 사이즈의 메서드라면 별도로 분리하는건 어떨까?
그리고 Collect할 때 UnModifiableList로 해줄 수 있겠니

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했습니당

) {
return groupByEventStatus.values().stream()
.map(events -> makeEventResponsesByStatus(events, imagesPerEventId))
.reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이정도면 아마 reduce(new ArrayList<>(), List::addAll)로 할 수 있지 않나?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

아 그거 처음에 그렇게 하려고 했는데 addAll의 반환 타입이 boolean이라 안되는 걸로 알고있어용

@@ -65,4 +70,28 @@ public static EventDetailResponse from(
event.getEventMode().getValue()
);
}

public static List<EventDetailResponse> makeEventResponsesByStatus(final List<Event> events,
Copy link
Collaborator

Choose a reason for hiding this comment

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

여기 변환로직이 조금 복잡한데 천천히 바꿔보자 솔직히 안 읽혀

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오키오키

Copy link
Collaborator Author

@amaran-th amaran-th left a comment

Choose a reason for hiding this comment

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

@hong-sile
리뷰 반영했습니당~~

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했습니당

final Map<Long, AllImagesOfContent> imagesPerEventId) {
return events.stream()
.map(event -> {
final AllImagesOfContent allImageUrls = imagesPerEventId.get(event.getId());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했습니당

) {
return groupByEventStatus.values().stream()
.map(events -> makeEventResponsesByStatus(events, imagesPerEventId))
.reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

아 그거 처음에 그렇게 하려고 했는데 addAll의 반환 타입이 boolean이라 안되는 걸로 알고있어용

return images.stream()
.filter(Image::isThumbnail)
.findFirst()
.orElseThrow()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

헐 커스텀 예외 만들려고 생각했었는데 잊어버렸나보다...
알려줘서 고마워 바로 반영할게

@@ -65,4 +70,28 @@ public static EventDetailResponse from(
event.getEventMode().getValue()
);
}

public static List<EventDetailResponse> makeEventResponsesByStatus(final List<Event> events,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오키오키

final List<String> informationImageUrls = extractInformationImages(imageUrls);
.collect(toList()));
final String thumbnailImageUrl = images.extractThumbnailImage();
final List<String> informationImageUrls = images.extractInformationImages();
return EventDetailResponse.from(event, thumbnailImageUrl, informationImageUrls);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

좋은 아이디어 감사합니다~~ 반영했습니다!

for (Image image : images) {
imageUrlPerEventId.put(image.getContentId(), image.getName());

Map<Long, AllImagesOfContent> imageUrlsPerEventId = new HashMap<>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

전에 어디선가 코드리뷰 받으면서 알게된 건데 stream.forEach()로 원소를 순회하게 되면, 도중에 예외가 발생했을 때 순회가 중단되지 않고 순회를 마친 후 예외가 던져진다고 하더라구요.
그래서 저는 반복문 내에 레파지토리를 조회하는 로직이 포함된 경우 for문으로 풀어서 작성하고 있습니다.

for (Image image : images) {
imageUrlPerEventId.put(image.getContentId(), image.getName());

Map<Long, AllImagesOfContent> imageUrlsPerEventId = new HashMap<>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했습니다!

Copy link
Collaborator Author

@amaran-th amaran-th left a comment

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@amaran-th amaran-th left a comment

Choose a reason for hiding this comment

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

- 단건 조회, 다건 조회 API에 적용

#803
Copy link
Collaborator

@hong-sile hong-sile left a comment

Choose a reason for hiding this comment

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

일단 이번 목표는 명세를 수정하는 거니, 더 리팩터링 관련해선 리뷰를 안 남길게요. 수고했슴다

for (Image image : images) {
imageUrlPerEventId.put(image.getContentId(), image.getName());

Map<Long, AllImagesOfContent> imageUrlsPerEventId = new HashMap<>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

그 stream.forEach()로 원소순회 시 예외 발생하면 순회를 마친 후 예외가 던져진다.
는 아마 내가 알려줬던 것 같은데, 기억하고 있네

나는 반복문 내에 Repository에서 조회 로직이 필요하도록 짰다면 다른 방식을 한번 고민해볼것 같아.
지금 그러면 쿼리가 event 수만큼 나가고 있는 거잖아.
내 생각엔 findAllByEventIds(eventIds)를 하고, 그 다음에 grouping을 할 수도 있을 것 같은데, 어떻게 생각해?
그러면 쿼리가 훨씬 줄 것 같아서

return new ArrayList<>();
}

return tags.stream()
Copy link
Collaborator

Choose a reason for hiding this comment

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

여기 작업하는 부분이 아니긴 하지만, 이런 것도 엄청 쿼리가 많이 나가는 부분이잖아 In절 쿼리로 한번에 조회하도록 할 수있을 것 같은데?

tagRepository.findByNames(tagNames)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

반영했어~~!

final String thumbnailImageUrl = savedImages.extractThumbnailImage();
final List<String> informationImageUrls = savedImages.extractInformationImages();
return EventResponse.from(event, thumbnailImageUrl, informationImageUrls);
return EventResponse.from(event, savedImages);
Copy link
Collaborator

Choose a reason for hiding this comment

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

코드리뷰 해줬던 부분을 반영하고, 하나의 커밋으로 합쳐서 커밋했네.
앞으로는 쪼개서 커밋해보도록 하자

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵...!👀

amaran-th added 4 commits November 2, 2023 15:10
- 스크랩 행사 목록 조회 시 행사마다 image 조회 쿼리가 나가는 대신 행사들의 이미지를 모두 불러온 후 그룹핑되도록 수정
- 코드 중복을 없애기 위한 ImageQueryService 추가

#803
@amaran-th amaran-th merged commit 2bf14c4 into backend-main Nov 2, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend 백엔드 관련 이슈 High Priority 리뷰 우선순위가 높은 PR 리팩터링 테스트 코드의 검증 값이 변환하지 않고 코드 변경
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants