Skip to content

Commit

Permalink
Merge pull request #51 from catchroom/develop
Browse files Browse the repository at this point in the history
Feat : 채팅내역불러오기 Paging 처리
  • Loading branch information
HyemIin authored Jan 24, 2024
2 parents 922abce + 8ea0627 commit 2c7e9ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +25,7 @@
public class ChatMongoService {

private final ChatMessageRepository chatMessageRepository;
private final MongoTemplate mongoTemplate;

// 채팅 저장
@Transactional
Expand All @@ -33,19 +42,35 @@ public ChatMessageDto save(ChatMessageDto chatMessageDto) {

// 채팅 불러오기
@Transactional(readOnly = true)
public List<ChatMessageDto> findAll(String roomId) {
List<ChatMessageDto> chatMessageList =
chatMessageRepository.findAllByRoomId(roomId)
public List<ChatMessageDto> findAll(String roomId,Integer pageNumber) {
return findByRoomIdWithPaging(roomId,pageNumber,20)
.stream().map(ChatMessageDto::fromEntity)
.collect(Collectors.toList());
return chatMessageList;
}

@Transactional
public void deleteRoomId(String roomId) {
chatMessageRepository.deleteAllByRoomId(roomId);
}

private Page<ChatMessage> 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<ChatMessage> filteredChatMessage = mongoTemplate.find(query, ChatMessage.class, "chat");
return PageableExecutionUtils.getPage(
filteredChatMessage,
pageable,
() -> mongoTemplate.count(query.skip(-1).limit(-1), ChatMessage.class)
);
}



}

0 comments on commit 2c7e9ba

Please sign in to comment.