-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/palettee/chat/repository/ChatCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/palettee/chat/repository/ChatCustomRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |