Skip to content
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

[dev] Offset, Limit 채팅 레코드 설정 #357

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public ResponseEntity<?> findChatRecords(@RequestParam("roomId") Long roomId) {
String userId = userContext.getUserId();
List<ChatRecordDTO> records = chatService.findAllByRoomId(roomId);
ChatRequest.ChatRecordDTOsWithUser response = ChatRecordDTOsWithUser.builder()
.userName(userService.findById(userId).getUserName())
.records(records)
.userId(userId)
.userName(userService.findById(userId).getUserName())
.build();

return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package chatting.chat.domain.chat.consts;

/**
* 채팅 기록에 대한 상수를 정의한 클래스입니다.
*/
public class ChatConst {
// 채팅 기록을 가져올 때 최대로 가져올 수 있는 개수.
public static final Integer maxChatRecordPage = 1000;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chatting.chat.domain.chat.repository;

import chatting.chat.domain.chat.entity.Chatting;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -14,6 +16,8 @@ public interface ChatRepository extends JpaRepository<Chatting, String> {
@Query("select c from Chatting c where c.room.roomId = :roomId")
List<Chatting> findAllByRoomId(@Param("roomId") Long roomId); // in asc order by its date and time

@Query("SELECT c FROM Chatting c WHERE c.room.roomId = :roomId")
Page<Chatting> findAllByRoomId(Long roomId, Pageable pageable);


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package chatting.chat.domain.chat.service;


import chatting.chat.domain.chat.consts.ChatConst;
import chatting.chat.domain.chat.entity.Chatting;
import chatting.chat.domain.chat.repository.ChatRepository;
import chatting.chat.domain.participant.entity.Participant;
import chatting.chat.domain.room.entity.Room;
import chatting.chat.domain.participant.repository.ParticipantRepository;
import chatting.chat.domain.room.repository.RoomRepository;
Expand All @@ -12,29 +12,28 @@
import chatting.chat.web.kafka.dto.RequestAddChatMessageDTO;
import com.example.commondto.dto.chat.ChatRequest.ChatRecordDTO;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static com.example.commondto.error.ErrorCode.*;

import com.example.commondto.error.CustomException;
import com.example.commondto.error.ErrorCode;
import com.example.commondto.error.ErrorResponse;
import com.example.commondto.error.AppException;

@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ChatService {


private final ChatRepository chatRepository;
private final RoomRepository roomRepository;
private final UserRepository userRepository;
Expand All @@ -52,8 +51,10 @@ public class ChatService {
public List<ChatRecordDTO> findAllByRoomId(Long roomId) {
roomRepository.findById(roomId)
.orElseThrow(() -> new CustomException(CANNOT_FIND_ROOM));
return chatRepository.findAllByRoomId(roomId).stream().map(
Chatting::toChatRecordDTO).collect(Collectors.toList());
PageRequest pr = PageRequest.of(0, ChatConst.maxChatRecordPage,
Sort.by("createdAt").descending());
return chatRepository.findAllByRoomId(roomId, pr).map(Chatting::toChatRecordDTO)
.toList();
}

public ChatRecordDTO findById(String id) {
Expand All @@ -67,10 +68,12 @@ public void saveAll(List<Chatting> chattings) {

/**
* 채팅을 저장하는 메소드입니다.
* <br>채팅을 저장하기 전에 해당 유저가 해당 채팅방에 참여중인지 확인합니다. 참여중이지 않다면 예외를 발생시킵니다. {@link ErrorCode#INVALID_PARTICIPANT}
* <br>채팅을 저장하기 전에 해당 유저가 해당 채팅방에 참여중인지 확인합니다. 참여중이지 않다면 예외를 발생시킵니다.
* {@link ErrorCode#INVALID_PARTICIPANT}
* <br>채팅방이 존재하지 않는다면 예외를 발생시킵니다. {@link ErrorCode#CANNOT_FIND_ROOM}
* <br>채팅을 전송하는 유저가 존재하지 않는다면 예외를 발생시킵니다. {@link ErrorCode#CANNOT_FIND_USER}
* @param req {@link RequestAddChatMessageDTO}
*
* @param req {@link RequestAddChatMessageDTO}
* @param userId
* @return {@link ChatRecordDTO}
* @throws CustomException
Expand Down