Skip to content

Commit

Permalink
feat: No offset 방식의 채팅 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
Leehunil committed Dec 4, 2024
1 parent e718870 commit f53c123
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.palettee.chat.repository;

import com.palettee.chat.controller.dto.response.ChatCustomResponse;

import java.time.LocalDateTime;

public interface ChatCustomRepository {
ChatCustomResponse findChatNoOffset(Long chatRoomId, int size, LocalDateTime lastTimeStamp);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.palettee.chat.repository;

import com.palettee.chat.controller.dto.response.ChatCustomResponse;
import com.palettee.chat.domain.Chat;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

import static com.palettee.chat.domain.QChat.*;
import static com.palettee.chat.domain.QChatImage.*;
import static com.palettee.chat_room.domain.QChatRoom.*;
import static com.palettee.user.domain.QUser.*;

@Repository
public class ChatCustomRepositoryImpl implements ChatCustomRepository{
private final JPAQueryFactory queryFactory;

public ChatCustomRepositoryImpl(JPAQueryFactory queryFactory) {
this.queryFactory = queryFactory;
}

@Override
public ChatCustomResponse findChatNoOffset(Long chatRoomId, int size, LocalDateTime sendAt) {
List<Chat> chats = queryFactory
.selectFrom(chat)
.where(
chat.chatRoom.id.eq(chatRoomId),
sendAt != null ?
chat.sendAt.lt(sendAt) : null
)
.leftJoin(chat.chatImages, chatImage).fetchJoin()
.leftJoin(chat.user, user).fetchJoin()
.leftJoin(chat.chatRoom, chatRoom).fetchJoin()
.orderBy(chat.sendAt.desc())
.limit(size + 1)
.fetch();

boolean hasNext = chats.size() > size;

LocalDateTime lastSendAt = null;
if(hasNext) {
if(size != 0) {
lastSendAt = chats.get(size-1).getSendAt();
}
chats = chats.subList(0, size);
}

return ChatCustomResponse.toResponseFromEntity(chats, hasNext, lastSendAt);
}
}

0 comments on commit f53c123

Please sign in to comment.