Skip to content

Commit

Permalink
revise: search chatrooms using banned, exploded condition
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed May 21, 2024
1 parent 8c06a87 commit 3a27685
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
13 changes: 10 additions & 3 deletions src/main/java/toy/bookchat/bookchat/domain/chatroom/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import javax.persistence.ManyToOne;
import lombok.Builder;
import lombok.Getter;
import org.hibernate.annotations.DynamicInsert;
import toy.bookchat.bookchat.domain.BaseEntity;
import toy.bookchat.bookchat.domain.book.Book;
import toy.bookchat.bookchat.domain.user.User;

@Entity
@Getter
@Entity
@DynamicInsert
public class ChatRoom extends BaseEntity {

@Id
Expand All @@ -29,10 +31,10 @@ public class ChatRoom extends BaseEntity {
private Book book;
@ManyToOne(fetch = FetchType.LAZY)
private User host;
private Boolean isDeleted;

@Builder
private ChatRoom(Long id, String roomName, String roomSid, Integer roomSize,
Integer defaultRoomImageType, String roomImageUri, Book book, User host) {
private ChatRoom(Long id, String roomName, String roomSid, Integer roomSize, Integer defaultRoomImageType, String roomImageUri, Book book, User host, Boolean isDeleted) {
this.id = id;
this.roomName = roomName;
this.roomSid = roomSid;
Expand All @@ -41,6 +43,7 @@ private ChatRoom(Long id, String roomName, String roomSid, Integer roomSize,
this.roomImageUri = roomImageUri;
this.book = book;
this.host = host;
this.isDeleted = isDeleted;
}

protected ChatRoom() {
Expand Down Expand Up @@ -73,4 +76,8 @@ public void changeRoomSize(Integer roomSize) {
public void changeRoomImageUri(String roomImageUri) {
this.roomImageUri = roomImageUri;
}

public void explode() {
this.isDeleted = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public UserChatRoomDetailResponse getUserChatRoomDetails(@PathVariable Long room
}

@GetMapping("/chatrooms")
public ChatRoomsResponseSlice getChatRooms(@ModelAttribute ChatRoomRequest chatRoomRequest, Pageable pageable) {
public ChatRoomsResponseSlice getChatRooms(@ModelAttribute ChatRoomRequest chatRoomRequest, @UserPayload TokenPayload tokenPayload, Pageable pageable) {
chatRoomRequest.validate();
return chatRoomService.getChatRooms(chatRoomRequest, pageable);
return chatRoomService.getChatRooms(tokenPayload.getUserId(), chatRoomRequest, pageable);
}

@GetMapping("/chatrooms/{roomId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ChatRoomQueryRepository {

Slice<UserChatRoomResponse> findUserChatRoomsWithLastChat(Pageable pageable, Long bookId, Long postCursorId, Long userId);

Slice<ChatRoomResponse> findChatRooms(ChatRoomRequest chatRoomRequest, Pageable pageable);
Slice<ChatRoomResponse> findChatRooms(Long userId, ChatRoomRequest chatRoomRequest, Pageable pageable);

ChatRoomDetails findChatRoomDetails(Long roomId, Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import static toy.bookchat.bookchat.domain.book.QBook.book;
import static toy.bookchat.bookchat.domain.chat.QChat.chat;
import static toy.bookchat.bookchat.domain.chatroom.QChatRoom.chatRoom;
import static toy.bookchat.bookchat.domain.chatroom.QChatRoomBlockedUser.chatRoomBlockedUser;
import static toy.bookchat.bookchat.domain.chatroom.QChatRoomHashTag.chatRoomHashTag;
import static toy.bookchat.bookchat.domain.chatroom.QHashTag.hashTag;
import static toy.bookchat.bookchat.domain.common.RepositorySupport.toSlice;
import static toy.bookchat.bookchat.domain.participant.QParticipant.participant;
import static toy.bookchat.bookchat.domain.user.QUser.user;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
Expand Down Expand Up @@ -123,7 +125,7 @@ private BooleanExpression eqBookId(Long bookId) {
}

@Override
public Slice<ChatRoomResponse> findChatRooms(ChatRoomRequest chatRoomRequest,
public Slice<ChatRoomResponse> findChatRooms(Long userId, ChatRoomRequest chatRoomRequest,
Pageable pageable) {
QChat subChat = new QChat("subChat");
QChatRoomHashTag subChatRoomHashTag = new QChatRoomHashTag("subChatRoomHashTag");
Expand All @@ -149,7 +151,10 @@ public Slice<ChatRoomResponse> findChatRooms(ChatRoomRequest chatRoomRequest,
chat.user.id,
chat.id,
chat.message,
chat.createdAt))
chat.createdAt,
participant.id.isNotNull(),
chatRoomBlockedUser.id.isNotNull()
))
.from(chatRoom)
.join(user).on(chatRoom.host.id.eq(user.id))
.join(book).on(chatRoom.book.id.eq(book.id))
Expand All @@ -159,13 +164,16 @@ public Slice<ChatRoomResponse> findChatRooms(ChatRoomRequest chatRoomRequest,
.where(subChatRoomHashTag.chatRoom.id.eq(chatRoom.id),
inTags(chatRoomRequest.getTags()))))
.join(hashTag).on(hashTag.id.eq(chatRoomHashTag.hashTag.id))
.leftJoin(participant).on(chatRoom.id.eq(participant.chatRoom.id).and(participant.user.id.eq(userId)))
.leftJoin(chatRoomBlockedUser).on(chatRoomBlockedUser.chatRoom.id.eq(chatRoom.id).and(chatRoomBlockedUser.user.id.eq(userId)))
.leftJoin(chat).on(chat.id.in(
JPAExpressions.select(subChat.id.max())
.from(subChat)
.groupBy(subChat.chatRoom)
.having(subChat.chatRoom.id.eq(chatRoom.id))))
.groupBy(chatRoom.id, chat.id)
.where(chat.chatRoom.id.eq(chatRoom.id),
notExplodedChatRoom(),
ltCursorId(chatRoomRequest.getPostCursorId()),
eqIsbn(chatRoomRequest.getIsbn()),
containsTitle(chatRoomRequest.getTitle()),
Expand All @@ -190,6 +198,10 @@ public Slice<ChatRoomResponse> findChatRooms(ChatRoomRequest chatRoomRequest,
return toSlice(contents, pageable);
}

private Predicate notExplodedChatRoom() {
return chatRoom.isDeleted.isFalse();
}

private BooleanExpression ltCursorId(Long cursorId) {
return cursorId == null ? null : chatRoom.id.lt(cursorId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@EqualsAndHashCode
public class ChatRoomResponse {

Expand All @@ -29,13 +31,15 @@ public class ChatRoomResponse {
private Long lastChatId;
private String lastChatMessage;
private LocalDateTime lastChatDispatchTime;
private Boolean isEntered;
private Boolean isBanned;

@Builder
public ChatRoomResponse(Long roomId, String roomName, String roomSid,
String bookTitle, String bookCoverImageUri, List<String> bookAuthors, Long hostId, String hostName,
Integer hostDefaultProfileImageType, String hostProfileImageUri, Long roomMemberCount,
Integer roomSize, Integer defaultRoomImageType,
String roomImageUri, String tags, Long lastChatSenderId, Long lastChatId, String lastChatMessage, LocalDateTime lastChatDispatchTime) {
String roomImageUri, String tags, Long lastChatSenderId, Long lastChatId, String lastChatMessage, LocalDateTime lastChatDispatchTime, Boolean isEntered, Boolean isBanned) {
this.roomId = roomId;
this.roomName = roomName;
this.roomSid = roomSid;
Expand All @@ -55,14 +59,16 @@ public ChatRoomResponse(Long roomId, String roomName, String roomSid,
this.lastChatId = lastChatId;
this.lastChatMessage = lastChatMessage;
this.lastChatDispatchTime = lastChatDispatchTime;
this.isEntered = isEntered;
this.isBanned = isBanned;
}

@Builder
public ChatRoomResponse(Long roomId, String roomName, String roomSid,
String bookTitle, String bookCoverImageUri, Long hostId, String hostName,
Integer hostDefaultProfileImageType, String hostProfileImageUri, Long roomMemberCount,
Integer roomSize, Integer defaultRoomImageType,
String roomImageUri, String tags, Long lastChatSenderId, Long lastChatId, String lastChatMessage, LocalDateTime lastChatDispatchTime) {
String roomImageUri, String tags, Long lastChatSenderId, Long lastChatId, String lastChatMessage, LocalDateTime lastChatDispatchTime, Boolean isEntered, Boolean isBanned) {
this.roomId = roomId;
this.roomName = roomName;
this.roomSid = roomSid;
Expand All @@ -81,6 +87,8 @@ public ChatRoomResponse(Long roomId, String roomName, String roomSid,
this.lastChatId = lastChatId;
this.lastChatMessage = lastChatMessage;
this.lastChatDispatchTime = lastChatDispatchTime;
this.isEntered = isEntered;
this.isBanned = isBanned;
}

public void setBookAuthors(List<String> bookAuthors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public UserChatRoomDetailResponse getUserChatRoomDetails(Long roomId, Long userI
}

@Transactional(readOnly = true)
public ChatRoomsResponseSlice getChatRooms(ChatRoomRequest chatRoomRequest, Pageable pageable) {
return ChatRoomsResponseSlice.of(chatRoomRepository.findChatRooms(chatRoomRequest, pageable));
public ChatRoomsResponseSlice getChatRooms(Long userId, ChatRoomRequest chatRoomRequest, Pageable pageable) {
return ChatRoomsResponseSlice.of(chatRoomRepository.findChatRooms(userId, chatRoomRequest, pageable));
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -223,9 +223,7 @@ public void exitChatRoom(Long userId, Long roomId) {
ChatRoom chatRoom = participant.getChatRoom();

if (user == chatRoom.getHost()) {
chatRepository.deleteByChatRoom(chatRoom);
participantRepository.deleteByChatRoom(chatRoom);
chatRoomRepository.delete(chatRoom);
chatRoom.explode();

messagePublisher.sendNotificationMessage(chatRoom.getRoomSid(), NotificationMessage.createHostExitMessage());
return;
Expand Down

0 comments on commit 3a27685

Please sign in to comment.