-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: 읽은 메시지 로그 도메인 생성 * feat: 읽은 메시지 로그 저장 레포지토리 생성 * refactor: 컬럼명 변경 * feat: 메시지 조회 시 마지막으로 읽은 메시지 로그 저장 * feat: 채팅방 목록 조회 시 읽지 않은 메시지 개수를 포함하는 기능 추가 * feat: 채팅방 생성 시 참여자에 대한 메시지 로그 생성하는 기능 추가 * feat: 경매 아이디로 채팅방 조회하는 기능 추가 * feat: 채팅방 목록 조회 시 반환값에 안 읽은 메시지 개수 추가 * refactor: 메시지 로그 조회 네이밍 변경 * feat: 어노테이션 추가 * feat: 로그 찾지 못한 경우에 대한 커스텀 예외 추가 * refactor: 로그 생성 로직 이벤트로 분리 * refactor: 불필요한 메서드 삭제 * test: 생략한 테스트 추가 * refactor: 채팅방과 메시지 조회 로그cascade type 지정 * refactor: 불필요한 파라미터 삭제 * feat: flyway 스크립트 작성 * refactor: 불필요한 어노테이션 삭제 * refactor: 개행 추가 및 분기문 스트림으로 대체 * feat: 안 읽은 메시지 개수 컨트롤러 업데이트 * refactor: 불필요한 필드 삭제 * refactor: 불필요한 join이 발생하는 쿼리 삭제 * refactor: 불필요한 import문 삭제 * refactor: 메시지 전송과 읽음 저장 트랜잭션 분리 * refactor: 불필요한 어노테이션 삭제, 자동 정렬 * test: 예외 케이스 테스트 추가 * refactor: 업데이트에 적절한 변수명으로 변경 * refactor: 레포지토리 저장 시 여러 개 저장하는 메서드 생성 * chore: 잘못된 flyway 스크림트 수정
- Loading branch information
Showing
36 changed files
with
1,238 additions
and
159 deletions.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
...ddang/src/main/java/com/ddang/ddang/chat/application/LastReadMessageLogEventListener.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,39 @@ | ||
package com.ddang.ddang.chat.application; | ||
|
||
import com.ddang.ddang.chat.application.event.CreateReadMessageLogEvent; | ||
import com.ddang.ddang.chat.application.event.UpdateReadMessageLogEvent; | ||
import com.ddang.ddang.chat.application.exception.ReadMessageLogNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class LastReadMessageLogEventListener { | ||
|
||
private final LastReadMessageLogService lastReadMessageLogService; | ||
|
||
@TransactionalEventListener | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public void create(final CreateReadMessageLogEvent createReadMessageLogEvent) { | ||
try { | ||
lastReadMessageLogService.create(createReadMessageLogEvent); | ||
} catch (final IllegalArgumentException ex) { | ||
log.error("exception type : {}, ", ex.getClass().getSimpleName(), ex); | ||
} | ||
} | ||
|
||
@TransactionalEventListener | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public void update(final UpdateReadMessageLogEvent updateReadMessageLogEvent) { | ||
try { | ||
lastReadMessageLogService.update(updateReadMessageLogEvent); | ||
} catch (final ReadMessageLogNotFoundException ex) { | ||
log.error("exception type : {}, ", ex.getClass().getSimpleName(), ex); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
backend/ddang/src/main/java/com/ddang/ddang/chat/application/LastReadMessageLogService.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,46 @@ | ||
package com.ddang.ddang.chat.application; | ||
|
||
import com.ddang.ddang.chat.application.event.CreateReadMessageLogEvent; | ||
import com.ddang.ddang.chat.application.event.UpdateReadMessageLogEvent; | ||
import com.ddang.ddang.chat.application.exception.ReadMessageLogNotFoundException; | ||
import com.ddang.ddang.chat.domain.ChatRoom; | ||
import com.ddang.ddang.chat.domain.ReadMessageLog; | ||
import com.ddang.ddang.chat.domain.repository.ReadMessageLogRepository; | ||
import com.ddang.ddang.user.domain.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class LastReadMessageLogService { | ||
|
||
private final ReadMessageLogRepository readMessageLogRepository; | ||
|
||
@Transactional | ||
public void create(final CreateReadMessageLogEvent createReadMessageLogEvent) { | ||
final ChatRoom chatRoom = createReadMessageLogEvent.chatRoom(); | ||
final User buyer = chatRoom.getBuyer(); | ||
final User seller = chatRoom.getAuction().getSeller(); | ||
final ReadMessageLog buyerReadMessageLog = new ReadMessageLog(chatRoom, buyer); | ||
final ReadMessageLog sellerReadMessageLog = new ReadMessageLog(chatRoom, seller); | ||
|
||
readMessageLogRepository.saveAll(List.of(buyerReadMessageLog, sellerReadMessageLog)); | ||
} | ||
|
||
@Transactional | ||
public void update(final UpdateReadMessageLogEvent updateReadMessageLogEvent) { | ||
final User reader = updateReadMessageLogEvent.reader(); | ||
final ChatRoom chatRoom = updateReadMessageLogEvent.chatRoom(); | ||
final ReadMessageLog messageLog = readMessageLogRepository.findBy(reader.getId(), chatRoom.getId()) | ||
.orElseThrow(() -> | ||
new ReadMessageLogNotFoundException( | ||
"메시지 조회 로그가 존재하지 않습니다." | ||
)); | ||
|
||
messageLog.updateLastReadMessage(updateReadMessageLogEvent.lastReadMessage().getId()); | ||
} | ||
} |
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
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
6 changes: 6 additions & 0 deletions
6
...ddang/src/main/java/com/ddang/ddang/chat/application/event/CreateReadMessageLogEvent.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,6 @@ | ||
package com.ddang.ddang.chat.application.event; | ||
|
||
import com.ddang.ddang.chat.domain.ChatRoom; | ||
|
||
public record CreateReadMessageLogEvent(ChatRoom chatRoom) { | ||
} |
8 changes: 8 additions & 0 deletions
8
...ddang/src/main/java/com/ddang/ddang/chat/application/event/UpdateReadMessageLogEvent.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,8 @@ | ||
package com.ddang.ddang.chat.application.event; | ||
|
||
import com.ddang.ddang.chat.domain.ChatRoom; | ||
import com.ddang.ddang.chat.domain.Message; | ||
import com.ddang.ddang.user.domain.User; | ||
|
||
public record UpdateReadMessageLogEvent(User reader, ChatRoom chatRoom, Message lastReadMessage) { | ||
} |
8 changes: 8 additions & 0 deletions
8
...main/java/com/ddang/ddang/chat/application/exception/ReadMessageLogNotFoundException.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,8 @@ | ||
package com.ddang.ddang.chat.application.exception; | ||
|
||
public class ReadMessageLogNotFoundException extends IllegalArgumentException { | ||
|
||
public ReadMessageLogNotFoundException(final String message) { | ||
super(message); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
backend/ddang/src/main/java/com/ddang/ddang/chat/domain/ReadMessageLog.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,49 @@ | ||
package com.ddang.ddang.chat.domain; | ||
|
||
import com.ddang.ddang.user.domain.User; | ||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.ForeignKey; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToOne; | ||
import lombok.AccessLevel; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@EqualsAndHashCode(of = "id", callSuper = false) | ||
@ToString(of = {"id", "lastReadMessageId"}) | ||
public class ReadMessageLog { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE}) | ||
@JoinColumn(name = "chat_room_id", nullable = false, foreignKey = @ForeignKey(name = "fk_read_message_log_chat_room")) | ||
private ChatRoom chatRoom; | ||
|
||
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE}) | ||
@JoinColumn(name = "reader_id", nullable = false, foreignKey = @ForeignKey(name = "fk_read_message_log_reader")) | ||
private User reader; | ||
|
||
private Long lastReadMessageId = 0L; | ||
|
||
public ReadMessageLog(final ChatRoom chatRoom, final User reader) { | ||
this.chatRoom = chatRoom; | ||
this.reader = reader; | ||
} | ||
|
||
public void updateLastReadMessage(final Long lastReadMessageId) { | ||
this.lastReadMessageId = lastReadMessageId; | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
.../ddang/src/main/java/com/ddang/ddang/chat/domain/repository/ReadMessageLogRepository.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,13 @@ | ||
package com.ddang.ddang.chat.domain.repository; | ||
|
||
import com.ddang.ddang.chat.domain.ReadMessageLog; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ReadMessageLogRepository { | ||
|
||
Optional<ReadMessageLog> findBy(final Long readerId, final Long chatRoomId); | ||
|
||
List<ReadMessageLog> saveAll(List<ReadMessageLog> readMessageLogs); | ||
} |
17 changes: 17 additions & 0 deletions
17
...ain/java/com/ddang/ddang/chat/infrastructure/persistence/JpaReadMessageLogRepository.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,17 @@ | ||
package com.ddang.ddang.chat.infrastructure.persistence; | ||
|
||
import com.ddang.ddang.chat.domain.ReadMessageLog; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.Optional; | ||
|
||
public interface JpaReadMessageLogRepository extends JpaRepository<ReadMessageLog, Long> { | ||
|
||
@Query(""" | ||
SELECT rml | ||
FROM ReadMessageLog rml | ||
WHERE rml.chatRoom.id = :chatRoomId AND rml.reader.id = :readerId | ||
""") | ||
Optional<ReadMessageLog> findLastReadMessageByUserIdAndChatRoomId(final Long readerId, final Long chatRoomId); | ||
} |
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
Oops, something went wrong.