11package flipnote .reaction .bookmark .service ;
22
3- import org .springframework .amqp .rabbit .core .RabbitTemplate ;
3+ import java .util .List ;
4+ import java .util .Map ;
5+
46import org .springframework .dao .DataIntegrityViolationException ;
57import org .springframework .data .domain .Page ;
68import org .springframework .stereotype .Service ;
79import org .springframework .transaction .annotation .Transactional ;
810
11+ import cardset .Cardset ;
12+ import cardset .Cardset .CardSetSummary ;
913import flipnote .reaction .bookmark .entity .Bookmark ;
1014import flipnote .reaction .bookmark .entity .BookmarkTargetType ;
1115import flipnote .reaction .bookmark .exception .BookmarkErrorCode ;
1216import flipnote .reaction .bookmark .model .request .BookmarkSearchRequest ;
1317import flipnote .reaction .bookmark .model .response .BookmarkResponse ;
1418import flipnote .reaction .bookmark .repository .BookmarkRepository ;
1519import flipnote .reaction .common .config .RabbitMqConfig ;
20+ import flipnote .reaction .common .event .ReactionEventPublisher ;
1621import flipnote .reaction .common .exception .BizException ;
17- import flipnote .reaction .common .model .event .ReactionMessage ;
22+ import flipnote .reaction .common .exception .CommonErrorCode ;
23+ import flipnote .reaction .common .grpc .CardSetGrpcClient ;
1824import flipnote .reaction .common .model .response .IdResponse ;
1925import flipnote .reaction .common .model .response .PagingResponse ;
2026import lombok .RequiredArgsConstructor ;
@@ -28,11 +34,16 @@ public class BookmarkService {
2834
2935 private final BookmarkRepository bookmarkRepository ;
3036 private final BookmarkReader bookmarkReader ;
31- private final RabbitTemplate rabbitTemplate ;
37+ private final ReactionEventPublisher eventPublisher ;
38+ private final CardSetGrpcClient cardSetGrpcClient ;
3239
3340 @ Transactional
3441 public IdResponse addBookmark (BookmarkTargetType targetType , Long targetId , Long userId ) {
35- // TODO: gRPC로 대상 존재 여부 검증 (CardSet/Group 서비스 호출)
42+ if (targetType == BookmarkTargetType .CARD_SET ) {
43+ if (!cardSetGrpcClient .isCardSetViewable (targetId , userId )) {
44+ throw new BizException (CommonErrorCode .TARGET_NOT_VIEWABLE );
45+ }
46+ }
3647
3748 if (bookmarkReader .isBookmarked (targetType , targetId , userId )) {
3849 throw new BizException (BookmarkErrorCode .ALREADY_BOOKMARKED );
@@ -50,7 +61,8 @@ public IdResponse addBookmark(BookmarkTargetType targetType, Long targetId, Long
5061 throw new BizException (BookmarkErrorCode .ALREADY_BOOKMARKED );
5162 }
5263
53- publishEvent (RabbitMqConfig .ROUTING_KEY_BOOKMARK_ADDED , "BOOKMARK_ADDED" , targetType , targetId , userId );
64+ eventPublisher .publish (RabbitMqConfig .ROUTING_KEY_BOOKMARK_ADDED , "BOOKMARK_ADDED" ,
65+ targetType .name (), targetId , userId );
5466
5567 return IdResponse .from (bookmark .getId ());
5668 }
@@ -60,7 +72,8 @@ public void removeBookmark(BookmarkTargetType targetType, Long targetId, Long us
6072 Bookmark bookmark = bookmarkReader .findByTargetAndUserId (targetType , targetId , userId );
6173 bookmarkRepository .delete (bookmark );
6274
63- publishEvent (RabbitMqConfig .ROUTING_KEY_BOOKMARK_REMOVED , "BOOKMARK_REMOVED" , targetType , targetId , userId );
75+ eventPublisher .publish (RabbitMqConfig .ROUTING_KEY_BOOKMARK_REMOVED , "BOOKMARK_REMOVED" ,
76+ targetType .name (), targetId , userId );
6477 }
6578
6679 public PagingResponse <BookmarkResponse > getBookmarks (BookmarkTargetType targetType , Long userId ,
@@ -69,23 +82,17 @@ public PagingResponse<BookmarkResponse> getBookmarks(BookmarkTargetType targetTy
6982 targetType , userId , request .getPageRequest ()
7083 );
7184
72- // TODO: gRPC로 대상 상세 정보 fetch (CardSet 서비스 호출)
85+ List <Long > targetIds = bookmarkPage .getContent ().stream ()
86+ .map (Bookmark ::getTargetId )
87+ .toList ();
7388
74- Page < BookmarkResponse > responsePage = bookmarkPage . map ( BookmarkResponse :: from );
75- return PagingResponse . from ( responsePage );
76- }
89+ Map < Long , CardSetSummary > summaryMap = targetIds . isEmpty ()
90+ ? Map . of ()
91+ : cardSetGrpcClient . getCardSetsByIds ( targetIds , userId );
7792
78- private void publishEvent (String routingKey , String eventType ,
79- BookmarkTargetType targetType , Long targetId , Long userId ) {
80- try {
81- rabbitTemplate .convertAndSend (
82- RabbitMqConfig .EXCHANGE ,
83- routingKey ,
84- new ReactionMessage (eventType , targetType .name (), targetId , userId )
85- );
86- } catch (Exception e ) {
87- log .error ("북마크 이벤트 발행 실패: eventType={}, targetType={}, targetId={}, userId={}" ,
88- eventType , targetType , targetId , userId , e );
89- }
93+ Page <BookmarkResponse > responsePage = bookmarkPage .map (
94+ bookmark -> BookmarkResponse .from (bookmark , summaryMap .get (bookmark .getTargetId ()))
95+ );
96+ return PagingResponse .from (responsePage );
9097 }
9198}
0 commit comments