Skip to content

Commit

Permalink
refactor: DayLog의 Item들 순서를 변경하는 로직 예외처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hgo641 committed Jul 25, 2023
1 parent aa2aa0a commit d31f14d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public enum ExceptionCode {
ALREADY_DELETED_TRIP_ITEM(2001, "이미 삭제된 여행 아이템입니다."),
ALREADY_DELETED_DATE(2002, "이미 삭제된 날짜입니다."),

INVALID_RATING(3001, "별점은 N.0점이거나 N.5점 형태이어야 합니다.");
INVALID_RATING(3001, "별점은 N.0점이거나 N.5점 형태이어야 합니다."),

INVALID_ORDERED_ITEM_IDS(4001, "날짜에 속한 모든 여행 아이템들의 ID가 필요합니다.");

private final int code;
private final String message;
Expand Down
20 changes: 14 additions & 6 deletions backend/src/main/java/hanglog/trip/service/DayLogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


import static hanglog.global.exception.ExceptionCode.ALREADY_DELETED_DATE;
import static hanglog.global.exception.ExceptionCode.INVALID_ORDERED_ITEM_IDS;
import static hanglog.global.exception.ExceptionCode.NOT_FOUND_DAY_LOG_ID;
import static hanglog.global.exception.ExceptionCode.NOT_FOUND_TRIP_ID;
import static hanglog.global.exception.ExceptionCode.NOT_FOUND_TRIP_ITEM_ID;

import hanglog.global.exception.BadRequestException;
import hanglog.trip.domain.DayLog;
Expand Down Expand Up @@ -62,19 +64,25 @@ public void updateOrdinalOfDayLogItems(final Long dayLogId,
.orElseThrow(() -> new BadRequestException(NOT_FOUND_DAY_LOG_ID));
final List<Item> items = dayLog.getItems();

// TODO: orders가 item들 수만큼 있는지 valid - custom exeption 추가 필요
if (itemsOrdinalUpdateRequest.getItemIds().size() != items.size()) {
throw new IllegalArgumentException("데이로그에 있는 모든 아이템들에 대한 아이디 리스트가 필요합니다.");
}
final List<Long> orderedItemIds = itemsOrdinalUpdateRequest.getItemIds();
validateOrderedItemIds(items, orderedItemIds);
changeOrdinalOfItemsByOrderedItemIds(orderedItemIds);
}

changeOrdinalOfItemsByOrderedItemIds(itemsOrdinalUpdateRequest.getItemIds());
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 changeOrdinalOfItemsByOrderedItemIds(final List<Long> orderedItemIds) {
int ordinal = 1;

for (final Long itemId : orderedItemIds) {
final Item item = itemRepository.findById(itemId).get();
final Item item = itemRepository.findById(itemId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_TRIP_ITEM_ID));
item.changeOrdinal(ordinal++);
}
}
Expand Down

0 comments on commit d31f14d

Please sign in to comment.