Skip to content

Commit

Permalink
Merge pull request #164 from KUIT-Space/refactor/#154/채팅방-서비스-리팩토링
Browse files Browse the repository at this point in the history
Refactor/#154: 채팅방 서비스 리팩토링
  • Loading branch information
hyunn522 authored Nov 5, 2024
2 parents 8822b5f + 750a00f commit 595e7ad
Show file tree
Hide file tree
Showing 23 changed files with 698 additions and 670 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.handler.annotation.*;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import space.space_spring.argumentResolver.userSpace.CheckUserSpace;
import space.space_spring.dto.chat.request.ChatMessageRequest;
import space.space_spring.dto.chat.response.ChatMessageLogResponse;
import space.space_spring.dto.chat.response.ChatMessageResponse;
import space.space_spring.service.ChattingService;
import space.space_spring.service.UserChatRoomService;

import java.io.IOException;
import java.util.Map;
Expand All @@ -27,7 +21,6 @@ public class ChattingController {

private final ChattingService chattingService;

private final UserChatRoomService userChatRoomService;

@MessageMapping("/chat/{chatRoomId}") // {chatRoomId} 채팅방으로 보낸 메세지 매핑
@SendTo("/topic/chat/{chatRoomId}") // {chatRoomId} 채팅방을 구독한 곳들로 메세지 전송
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/space/space_spring/dao/chat/ChatRoomDao.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/space/space_spring/dao/chat/ChatRoomRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package space.space_spring.dao.chat;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import space.space_spring.dao.chat.custom.ChatRoomRepositoryCustom;
import space.space_spring.entity.ChatRoom;
import space.space_spring.entity.enumStatus.BaseStatusType;

@Repository
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long>, ChatRoomRepositoryCustom {
ChatRoom findByIdAndStatus(Long chatRoomId, BaseStatusType status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

@Repository
public interface ChattingDao extends MongoRepository<ChatMessage, String> {
public interface ChattingRepository extends MongoRepository<ChatMessage, String> {
List<ChatMessage> findByChatRoomId(Long chatRoomId);

ChatMessage findTopByChatRoomIdOrderByCreatedAtDesc(Long chatRoomId);
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/space/space_spring/dao/chat/UserChatRoomDao.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package space.space_spring.dao.chat;

import org.springframework.data.jpa.repository.JpaRepository;
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 UserChatRoomRepository extends JpaRepository<UserChatRoom, Long> {
UserChatRoom findByUserAndChatRoomAndStatus(User userByUserId, ChatRoom chatRoomByChatRoomId, BaseStatusType status);

List<UserChatRoom> findByChatRoomAndStatus(ChatRoom chatRoom, BaseStatusType status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

import java.util.List;

public interface ChatRoomDaoCustom {
public interface ChatRoomRepositoryCustom {
List<ChatRoom> findByUserAndSpace(User user, Space space);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
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;

@RequiredArgsConstructor
public class ChatRoomDaoImpl implements ChatRoomDaoCustom {
public class ChatRoomRepositoryImpl implements ChatRoomRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Override
public List<ChatRoom> findByUserAndSpace(User who, Space where) {
return jpaQueryFactory
.selectFrom(chatRoom)
.join(userChatRoom).on(userChatRoom.chatRoom.eq(chatRoom))
.where(userChatRoom.user.eq(who)
.and(chatRoom.space.eq(where)))
.join(userChatRoom).on(
userChatRoom.chatRoom.eq(chatRoom)
.and(userChatRoom.user.eq(who))
.and(userChatRoom.status.eq(BaseStatusType.ACTIVE)))
.where(chatRoom.space.eq(where)
.and(chatRoom.status.eq(BaseStatusType.ACTIVE)))
.orderBy(chatRoom.lastModifiedAt.desc())
.fetch();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package space.space_spring.dto.chat.dto;

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.HashMap;

@Getter
@Builder
public class LastMessageInfoDto {

private LocalDateTime lastUpdateTime;

private HashMap<String, String> lastContent;
}

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; }
}
3 changes: 3 additions & 0 deletions src/main/java/space/space_spring/entity/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;
import java.time.ZoneId;

@Entity
@Getter
@Builder
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/space/space_spring/entity/UserChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public static UserChatRoom of(ChatRoom chatRoom, User user, LocalDateTime lastRe
public void setLastReadTime(LocalDateTime lastReadTime) {
this.lastReadTime = lastReadTime;
}

public void setUserRejoin() {
this.setLastReadTime(LocalDateTime.now());
this.updateActive();
}
}
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;
}
Empty file.
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions src/main/java/space/space_spring/global/util/TimeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package space.space_spring.global.util;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;

@Component
public class TimeUtils {

/**
* UTC 시간을 서울 시간으로 변환
*/
public LocalDateTime getEncodedTime(LocalDateTime originalTime) {
return originalTime
.atZone(ZoneId.of("UTC"))
.withZoneSameInstant(ZoneId.of("Asia/Seoul"))
.toLocalDateTime();
}
}
Loading

0 comments on commit 595e7ad

Please sign in to comment.