diff --git a/src/main/java/com/moabam/api/application/room/RoomService.java b/src/main/java/com/moabam/api/application/room/RoomService.java index d50c4658..fbd1fc4e 100644 --- a/src/main/java/com/moabam/api/application/room/RoomService.java +++ b/src/main/java/com/moabam/api/application/room/RoomService.java @@ -147,6 +147,15 @@ public void deportParticipant(Long managerId, Long roomId, Long memberId) { member.exitRoom(room.getRoomType()); } + public boolean checkIfParticipant(Long memberId, Long roomId) { + try { + getParticipant(memberId, roomId); + return true; + } catch (NotFoundException e) { + return false; + } + } + public void validateRoomById(Long roomId) { if (!roomRepository.existsById(roomId)) { throw new NotFoundException(ROOM_NOT_FOUND); diff --git a/src/main/java/com/moabam/api/application/room/SearchService.java b/src/main/java/com/moabam/api/application/room/SearchService.java index 73ee5cb9..9e62b9d0 100644 --- a/src/main/java/com/moabam/api/application/room/SearchService.java +++ b/src/main/java/com/moabam/api/application/room/SearchService.java @@ -1,12 +1,8 @@ package com.moabam.api.application.room; -import static com.moabam.global.common.util.GlobalConstant.NOT_COMPLETED_RANK; -import static com.moabam.global.common.util.GlobalConstant.ROOM_FIXED_SEARCH_SIZE; -import static com.moabam.global.error.model.ErrorMessage.INVENTORY_NOT_FOUND; -import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND; -import static com.moabam.global.error.model.ErrorMessage.ROOM_DETAILS_ERROR; -import static com.moabam.global.error.model.ErrorMessage.ROOM_MODIFY_UNAUTHORIZED_REQUEST; -import static org.apache.commons.lang3.StringUtils.isEmpty; +import static com.moabam.global.common.util.GlobalConstant.*; +import static com.moabam.global.error.model.ErrorMessage.*; +import static org.apache.commons.lang3.StringUtils.*; import java.time.LocalDate; import java.time.Period; @@ -51,6 +47,8 @@ import com.moabam.api.dto.room.RoomsHistoryResponse; import com.moabam.api.dto.room.RoutineResponse; import com.moabam.api.dto.room.TodayCertificateRankResponse; +import com.moabam.api.dto.room.UnJoinedRoomCertificateRankResponse; +import com.moabam.api.dto.room.UnJoinedRoomDetailsResponse; import com.moabam.global.common.util.ClockHolder; import com.moabam.global.error.exception.ForbiddenException; import com.moabam.global.error.exception.NotFoundException; @@ -187,6 +185,44 @@ public GetAllRoomsResponse searchRooms(String keyword, @Nullable RoomType roomTy return RoomMapper.toSearchAllRoomsResponse(hasNext, getAllRoomResponse); } + public UnJoinedRoomDetailsResponse getUnJoinedRoomDetails(Long roomId) { + Room room = roomRepository.findById(roomId) + .orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND)); + + List routines = routineRepository.findAllByRoomId(roomId); + List routineResponses = RoutineMapper.toRoutineResponses(routines); + List sortedDailyMemberCertifications = + certificationsSearchRepository.findSortedDailyMemberCertifications(roomId, clockHolder.date()); + List memberIds = sortedDailyMemberCertifications.stream() + .map(DailyMemberCertification::getMemberId) + .toList(); + List members = memberService.getRoomMembers(memberIds); + List inventories = inventorySearchRepository.findDefaultInventories(memberIds, + room.getRoomType().name()); + List unJoinedRoomCertificateRankResponses = new ArrayList<>(); + + int rank = 1; + for (DailyMemberCertification certification : sortedDailyMemberCertifications) { + Member member = members.stream() + .filter(m -> m.getId().equals(certification.getMemberId())) + .findAny() + .orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND)); + + Inventory inventory = inventories.stream() + .filter(i -> i.getMemberId().equals(member.getId())) + .findAny() + .orElseThrow(() -> new NotFoundException(INVENTORY_NOT_FOUND)); + + UnJoinedRoomCertificateRankResponse response = RoomMapper.toUnJoinedRoomCertificateRankResponse(member, + rank, inventory); + + unJoinedRoomCertificateRankResponses.add(response); + rank += 1; + } + + return RoomMapper.toUnJoinedRoomDetails(room, routineResponses, unJoinedRoomCertificateRankResponses); + } + private boolean isHasNext(List getAllRoomResponse, List rooms) { boolean hasNext = false; diff --git a/src/main/java/com/moabam/api/application/room/mapper/RoomMapper.java b/src/main/java/com/moabam/api/application/room/mapper/RoomMapper.java index 7e7a9d0c..21e86124 100644 --- a/src/main/java/com/moabam/api/application/room/mapper/RoomMapper.java +++ b/src/main/java/com/moabam/api/application/room/mapper/RoomMapper.java @@ -1,8 +1,12 @@ package com.moabam.api.application.room.mapper; +import static org.apache.commons.lang3.StringUtils.*; + import java.time.LocalDate; import java.util.List; +import com.moabam.api.domain.item.Inventory; +import com.moabam.api.domain.member.Member; import com.moabam.api.domain.room.Participant; import com.moabam.api.domain.room.Room; import com.moabam.api.dto.room.CreateRoomRequest; @@ -17,6 +21,8 @@ import com.moabam.api.dto.room.RoomsHistoryResponse; import com.moabam.api.dto.room.RoutineResponse; import com.moabam.api.dto.room.TodayCertificateRankResponse; +import com.moabam.api.dto.room.UnJoinedRoomCertificateRankResponse; +import com.moabam.api.dto.room.UnJoinedRoomDetailsResponse; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -44,6 +50,7 @@ public static RoomDetailsResponse toRoomDetailsResponse(Long memberId, Room room .managerNickName(managerNickname) .roomImage(room.getRoomImage()) .level(room.getLevel()) + .exp(room.getExp()) .roomType(room.getRoomType()) .certifyTime(room.getCertifyTime()) .currentUserCount(room.getCurrentUserCount()) @@ -131,4 +138,34 @@ public static GetAllRoomsResponse toSearchAllRoomsResponse(boolean hasNext, .rooms(getAllRoomResponse) .build(); } + + public static UnJoinedRoomDetailsResponse toUnJoinedRoomDetails(Room room, List routines, + List responses) { + return UnJoinedRoomDetailsResponse.builder() + .roomId(room.getId()) + .isPassword(!isEmpty(room.getPassword())) + .title(room.getTitle()) + .roomImage(room.getRoomImage()) + .level(room.getLevel()) + .exp(room.getExp()) + .roomType(room.getRoomType()) + .certifyTime(room.getCertifyTime()) + .currentUserCount(room.getCurrentUserCount()) + .maxUserCount(room.getMaxUserCount()) + .announcement(room.getAnnouncement()) + .routines(routines) + .certifiedRanks(responses) + .build(); + } + + public static UnJoinedRoomCertificateRankResponse toUnJoinedRoomCertificateRankResponse(Member member, int rank, + Inventory inventory) { + return UnJoinedRoomCertificateRankResponse.builder() + .rank(rank) + .memberId(member.getId()) + .nickname(member.getNickname()) + .awakeImage(inventory.getItem().getImage()) + .sleepImage(inventory.getItem().getImage()) + .build(); + } } diff --git a/src/main/java/com/moabam/api/domain/room/repository/RoomRepository.java b/src/main/java/com/moabam/api/domain/room/repository/RoomRepository.java index 494942fc..b086872f 100644 --- a/src/main/java/com/moabam/api/domain/room/repository/RoomRepository.java +++ b/src/main/java/com/moabam/api/domain/room/repository/RoomRepository.java @@ -42,9 +42,9 @@ List searchByKeywordAndRoomType(@Param(value = "keyword") String keyword, List searchByKeywordAndRoomId(@Param(value = "keyword") String keyword, @Param(value = "roomId") Long roomId); @Query(value = "select distinct rm.* from room rm left join routine rt on rm.id = rt.room_id " - + "where rm.title like %:keyword% " + + "where (rm.title like %:keyword% " + "or rm.manager_nickname like %:keyword% " - + "or rt.content like %:keyword% " + + "or rt.content like %:keyword%) " + "and rm.room_type = :roomType " + "and rm.id < :roomId " + "order by rm.id desc limit 11", nativeQuery = true) diff --git a/src/main/java/com/moabam/api/dto/room/RoomDetailsResponse.java b/src/main/java/com/moabam/api/dto/room/RoomDetailsResponse.java index abb7d06b..339ecbcc 100644 --- a/src/main/java/com/moabam/api/dto/room/RoomDetailsResponse.java +++ b/src/main/java/com/moabam/api/dto/room/RoomDetailsResponse.java @@ -15,6 +15,7 @@ public record RoomDetailsResponse( String managerNickName, String roomImage, int level, + int exp, RoomType roomType, int certifyTime, int currentUserCount, diff --git a/src/main/java/com/moabam/api/dto/room/UnJoinedRoomCertificateRankResponse.java b/src/main/java/com/moabam/api/dto/room/UnJoinedRoomCertificateRankResponse.java new file mode 100644 index 00000000..0dfe7a21 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/room/UnJoinedRoomCertificateRankResponse.java @@ -0,0 +1,14 @@ +package com.moabam.api.dto.room; + +import lombok.Builder; + +@Builder +public record UnJoinedRoomCertificateRankResponse( + int rank, + Long memberId, + String nickname, + String awakeImage, + String sleepImage +) { + +} diff --git a/src/main/java/com/moabam/api/dto/room/UnJoinedRoomDetailsResponse.java b/src/main/java/com/moabam/api/dto/room/UnJoinedRoomDetailsResponse.java new file mode 100644 index 00000000..d521ea30 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/room/UnJoinedRoomDetailsResponse.java @@ -0,0 +1,26 @@ +package com.moabam.api.dto.room; + +import java.util.List; + +import com.moabam.api.domain.room.RoomType; + +import lombok.Builder; + +@Builder +public record UnJoinedRoomDetailsResponse( + Long roomId, + boolean isPassword, + String title, + String roomImage, + int level, + int exp, + RoomType roomType, + int certifyTime, + int currentUserCount, + int maxUserCount, + String announcement, + List routines, + List certifiedRanks +) { + +} diff --git a/src/main/java/com/moabam/api/presentation/RoomController.java b/src/main/java/com/moabam/api/presentation/RoomController.java index 55146a8e..ce4725e3 100644 --- a/src/main/java/com/moabam/api/presentation/RoomController.java +++ b/src/main/java/com/moabam/api/presentation/RoomController.java @@ -32,6 +32,7 @@ import com.moabam.api.dto.room.MyRoomsResponse; import com.moabam.api.dto.room.RoomDetailsResponse; import com.moabam.api.dto.room.RoomsHistoryResponse; +import com.moabam.api.dto.room.UnJoinedRoomDetailsResponse; import com.moabam.global.auth.annotation.Auth; import com.moabam.global.auth.model.AuthMember; @@ -87,6 +88,18 @@ public void exitRoom(@Auth AuthMember authMember, @PathVariable("roomId") Long r roomService.exitRoom(authMember.id(), roomId); } + @GetMapping("/{roomId}/check") + @ResponseStatus(HttpStatus.OK) + public boolean checkIfParticipant(@Auth AuthMember authMember, @PathVariable("roomId") Long roomId) { + return roomService.checkIfParticipant(authMember.id(), roomId); + } + + @GetMapping("/{roomId}/un-joined") + @ResponseStatus(HttpStatus.OK) + public UnJoinedRoomDetailsResponse getUnJoinedRoomDetails(@PathVariable("roomId") Long roomId) { + return searchService.getUnJoinedRoomDetails(roomId); + } + @GetMapping("/{roomId}/{date}") @ResponseStatus(HttpStatus.OK) public RoomDetailsResponse getRoomDetails(@Auth AuthMember authMember, @PathVariable("roomId") Long roomId, diff --git a/src/test/java/com/moabam/api/presentation/RoomControllerTest.java b/src/test/java/com/moabam/api/presentation/RoomControllerTest.java index 580005b7..265450c4 100644 --- a/src/test/java/com/moabam/api/presentation/RoomControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/RoomControllerTest.java @@ -971,6 +971,72 @@ void get_join_history_success() throws Exception { .andDo(print()); } + @DisplayName("참여중이지 않은 방에 대한 확인 성공") + @WithMember + @Test + void check_if_participant_false_success() throws Exception { + // given + Room room = RoomFixture.room(); + Room savedRoom = roomRepository.save(room); + + // expected + mockMvc.perform(get("/rooms/" + savedRoom.getId() + "/check")) + .andExpect(status().isOk()) + .andDo(print()); + } + + @DisplayName("참여중이지 않은 방의 정보 불러오기 성공") + @Test + void get_un_joined_room_details() throws Exception { + // given + Room room = RoomFixture.room("테스트 방", NIGHT, 21); + Room savedRoom = roomRepository.save(room); + + Member member1 = MemberFixture.member("901010", "testtest"); + member1 = memberRepository.save(member1); + + Item item = ItemFixture.nightMageSkin(); + + Inventory inventory = InventoryFixture.inventory(member1.getId(), item); + inventory.select(); + + itemRepository.save(item); + inventoryRepository.save(inventory); + + Participant participant = RoomFixture.participant(savedRoom, member1.getId()); + participantRepository.save(participant); + + Routine routine1 = RoomFixture.routine(savedRoom, "물 마시기"); + Routine routine2 = RoomFixture.routine(savedRoom, "커피 마시기"); + routineRepository.saveAll(List.of(routine1, routine2)); + + DailyMemberCertification dailyMemberCertification = RoomFixture.dailyMemberCertification(member1.getId(), + savedRoom.getId(), participant); + dailyMemberCertificationRepository.save(dailyMemberCertification); + + // expected + mockMvc.perform(get("/rooms/" + savedRoom.getId() + "/un-joined")) + .andExpect(status().isOk()) + .andDo(print()); + } + + @DisplayName("참여중인 방에 대한 확인 성공") + @WithMember + @Test + void check_if_participant_true_success() throws Exception { + // given + Room room = RoomFixture.room(); + Room savedRoom = roomRepository.save(room); + + Participant participant = RoomFixture.participant(room, 1L); + participantRepository.save(participant); + + // expected + mockMvc.perform(get("/rooms/" + savedRoom.getId() + "/check")) + .andExpect(status().isOk()) + .andDo(print()); + } + @DisplayName("아침, 저녁 방 전체 조회 성공 - 첫 번째 조회, 다음 페이지 있음") @WithMember(id = 1L) @Test