-
Notifications
You must be signed in to change notification settings - Fork 0
[refactor] 방 생성, 투표 생성 api 수정 #183
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
Changes from all commits
70c30c1
0163960
dad52a6
1afa3e2
d8674b9
5d5f23f
1d110f3
db185f3
34e46ae
f2c3faf
ef2444b
691e99e
3cc47d5
3b7cbc5
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 |
|---|---|---|
|
|
@@ -21,13 +21,23 @@ public class RoomParticipant extends BaseDomainEntity { | |
|
|
||
| private Long roomId; | ||
|
|
||
| public static RoomParticipant withoutId(Long userId, Long roomId, String roomParticipantRole) { | ||
| public static RoomParticipant memberWithoutId(Long userId, Long roomId) { | ||
| return RoomParticipant.builder() | ||
| .currentPage(0) | ||
| .userPercentage(0.0) | ||
| .userId(userId) | ||
| .roomId(roomId) | ||
| .roomParticipantRole(roomParticipantRole) | ||
| .roomParticipantRole(RoomParticipantRole.MEMBER.getType()) | ||
| .build(); | ||
| } | ||
|
|
||
| public static RoomParticipant hostWithoutId(Long userId, Long roomId) { | ||
| return RoomParticipant.builder() | ||
| .currentPage(0) | ||
| .userPercentage(0.0) | ||
| .userId(userId) | ||
| .roomId(roomId) | ||
| .roomParticipantRole(RoomParticipantRole.HOST.getType()) | ||
| .build(); | ||
| } | ||
|
Comment on lines
+24
to
42
Contributor
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. LGTM |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package konkuk.thip.roompost.application.service.helper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.book.application.port.out.BookCommandPort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.book.domain.Book; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.common.annotation.HelperService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.application.port.out.RoomCommandPort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.application.port.out.RoomParticipantCommandPort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.domain.Room; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.domain.RoomParticipant; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @HelperService | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @RequiredArgsConstructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class RoomProgressHelper { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final RoomParticipantCommandPort roomParticipantCommandPort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final RoomCommandPort roomCommandPort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final BookCommandPort bookCommandPort; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void updateUserAndRoomProgress(Long userId, Long roomId, int currentPage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RoomParticipant roomParticipant = roomParticipantCommandPort.getByUserIdAndRoomIdOrThrow(userId, roomId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Room room = roomCommandPort.getByIdOrThrow(roomId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Book book = bookCommandPort.findById(room.getBookId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 1. 유저 진행률 update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean updated = roomParticipant.updateUserProgress(currentPage, book.getPageCount()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!updated) return; // update 되지 않았으면 종료 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 2. 방 평균 진행률 update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<RoomParticipant> all = roomParticipantCommandPort.findAllByRoomId(roomId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double total = all.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(p -> !roomParticipant.getId().equals(p.getId())) // 현재 유저 제외 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .mapToDouble(RoomParticipant::getUserPercentage) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .sum(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total += roomParticipant.getUserPercentage(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| room.updateRoomPercentage(total / all.size()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 3. 영속화 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roomCommandPort.update(room); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roomParticipantCommandPort.update(roomParticipant); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+45
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. 참여자 목록 공집합 및 평균 계산 시 예외 가능성 방어
간단한 방어 로직을 추가해 주세요. // 2. 방 평균 진행률 update
List<RoomParticipant> all = roomParticipantCommandPort.findAllByRoomId(roomId);
- double total = all.stream()
+ if (all.isEmpty()) {
+ return; // 참여자가 없으면 평균 계산 불가
+ }
+ double total = all.stream()
.filter(p -> !roomParticipant.getId().equals(p.getId())) // 현재 유저 제외
.mapToDouble(RoomParticipant::getUserPercentage)
.sum();
total += roomParticipant.getUserPercentage();
room.updateRoomPercentage(total / all.size());
// 3. 영속화
roomCommandPort.update(room);
roomParticipantCommandPort.update(roomParticipant);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
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. 방을 생성할때 무조건 방의 HOST를 위해 최소 하나의 RoomParticipant가 만들어지는데 참여자가 없을리가 있나? 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. @buzz0331 맞는 말씀입니다! 비즈니스 로직상 방 생성 시 항상 HOST 역할의 RoomParticipant가 생성되고, 이 헬퍼 메서드가 호출되는 경로(투표 생성, 기록 생성)에서는 모두 사용자가 해당 방의 참여자인지 사전 검증을 거치기 때문에 논리적으로 참여자 목록이 비어있을 수 없습니다. 해당 방어 로직은 불필요한 over-engineering일 가능성이 높습니다. 비즈니스 규칙이 명확하게 보장하는 상황에서는 과도한 방어 코드가 오히려 코드의 가독성을 해칠 수 있죠. 기존 코드 그대로 유지하시는 것이 적절할 것 같습니다. ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,10 @@ | |
| import konkuk.thip.book.application.port.out.BookCommandPort; | ||
| import konkuk.thip.book.domain.Book; | ||
| import konkuk.thip.room.application.port.out.RoomCommandPort; | ||
| import konkuk.thip.room.application.port.out.RoomParticipantCommandPort; | ||
| import konkuk.thip.room.application.service.validator.RoomParticipantValidator; | ||
| import konkuk.thip.room.domain.Room; | ||
| import konkuk.thip.roompost.application.service.helper.RoomProgressHelper; | ||
| import konkuk.thip.vote.application.port.in.VoteCreateUseCase; | ||
| import konkuk.thip.vote.application.port.in.dto.VoteCreateCommand; | ||
| import konkuk.thip.vote.application.port.in.dto.VoteCreateResult; | ||
|
|
@@ -22,14 +25,19 @@ | |
| @Slf4j | ||
| public class VoteCreateService implements VoteCreateUseCase { | ||
|
|
||
| private final RoomParticipantValidator roomParticipantValidator; | ||
| private final RoomParticipantCommandPort roomParticipantCommandPort; | ||
| private final VoteCommandPort voteCommandPort; | ||
| private final RoomCommandPort roomCommandPort; | ||
| private final BookCommandPort bookCommandPort; | ||
|
|
||
| private final RoomProgressHelper roomProgressHelper; | ||
|
|
||
| @Transactional | ||
| @Override | ||
| //todo UserRoom 업데이트 로직 추가 필요!! | ||
| public VoteCreateResult createVote(VoteCreateCommand command) { | ||
| roomParticipantValidator.validateUserIsRoomMember(command.roomId(), command.userId()); | ||
|
Member
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. 굿굿 |
||
|
|
||
| // 1. validate | ||
| Vote vote = Vote.withoutId( | ||
| command.content(), | ||
|
|
@@ -39,7 +47,9 @@ public VoteCreateResult createVote(VoteCreateCommand command) { | |
| command.roomId() | ||
| ); | ||
|
|
||
| validateVote(vote); | ||
| Room room = roomCommandPort.getByIdOrThrow(vote.getRoomId()); | ||
| Book book = bookCommandPort.findById(room.getBookId()); | ||
| validateVote(vote, book); | ||
|
|
||
| // 2. vote 저장 | ||
| Long savedVoteId = voteCommandPort.saveVote(vote); | ||
|
|
@@ -48,19 +58,18 @@ public VoteCreateResult createVote(VoteCreateCommand command) { | |
| List<VoteItem> voteItems = command.voteItemCreateCommands().stream() | ||
| .map(itemCmd -> VoteItem.withoutId( | ||
| itemCmd.itemName(), | ||
| 0, | ||
| savedVoteId | ||
| )) | ||
| .toList(); | ||
| voteCommandPort.saveAllVoteItems(voteItems); | ||
|
|
||
| // 4. RoomParticipant, Room progress 정보 update | ||
| roomProgressHelper.updateUserAndRoomProgress(vote.getCreatorId(), vote.getRoomId(), vote.getPage()); | ||
|
|
||
| return VoteCreateResult.of(savedVoteId, command.roomId()); | ||
| } | ||
|
|
||
| private void validateVote(Vote vote) { | ||
| Room room = roomCommandPort.getByIdOrThrow(vote.getRoomId()); | ||
| Book book = bookCommandPort.findById(room.getBookId()); | ||
|
|
||
| private void validateVote(Vote vote, Book book) { | ||
| // 페이지 유효성 검증 | ||
| vote.validatePage(book.getPageCount()); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,8 +7,10 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.common.util.TestEntityFactory; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.adapter.out.jpa.CategoryJpaEntity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.adapter.out.jpa.RoomJpaEntity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.adapter.out.jpa.RoomParticipantJpaEntity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.adapter.out.persistence.repository.category.CategoryJpaRepository; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.adapter.out.persistence.repository.RoomJpaRepository; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.room.adapter.out.persistence.repository.roomparticipant.RoomParticipantJpaRepository; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.user.adapter.out.jpa.AliasJpaEntity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.user.adapter.out.jpa.UserJpaEntity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import konkuk.thip.user.adapter.out.jpa.UserRole; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -24,6 +26,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.test.context.ActiveProfiles; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.test.web.servlet.MockMvc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.test.web.servlet.ResultActions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.time.LocalDate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.time.format.DateTimeFormatter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -63,13 +66,17 @@ class RoomCreateAPITest { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private RoomJpaRepository roomJpaRepository; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private RoomParticipantJpaRepository roomParticipantJpaRepository; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @AfterEach | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void tearDown() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roomJpaRepository.deleteAll(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bookJpaRepository.deleteAll(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userJpaRepository.deleteAll(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| categoryJpaRepository.deleteAll(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aliasJpaRepository.deleteAll(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roomParticipantJpaRepository.deleteAllInBatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roomJpaRepository.deleteAllInBatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bookJpaRepository.deleteAllInBatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userJpaRepository.deleteAllInBatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| categoryJpaRepository.deleteAllInBatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aliasJpaRepository.deleteAllInBatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void saveUserAndCategory() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -273,4 +280,35 @@ void room_create_book_not_exist() throws Exception { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @DisplayName("방 생성에 성공하면, 방장의 정보가 DB에 저장된다.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional // RoomParticipant -> Room, User 의 manyToOne 지연로딩을 위해 추가 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void room_create_room_participant_save_success() throws Exception { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //given : user, category, pageCount값이 있는 book 생성, request 생성 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveUserAndCategory(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveBookWithPageCount(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Long userId = userJpaRepository.findAll().get(0).getUserId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Long bookId = bookJpaRepository.findAll().get(0).getBookId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Long categoryId = categoryJpaRepository.findAll().get(0).getCategoryId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, Object> request = buildRoomCreateRequest(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ResultActions result = mockMvc.perform(post("/rooms") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .requestAttr("userId", userId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .contentType(MediaType.APPLICATION_JSON) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .content(objectMapper.writeValueAsString(request) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result.andExpect(status().isOk()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .andExpect(jsonPath("$.data.roomId").exists()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RoomParticipantJpaEntity roomParticipantJpaEntity = roomParticipantJpaRepository.findAll().get(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertThat(roomParticipantJpaEntity.getUserJpaEntity().getUserId()).isEqualTo(userId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RoomJpaEntity savedRoomJpaEntity = roomJpaRepository.findAll().get(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertThat(roomParticipantJpaEntity.getRoomJpaEntity().getRoomId()).isEqualTo(savedRoomJpaEntity.getRoomId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+283
to
+313
Contributor
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. LGTM |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+283
to
314
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. 💡 Verification agent 🧩 Analysis chain검증 보강 제안: 역할/초기값까지 확인하고 조회도 더 견고하게 현재는 참가자-유저/방 매핑만 검증합니다. 다음을 추가하면 회귀를 더 잘 잡을 수 있습니다.
예시 수정: result.andExpect(status().isOk())
.andExpect(jsonPath("$.data.roomId").exists());
- RoomParticipantJpaEntity roomParticipantJpaEntity = roomParticipantJpaRepository.findAll().get(0);
- assertThat(roomParticipantJpaEntity.getUserJpaEntity().getUserId()).isEqualTo(userId);
-
- RoomJpaEntity savedRoomJpaEntity = roomJpaRepository.findAll().get(0);
- assertThat(roomParticipantJpaEntity.getRoomJpaEntity().getRoomId()).isEqualTo(savedRoomJpaEntity.getRoomId());
+ String json = result.andReturn().getResponse().getContentAsString();
+ Long roomId = objectMapper.readTree(json).path("data").path("roomId").asLong();
+
+ RoomParticipantJpaEntity roomParticipant = roomParticipantJpaRepository
+ .findByUserIdAndRoomId(userId, roomId)
+ .orElseThrow();
+ assertThat(roomParticipant.getUserJpaEntity().getUserId()).isEqualTo(userId);
+ assertThat(roomParticipant.getRoomJpaEntity().getRoomId()).isEqualTo(roomId);
+ // 초기 상태 검증(도메인 기본값에 맞게 조정)
+ assertThat(roomParticipant.getRoomParticipantRole()).isNotNull(); // .isEqualTo(RoomParticipantRole.HOST) 권장
+ assertThat(roomParticipant.getCurrentPage()).isZero();
+ assertThat(roomParticipant.getUserPercentage()).isEqualTo(0.0);🏁 Script executed: #!/bin/bash
set -e
# RoomParticipantJpaEntity 클래스 위치 확인
rg "class RoomParticipantJpaEntity" -n
# RoomParticipantJpaRepository 인터페이스 및 findByUserIdAndRoomId 메서드 존재 여부 확인
rg "interface RoomParticipantJpaRepository" -n
rg "findByUserIdAndRoomId" -n
# 엔티티 필드 및 기본값 설정 확인
rg "currentPage" -n src/main/java
rg "userPercentage" -n src/main/javaLength of output: 8152 아래 스크립트로 🏁 Script executed: #!/bin/bash
set -e
# 역할(role) 필드 선언 및 기본값 확인
rg "roomParticipantRole" -n src/main/java/konkuk/thip/room/adapter/out/jpa/RoomParticipantJpaEntity.java
# 엔티티 파일 상단 일부 출력
sed -n '1,200p' src/main/java/konkuk/thip/room/adapter/out/jpa/RoomParticipantJpaEntity.javaLength of output: 2311 아래 스크립트로 도메인 🏁 Script executed: #!/bin/bash
set -e
# 1. 도메인 RoomParticipant 클래스 내용 확인
rg "class RoomParticipant" -n src/main/java
sed -n '1,200p' src/main/java/konkuk/thip/room/domain/RoomParticipant.java
# 2. 방 생성 서비스에서 RoomParticipant를 생성하는 로직 확인
rg "class RoomCreateService" -n src/main/java
sed -n '1,200p' src/main/java/konkuk/thip/room/application/service/RoomCreateService.javaLength of output: 6197 검증 보강 제안: 역할/초기값 검증 및 조회 견고화 현재 테스트는 참가자–유저/방 매핑만 검증하고 있습니다. 아래 항목을 추가해 회귀를 더욱 잘 잡아주세요.
예시 수정: result.andExpect(status().isOk())
.andExpect(jsonPath("$.data.roomId").exists());
- RoomParticipantJpaEntity roomParticipantJpaEntity = roomParticipantJpaRepository.findAll().get(0);
- assertThat(roomParticipantJpaEntity.getUserJpaEntity().getUserId()).isEqualTo(userId);
-
- RoomJpaEntity savedRoomJpaEntity = roomJpaRepository.findAll().get(0);
- assertThat(roomParticipantJpaEntity.getRoomJpaEntity().getRoomId()).isEqualTo(savedRoomJpaEntity.getRoomId());
+ String json = result.andReturn().getResponse().getContentAsString();
+ Long roomId = objectMapper.readTree(json).path("data").path("roomId").asLong();
+
+ RoomParticipantJpaEntity roomParticipant = roomParticipantJpaRepository
+ .findByUserIdAndRoomId(userId, roomId)
+ .orElseThrow();
+ assertThat(roomParticipant.getUserJpaEntity().getUserId()).isEqualTo(userId);
+ assertThat(roomParticipant.getRoomJpaEntity().getRoomId()).isEqualTo(roomId);
+ assertThat(roomParticipant.getRoomParticipantRole()).isEqualTo(RoomParticipantRole.HOST);
+ assertThat(roomParticipant.getCurrentPage()).isZero();
+ assertThat(roomParticipant.getUserPercentage()).isEqualTo(0.0);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
와우 공통 로직을 분리하니까 확실히 응용서비스 로직이 간단해졌네요! 굿굿