-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/#634: Bookmark 동시성 이슈 해결 및 이벤트 발행 #640
base: refactor/dependency
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.mapbefine.mapbefine.topic.domain; | ||
|
||
public record BookmarkAdditionEvent(Long topicId) { | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.mapbefine.mapbefine.topic.domain; | ||
|
||
public record BookmarkDeleteEvent(Long topicId) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,15 @@ public interface TopicRepository extends JpaRepository<Topic, Long> { | |
@EntityGraph(attributePaths = {"creator"}) | ||
List<Topic> findAllByCreatorId(Long creatorId); | ||
|
||
@Modifying(clearAutomatically = true) | ||
@Query("update Topic t set t.bookmarkCount = t.bookmarkCount - 1 where t.id = :topicId") | ||
void decreaseBookmarkCountById(@Param("topicId") Long topicId); | ||
|
||
|
||
@Modifying(clearAutomatically = true, flushAutomatically = true) | ||
@Query("update Topic t set t.bookmarkCount = t.bookmarkCount + 1 where t.id = :topicId") | ||
void increaseBookmarkCountById(@Param("topicId") Long topicId); | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 왜 이 녀석만 flushAutomatically 옵션을 두었는지 궁금해하실 것 같아요 ㅎㅎ 이거 진짜 팀 블로그에 쓸건데... 최대한 간략하게 설명드리자면 다음과 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Topic JPQL 이 수행될 때, 자기 것만 flush 하고, 또한 clearAutomatically 옵션으로 인해서, 영속성 컨텍스트가 비워져 Bookmark 저장 쿼리가 사라진다는 의미인가요?? 제가 이해한 게 맞나요? |
||
|
||
@Modifying(clearAutomatically = true) | ||
@Query("update Topic t set t.isDeleted = true where t.id = :topicId") | ||
void deleteById(@Param("topicId") Long topicId); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.mapbefine.mapbefine.topic.listener; | ||
|
||
import com.mapbefine.mapbefine.topic.domain.BookmarkAdditionEvent; | ||
import com.mapbefine.mapbefine.topic.domain.BookmarkDeleteEvent; | ||
import com.mapbefine.mapbefine.topic.domain.TopicRepository; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TopicEventListener { | ||
|
||
private final TopicRepository topicRepository; | ||
|
||
public TopicEventListener(TopicRepository topicRepository) { | ||
this.topicRepository = topicRepository; | ||
} | ||
|
||
@EventListener | ||
public void removeBookmark(BookmarkDeleteEvent event) { | ||
topicRepository.decreaseBookmarkCountById(event.topicId()); | ||
} | ||
|
||
@EventListener | ||
public void addBookmark(BookmarkAdditionEvent event) { | ||
topicRepository.increaseBookmarkCountById(event.topicId()); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기술 스택으로 Java17을 선택한 만큼, 레코드를 사용했는데요.
이를 도메인 패키지에 두었는데 너무 DTO 같아 보이려나요 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아니요!
보니까 PinUpdateEvent 도 Record 네용 ㅎㅎ
오히려 일관성을 위해서 더욱이 Record 로 두어야 할 것 같습니다.