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

DayLog의 Item들 순서 변경 기능 구현 #129

Merged
merged 10 commits into from
Jul 25, 2023

Conversation

hgo641
Copy link
Collaborator

@hgo641 hgo641 commented Jul 25, 2023

📄 Summary

#124

DayLog의 Item들 순서를 변경하는 기능 구현했습니다.

  • 정렬된 DayLog Item의 Id 리스트를 받아오면, 그 순서에 맞게 Item들의 ordinal값을 변경합니다.
  • Item의 Id 리스트가 DayLog에 속한 모든 Item들의 아이디를 포함하고 있지 않다면 예외를 던집니다.

🙋🏻 More

  • DayLogServiceupdateItemOrdinals 메소드에서 ItemFixture에 위치한 DayLog객체를 사용하고 있습니다. 새로운 DayLogItem들을 생성하면 코드가 너무 길어져서 ItemFixture에 있는 DayLog객체를 사용했는데 이 부분 괜찮나요? 새로운 객체들을 만들어서 테스트해보길 원하신다면 코멘트 달아주세요.

  • 의견있으시면 말해주세요!

batch update를 사용해 성능을 높여보려고 했는데, batch update를 쓰려면 Item객체의 ID 생성 전략을 spring 코드에서 직접 할당해줘야하는 전략으로 바꾸거나, JDBC를 사용해야 한다고 하더군요...
그래서 일단은 업데이트해야하는 Item의 개수만큼 쿼리를 날리는 방식으로 구현했습니다... 나중에 시간이 된다면 성능 향상을 도전해보겠습니다. (하지만 DayLog 하나당 Item 최대 20개? 나쁘지 않을지도?)

@hgo641 hgo641 added this to the 3차 스프린트 milestone Jul 25, 2023
@hgo641 hgo641 self-assigned this Jul 25, 2023
Comment on lines +80 to 89
private void changeOrdinalOfItemsByOrderedItemIds(final List<Long> orderedItemIds) {
int ordinal = 1;

for (final Long itemId : orderedItemIds) {
final Item item = itemRepository.findById(itemId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_TRIP_ITEM_ID));
item.changeOrdinal(ordinal++);
}
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이 부분 때문에 DayLogServiceItemRepository를 주입받고 있습니다.
ItemRepository에 의존하는 게 싫다면 DayLog.getItem()을 사용하는 방법이 있긴 한데,
Stream으로 해당 id를 가지는 객체를 찾거나(O(n)) 몇 줄의 코드를 더 추가해서 탐색 시간을 줄여야 할 것 같은데... 어떻게 생각하시나요?

Copy link
Collaborator

@LJW25 LJW25 left a comment

Choose a reason for hiding this comment

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

홍고 멋져요 🍊

Comment on lines +118 to +126
=== DayLog 아이템들 순서 수정 (PATCH /trips/:tripId/daylogs/:dayLogId/order)

==== 요청
include::{snippets}/day-log-controller-test/update-ordinal-of-items/http-request.adoc[]
include::{snippets}/day-log-controller-test/update-ordinal-of-items/path-parameters.adoc[]
include::{snippets}/day-log-controller-test/update-ordinal-of-items/request-fields.adoc[]

==== 응답
include::{snippets}/day-log-controller-test/update-day-log-title/http-response.adoc[]
Copy link
Collaborator

Choose a reason for hiding this comment

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

RestDocs 💯

requestFields(
fieldWithPath("itemIds")
.type(JsonFieldType.ARRAY)
.description("정렬된 아이템의 IDs")
Copy link
Collaborator

Choose a reason for hiding this comment

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

새로운 순서의 아이템 IDs 어떤가요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

옼케이

//given
final ItemsOrdinalUpdateRequest request = new ItemsOrdinalUpdateRequest(List.of(3L,2L,1L));

doNothing().when(dayLogService).updateOrdinalOfItems( any(), any());
Copy link
Collaborator

Choose a reason for hiding this comment

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

알수없는 공백
updateOrdinalOfItems( any(), any());

dayLogService.updateOrdinalOfItems(1L, request);

// then
List<Item> items = DAYLOG_FOR_ITEM_FIXTURE.getItems();
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
Member

@jjongwa jjongwa left a comment

Choose a reason for hiding this comment

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

항상 꾸준한 홍고생 응원합니다.
간단한 리뷰 남겼어요. 한 번씩 봐주세용

Comment on lines 61 to 70
public void updateOrdinalOfItems(final Long dayLogId,
final ItemsOrdinalUpdateRequest itemsOrdinalUpdateRequest) {
final DayLog dayLog = dayLogRepository.findById(dayLogId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_DAY_LOG_ID));
final List<Item> items = dayLog.getItems();

final List<Long> orderedItemIds = itemsOrdinalUpdateRequest.getItemIds();
validateOrderedItemIds(items, orderedItemIds);
changeOrdinalOfItemsByOrderedItemIds(orderedItemIds);
}
Copy link
Member

Choose a reason for hiding this comment

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

image 매개변수 한 줄에 가도 제한 안넘는거 같아요

Comment on lines 72 to 78
private void validateOrderedItemIds(final List<Item> items, final List<Long> orderedItemIds) {
for (final Item item : items) {
if (!orderedItemIds.contains(item.getId())) {
throw new BadRequestException(INVALID_ORDERED_ITEM_IDS);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private void validateOrderedItemIds(final List<Item> items, final List<Long> orderedItemIds) {
for (final Item item : items) {
if (!orderedItemIds.contains(item.getId())) {
throw new BadRequestException(INVALID_ORDERED_ITEM_IDS);
}
}
}
private void validateOrderedItemIds(final List<Item> items, final List<Long> orderedItemIds) {
items.stream().filter(item -> !orderedItemIds.contains(item.getId())).forEach(item -> {
throw new BadRequestException(INVALID_ORDERED_ITEM_IDS);
});
}

어떨까요

Copy link
Member

Choose a reason for hiding this comment

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

어 이미 바꼈네

Comment on lines 132 to 134
final ItemsOrdinalUpdateRequest request = new ItemsOrdinalUpdateRequest(List.of(3L,2L,1L));

doNothing().when(dayLogService).updateOrdinalOfItems( any(), any());
Copy link
Member

@jjongwa jjongwa Jul 25, 2023

Choose a reason for hiding this comment

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

Suggested change
final ItemsOrdinalUpdateRequest request = new ItemsOrdinalUpdateRequest(List.of(3L,2L,1L));
doNothing().when(dayLogService).updateOrdinalOfItems( any(), any());
//given
final ItemsOrdinalUpdateRequest request = new ItemsOrdinalUpdateRequest(List.of(3L, 2L, 1L));
doNothing().when(dayLogService).updateOrdinalOfItems(any(), any());

코드 정렬을 생활화 하자

Copy link
Collaborator

@waterricecake waterricecake left a comment

Choose a reason for hiding this comment

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

수고하셨습니다 홍고


@PatchMapping("/order")
public ResponseEntity<Void> updateOrdinalOfItems(
@PathVariable final Long tripId,
Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 안쓰이는거 같아요!

Comment on lines +76 to +79
final Set<Long> itemIds = items.stream()
.map(Item::getId)
.collect(Collectors.toSet());
final Set<Long> orderedItemIdsSet = new HashSet<>(orderedItemIds);
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 부분이 검증을 하는 것이 orderedItemIds가 가진 요소들이 정확히 itemIds가 가지고 있는지 판별하는 것이라고 생각하는데 set으로 받아주는 것 보다 sort해서 정확히 같은지 비교하는 방법은 어떤가요?

Suggested change
final Set<Long> itemIds = items.stream()
.map(Item::getId)
.collect(Collectors.toSet());
final Set<Long> orderedItemIdsSet = new HashSet<>(orderedItemIds);
final List<Long> itemIds = items.stream()
.map(item->item.getId())
.sorted()
.collect(Collectors.toList());
final List<Long> sortedOrderItemIds = orderedItemIds.stream().sorted().collect(Collectors.toList());
if(!itemIds.equals(sortedOrderItemIds)){
throw new BadRequestException(INVALID_ORDERED_ITEM_IDS);
}

@hgo641 hgo641 merged commit 862f281 into develop Jul 25, 2023
1 check passed
@hgo641 hgo641 deleted the feature/#124-item-order-update branch July 27, 2023 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants