-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from 5 commits
4517dc8
0871d36
aa2aa0a
d31f14d
dad16b1
8d18831
7e430b7
b0bb337
1d41b41
c865e10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package hanglog.trip.dto.request; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ItemsOrdinalUpdateRequest { | ||
@NotEmpty(message = "아이템 아이디들을 입력해주세요.") | ||
private List<Long> itemIds; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
|
||
import hanglog.trip.dto.request.DayLogUpdateTitleRequest; | ||
import hanglog.trip.dto.request.ItemsOrdinalUpdateRequest; | ||
import hanglog.trip.dto.response.DayLogGetResponse; | ||
import hanglog.trip.service.DayLogService; | ||
import jakarta.validation.Valid; | ||
|
@@ -37,4 +38,13 @@ public ResponseEntity<Void> updateDayLogTitle( | |
dayLogService.updateTitle(dayLogId, request); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@PatchMapping("/order") | ||
public ResponseEntity<Void> updateOrdinalOfItems( | ||
@PathVariable final Long tripId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 안쓰이는거 같아요! |
||
@PathVariable final Long dayLogId, | ||
@RequestBody @Valid final ItemsOrdinalUpdateRequest itemsOrdinalUpdateRequest) { | ||
dayLogService.updateOrdinalOfDayLogItems(dayLogId, itemsOrdinalUpdateRequest); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,14 +2,20 @@ | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
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; | ||||||||||||||||||||||||||
import hanglog.trip.domain.Item; | ||||||||||||||||||||||||||
import hanglog.trip.domain.repository.DayLogRepository; | ||||||||||||||||||||||||||
import hanglog.trip.domain.repository.ItemRepository; | ||||||||||||||||||||||||||
import hanglog.trip.dto.request.DayLogUpdateTitleRequest; | ||||||||||||||||||||||||||
import hanglog.trip.dto.request.ItemsOrdinalUpdateRequest; | ||||||||||||||||||||||||||
import hanglog.trip.dto.response.DayLogGetResponse; | ||||||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||||||
import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||||||
import org.springframework.stereotype.Service; | ||||||||||||||||||||||||||
import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||||||
|
@@ -20,6 +26,7 @@ | |||||||||||||||||||||||||
public class DayLogService { | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private final DayLogRepository dayLogRepository; | ||||||||||||||||||||||||||
private final ItemRepository itemRepository; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Transactional(readOnly = true) | ||||||||||||||||||||||||||
public DayLogGetResponse getById(final Long id) { | ||||||||||||||||||||||||||
|
@@ -50,4 +57,33 @@ private void validateAlreadyDeleted(final DayLog dayLog) { | |||||||||||||||||||||||||
throw new BadRequestException(ALREADY_DELETED_DATE); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public void updateOrdinalOfDayLogItems(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); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
어떨까요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어 이미 바꼈네 |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
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++); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+85
to
94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분 때문에 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,16 @@ | |
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import hanglog.trip.dto.request.DayLogUpdateTitleRequest; | ||
import hanglog.trip.dto.request.ItemsOrdinalUpdateRequest; | ||
import hanglog.trip.dto.response.DayLogGetResponse; | ||
import hanglog.trip.restdocs.RestDocsTest; | ||
import hanglog.trip.service.DayLogService; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -122,4 +125,35 @@ void updateDayLogTitle() throws Exception { | |
) | ||
); | ||
} | ||
|
||
@DisplayName("데이로그의 아이템들 순서를 변경할 수 있다.") | ||
@Test | ||
void updateOrdinalOfItems() throws Exception { | ||
//given | ||
final ItemsOrdinalUpdateRequest request = new ItemsOrdinalUpdateRequest(List.of(3L,2L,1L)); | ||
|
||
doNothing().when(dayLogService).updateOrdinalOfDayLogItems( any(), any()); | ||
|
||
// when & then | ||
mockMvc.perform(patch("/trips/{tripId}/daylogs/{dayLogId}/order", 1L, 1L) | ||
.contentType(APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))) | ||
.andExpect(status().isNoContent()) | ||
.andDo( | ||
restDocs.document( | ||
pathParameters( | ||
parameterWithName("tripId") | ||
.description("여행 ID"), | ||
parameterWithName("dayLogId") | ||
.description("날짜별 기록 ID") | ||
), | ||
requestFields( | ||
fieldWithPath("itemIds") | ||
.type(JsonFieldType.ARRAY) | ||
.description("정렬된 아이템의 IDs") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 새로운 순서의 아이템 IDs 어떤가요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 옼케이 |
||
.attributes(field("constraint", "ID 배열")) | ||
) | ||
) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
package hanglog.trip.service; | ||
|
||
import static hanglog.trip.fixture.DayLogFixture.*; | ||
import static hanglog.trip.fixture.DayLogFixture.LONDON_DAYLOG_1; | ||
import static hanglog.trip.fixture.DayLogFixture.UPDATED_LONDON_DAYLOG; | ||
import static hanglog.trip.fixture.ItemFixture.DAYLOG_FOR_ITEM_FIXTURE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.SoftAssertions.assertSoftly; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import hanglog.trip.domain.DayLog; | ||
import hanglog.trip.domain.Item; | ||
import hanglog.trip.domain.repository.DayLogRepository; | ||
import hanglog.trip.domain.repository.ItemRepository; | ||
import hanglog.trip.dto.request.DayLogUpdateTitleRequest; | ||
import hanglog.trip.dto.request.ItemsOrdinalUpdateRequest; | ||
import hanglog.trip.dto.response.DayLogGetResponse; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
@@ -29,6 +35,9 @@ class DayLogServiceTest { | |
@Mock | ||
private DayLogRepository dayLogRepository; | ||
|
||
@Mock | ||
private ItemRepository itemRepository; | ||
|
||
@DisplayName("날짜별 여행을 조회할 수 있다.") | ||
@Test | ||
void getDayLogById() { | ||
|
@@ -68,4 +77,30 @@ void updateTitle() { | |
verify(dayLogRepository).findById(UPDATED_LONDON_DAYLOG.getId()); | ||
verify(dayLogRepository).save(any(DayLog.class)); | ||
} | ||
|
||
@DisplayName("정렬된 아이템 ID 리스트를 받아 아이템들의 순서를 변경한다.") | ||
@Test | ||
void updateItemOrdinals() { | ||
// given | ||
final ItemsOrdinalUpdateRequest request = new ItemsOrdinalUpdateRequest(List.of(3L, 2L, 1L)); | ||
given(dayLogRepository.findById(1L)) | ||
.willReturn(Optional.of(DAYLOG_FOR_ITEM_FIXTURE)); | ||
given(itemRepository.findById(1L)) | ||
.willReturn(Optional.ofNullable(DAYLOG_FOR_ITEM_FIXTURE.getItems().get(0))); | ||
given(itemRepository.findById(2L)) | ||
.willReturn(Optional.ofNullable(DAYLOG_FOR_ITEM_FIXTURE.getItems().get(1))); | ||
given(itemRepository.findById(3L)) | ||
.willReturn(Optional.ofNullable(DAYLOG_FOR_ITEM_FIXTURE.getItems().get(2))); | ||
|
||
// when | ||
dayLogService.updateOrdinalOfDayLogItems(1L, request); | ||
|
||
// then | ||
List<Item> items = DAYLOG_FOR_ITEM_FIXTURE.getItems(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final 가능? |
||
assertSoftly(softly -> { | ||
softly.assertThat(items.get(0).getOrdinal()).isEqualTo(3); | ||
softly.assertThat(items.get(1).getOrdinal()).isEqualTo(2); | ||
softly.assertThat(items.get(2).getOrdinal()).isEqualTo(1); | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RestDocs 💯