From f53c12317c659d7f8b389a4c6a5a3c0437776398 Mon Sep 17 00:00:00 2001 From: ihun-il Date: Wed, 4 Dec 2024 21:42:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20No=20offset=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9D=98=20=EC=B1=84=ED=8C=85=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/repository/ChatCustomRepository.java | 9 ++++ .../repository/ChatCustomRepositoryImpl.java | 52 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/main/java/com/palettee/chat/repository/ChatCustomRepository.java create mode 100644 src/main/java/com/palettee/chat/repository/ChatCustomRepositoryImpl.java diff --git a/src/main/java/com/palettee/chat/repository/ChatCustomRepository.java b/src/main/java/com/palettee/chat/repository/ChatCustomRepository.java new file mode 100644 index 0000000..425fa9f --- /dev/null +++ b/src/main/java/com/palettee/chat/repository/ChatCustomRepository.java @@ -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); +} diff --git a/src/main/java/com/palettee/chat/repository/ChatCustomRepositoryImpl.java b/src/main/java/com/palettee/chat/repository/ChatCustomRepositoryImpl.java new file mode 100644 index 0000000..1226f86 --- /dev/null +++ b/src/main/java/com/palettee/chat/repository/ChatCustomRepositoryImpl.java @@ -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 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); + } +}