Skip to content

Commit

Permalink
refactor: BaseStatus의 status 상수화
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunn522 committed Oct 30, 2024
1 parent a9f2ec6 commit 9268500
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/main/java/space/space_spring/dao/chat/ChatRoomDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import org.springframework.stereotype.Repository;
import space.space_spring.dao.chat.custom.ChatRoomDaoCustom;
import space.space_spring.entity.ChatRoom;
import space.space_spring.entity.enumStatus.BaseStatusType;

@Repository
public interface ChatRoomDao extends JpaRepository<ChatRoom, Long>, ChatRoomDaoCustom {
ChatRoom findByIdAndStatus(Long chatRoomId, String status);
ChatRoom findByIdAndStatus(Long chatRoomId, BaseStatusType status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import space.space_spring.entity.ChatRoom;
import space.space_spring.entity.User;
import space.space_spring.entity.UserChatRoom;
import space.space_spring.entity.enumStatus.BaseStatusType;

import java.util.List;


public interface UserChatRoomDao extends JpaRepository<UserChatRoom, Long> {
UserChatRoom findByUserAndChatRoomAndStatus(User userByUserId, ChatRoom chatRoomByChatRoomId, String status);
UserChatRoom findByUserAndChatRoomAndStatus(User userByUserId, ChatRoom chatRoomByChatRoomId, BaseStatusType status);

List<UserChatRoom> findByChatRoomAndStatus(ChatRoom chatRoom, String status);
List<UserChatRoom> findByChatRoomAndStatus(ChatRoom chatRoom, BaseStatusType status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import space.space_spring.entity.User;

import java.util.List;
import space.space_spring.entity.enumStatus.BaseStatusType;

import static space.space_spring.entity.QChatRoom.chatRoom;
import static space.space_spring.entity.QUserChatRoom.userChatRoom;
Expand All @@ -23,9 +24,9 @@ public List<ChatRoom> findByUserAndSpace(User who, Space where) {
.join(userChatRoom).on(
userChatRoom.chatRoom.eq(chatRoom)
.and(userChatRoom.user.eq(who))
.and(userChatRoom.status.eq("ACTIVE")))
.and(userChatRoom.status.eq(BaseStatusType.ACTIVE)))
.where(chatRoom.space.eq(where)
.and(chatRoom.status.eq("ACTIVE")))
.and(chatRoom.status.eq(BaseStatusType.ACTIVE)))
.orderBy(chatRoom.lastModifiedAt.desc())
.fetch();
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/space/space_spring/entity/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;

import java.time.LocalDateTime;
import space.space_spring.entity.enumStatus.BaseStatusType;

@Getter
@MappedSuperclass
Expand All @@ -17,14 +18,15 @@ public abstract class BaseEntity {
@Column(name = "updated_date")
private LocalDateTime lastModifiedAt;

@Enumerated(EnumType.STRING)
@Column(name = "status")
private String status;
private BaseStatusType status;

@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
lastModifiedAt = LocalDateTime.now();
status = "ACTIVE";
status = BaseStatusType.ACTIVE;
}

@PreUpdate
Expand All @@ -36,8 +38,8 @@ protected void initializeBaseEntityFields() {
onCreate();
}

public void updateActive() { this.status = "ACTIVE"; }
public void updateActive() { this.status = BaseStatusType.ACTIVE; }


public void updateInactive() { this.status = "INACTIVE"; }
public void updateInactive() { this.status = BaseStatusType.INACTIVE; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package space.space_spring.entity.enumStatus;

import lombok.Getter;

@Getter
public enum BaseStatusType {
ACTIVE,
INACTIVE;
}
30 changes: 17 additions & 13 deletions src/main/java/space/space_spring/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import space.space_spring.entity.*;
import space.space_spring.entity.document.ChatMessage;

import space.space_spring.entity.enumStatus.BaseStatusType;
import space.space_spring.exception.CustomException;
import space.space_spring.util.space.SpaceUtils;
import space.space_spring.util.user.UserUtils;
Expand Down Expand Up @@ -94,7 +95,7 @@ public ReadChatRoomMemberResponse readChatRoomMembers(Long spaceId, Long chatRoo
Space spaceById = spaceUtils.findSpaceBySpaceId(spaceId);

// TODO 2: chatRoomId에 해당하는 chatRoom find
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, "ACTIVE"))
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, BaseStatusType.ACTIVE))
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));

// TODO 3: 해당 userChatRoom의 user들 find
Expand All @@ -109,11 +110,11 @@ public ChatSuccessResponse updateLastReadTime(Long userId, Long chatRoomId) {
User userByUserId = userUtils.findUserByUserId(userId);

// TODO 2: chatRoomId에 해당하는 chatRoom find
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, "ACTIVE"))
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, BaseStatusType.ACTIVE))
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));

// TODO 3: 해당 user와 chatRoom에 대한 userChatRoom find
UserChatRoom targetChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, "ACTIVE");
UserChatRoom targetChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE);

// TODO 4: userChatRoom의 마지막으로 읽은 시간 저장
targetChatRoom.setLastReadTime(LocalDateTime.now());
Expand All @@ -128,7 +129,7 @@ public ChatSuccessResponse joinChatRoom(Long chatRoomId, JoinChatRoomRequest joi
List<Long> memberIdList = joinChatRoomRequest.getMemberList();

// TODO 1: chatRoomId에 해당하는 chatRoom find
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, "ACTIVE"))
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, BaseStatusType.ACTIVE))
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));

// TODO 2: 유저들의 채팅방 초대 이력에 따른 재초대
Expand All @@ -137,9 +138,10 @@ public ChatSuccessResponse joinChatRoom(Long chatRoomId, JoinChatRoomRequest joi
return ChatSuccessResponse.of(true);
}

@Transactional
public ChatSuccessResponse modifyChatRoomName(Long chatRoomId, String name) {
// TODO 1: chatRoomId에 해당하는 chatRoom find
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, "ACTIVE"))
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, BaseStatusType.ACTIVE))
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));

// TODO 2: 채팅방 이름 변경
Expand All @@ -149,16 +151,17 @@ public ChatSuccessResponse modifyChatRoomName(Long chatRoomId, String name) {
return ChatSuccessResponse.of(true);
}

@Transactional
public ChatSuccessResponse exitChatRoom(Long userId, Long chatRoomId) {
// TODO 1: userId에 해당하는 user find
User userByUserId = userUtils.findUserByUserId(userId);

// TODO 2: chatRoomId에 해당하는 chatRoom find
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, "ACTIVE"))
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, BaseStatusType.ACTIVE))
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));

// TODO 3: 해당 user와 chatRoom에 대한 userChatRoom find
UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, "ACTIVE");
UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE);

// TODO 4: 해당 userChatRoom inactive로 변경
userChatRoom.updateInactive();
Expand All @@ -167,9 +170,10 @@ public ChatSuccessResponse exitChatRoom(Long userId, Long chatRoomId) {
return ChatSuccessResponse.of(true);
}

@Transactional
public ChatSuccessResponse deleteChatRoom(Long chatRoomId) {
// TODO 1: chatRoomId에 해당하는 chatRoom find
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, "ACTIVE"))
ChatRoom chatRoomByChatRoomId = Optional.ofNullable(chatRoomDao.findByIdAndStatus(chatRoomId, BaseStatusType.ACTIVE))
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));

// TODO 2: 해당 chatRoom inactive로 변경
Expand All @@ -180,7 +184,7 @@ public ChatSuccessResponse deleteChatRoom(Long chatRoomId) {
}

private boolean isUserInChatRoom(User userByUserId, ChatRoom chatRoomByChatRoomId) {
List<UserChatRoom> chatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoomByChatRoomId, "ACTIVE");
List<UserChatRoom> chatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoomByChatRoomId, BaseStatusType.ACTIVE);
return chatRoomList.stream().anyMatch(userChatRoom -> userChatRoom.getUser().equals(userByUserId));
}

Expand All @@ -195,7 +199,7 @@ private LastMessageInfoDto getLastMsgInfo(ChatRoom chatRoom) {
}

private int calculateUnreadMsgCount(User userByUserId, ChatRoom chatRoom, LocalDateTime lastUpdateTime) {
UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoom, "ACTIVE");
UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoom, BaseStatusType.ACTIVE);
LocalDateTime lastReadTime = userChatRoom.getEncodedTime(); // LocalDateTime으로 변환
log.info("마지막으로 읽은 시간: " + lastReadTime);

Expand All @@ -211,7 +215,7 @@ private int calculateUnreadMsgCount(User userByUserId, ChatRoom chatRoom, LocalD

private List<UserInfoInSpace> getUserInChatRoom(Space space, ChatRoom chatRoom) {
List<UserInfoInSpace> userList = new ArrayList<>();
List<UserChatRoom> userChatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoom, "ACTIVE");
List<UserChatRoom> userChatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoom, BaseStatusType.ACTIVE);

userChatRoomList.forEach(userChatRoom -> {
User user = userChatRoom.getUser();
Expand All @@ -234,8 +238,8 @@ private void handleJoinChatRoom(List<Long> memberIdList, ChatRoom chatRoomByChat
// TODO 1: 유저가 채팅방에 초대된 적이 있는 경우 다시 초대
if (isUserInChatRoom(userByUserId, chatRoomByChatRoomId)) {
// TODO 2: 유저가 채팅방에 초대되었다가 나간 경우 다시 초대
if (userByUserId.getStatus().equals("INACTIVE")) {
UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, "ACTIVE");
if (userByUserId.getStatus().equals(BaseStatusType.INACTIVE)) {
UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE);
userChatRoom.setUserRejoin();
userChatRoomDao.save(userChatRoom);
continue;
Expand Down

0 comments on commit 9268500

Please sign in to comment.