diff --git a/src/main/java/com/catchroom/chat/message/controller/ChatFindController.java b/src/main/java/com/catchroom/chat/message/controller/ChatFindController.java index af2ee3b..4628cd3 100644 --- a/src/main/java/com/catchroom/chat/message/controller/ChatFindController.java +++ b/src/main/java/com/catchroom/chat/message/controller/ChatFindController.java @@ -23,10 +23,11 @@ public class ChatFindController { @GetMapping("/room/find") public ResponseEntity roomFindInfo( @RequestHeader("Authorization") String accessToken, - @RequestParam(name = "id") String id + @RequestParam(name = "id") String id, + @RequestParam(name = "page") Integer pageNumber ) { return ResponseEntity.ok( - ApiResponse.create(6003, chatMongoService.findAll(id)) + ApiResponse.create(6003, chatMongoService.findAll(id,pageNumber)) ); } diff --git a/src/main/java/com/catchroom/chat/message/dto/ChatMessageDto.java b/src/main/java/com/catchroom/chat/message/dto/ChatMessageDto.java index 9de7f25..f831ff7 100644 --- a/src/main/java/com/catchroom/chat/message/dto/ChatMessageDto.java +++ b/src/main/java/com/catchroom/chat/message/dto/ChatMessageDto.java @@ -2,13 +2,9 @@ import com.catchroom.chat.message.entity.ChatMessage; import com.catchroom.chat.message.type.MessageType; +import lombok.*; + import java.io.Serializable; -import java.time.LocalDateTime; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; @Builder @Getter diff --git a/src/main/java/com/catchroom/chat/message/service/ChatMongoService.java b/src/main/java/com/catchroom/chat/message/service/ChatMongoService.java index 6ccb485..1069a52 100644 --- a/src/main/java/com/catchroom/chat/message/service/ChatMongoService.java +++ b/src/main/java/com/catchroom/chat/message/service/ChatMongoService.java @@ -8,6 +8,14 @@ import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -17,6 +25,7 @@ public class ChatMongoService { private final ChatMessageRepository chatMessageRepository; + private final MongoTemplate mongoTemplate; // 채팅 저장 @Transactional @@ -33,12 +42,10 @@ public ChatMessageDto save(ChatMessageDto chatMessageDto) { // 채팅 불러오기 @Transactional(readOnly = true) - public List findAll(String roomId) { - List chatMessageList = - chatMessageRepository.findAllByRoomId(roomId) + public List findAll(String roomId,Integer pageNumber) { + return findByRoomIdWithPaging(roomId,pageNumber,20) .stream().map(ChatMessageDto::fromEntity) .collect(Collectors.toList()); - return chatMessageList; } @Transactional @@ -46,6 +53,24 @@ public void deleteRoomId(String roomId) { chatMessageRepository.deleteAllByRoomId(roomId); } + private Page findByRoomIdWithPaging(String roomId, int page, int size) { + Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.DESC,"time")); + + Query query = new Query() + .with(pageable) + .skip((long) pageable.getPageSize() * pageable.getPageNumber()) + .limit(pageable.getPageSize()); + + query.addCriteria(Criteria.where("roomId").is(roomId)); + + List filteredChatMessage = mongoTemplate.find(query, ChatMessage.class, "chat"); + return PageableExecutionUtils.getPage( + filteredChatMessage, + pageable, + () -> mongoTemplate.count(query.skip(-1).limit(-1), ChatMessage.class) + ); + } + }