11package project .flipnote .cardset .service ;
22
3+ import java .util .List ;
4+
35import org .springframework .data .domain .Page ;
46import org .springframework .stereotype .Service ;
57import org .springframework .transaction .annotation .Transactional ;
810import lombok .extern .slf4j .Slf4j ;
911import project .flipnote .cardset .entity .CardSet ;
1012import project .flipnote .cardset .entity .CardSetManager ;
13+ import project .flipnote .cardset .entity .CardSetMetadata ;
1114import project .flipnote .cardset .exception .CardSetErrorCode ;
1215import project .flipnote .cardset .model .CardSetDetailResponse ;
1316import project .flipnote .cardset .model .CardSetSearchRequest ;
1720import project .flipnote .cardset .model .CreateCardSetRequest ;
1821import project .flipnote .cardset .model .CreateCardSetResponse ;
1922import project .flipnote .cardset .repository .CardSetManagerRepository ;
23+ import project .flipnote .cardset .repository .CardSetMetadataRepository ;
2024import project .flipnote .cardset .repository .CardSetRepository ;
2125import project .flipnote .common .exception .BizException ;
2226import project .flipnote .common .model .response .PagingResponse ;
2327import project .flipnote .common .security .dto .AuthPrinciple ;
2428import project .flipnote .group .entity .Category ;
2529import project .flipnote .group .entity .Group ;
26- import project .flipnote .group .entity .GroupPermissionStatus ;
2730import project .flipnote .group .exception .GroupErrorCode ;
2831import project .flipnote .group .repository .GroupMemberRepository ;
2932import project .flipnote .group .repository .GroupRepository ;
30- import project .flipnote .group .service .GroupService ;
3133import project .flipnote .user .entity .UserProfile ;
3234import project .flipnote .user .entity .UserStatus ;
3335import project .flipnote .user .exception .UserErrorCode ;
@@ -44,8 +46,8 @@ public class CardSetService {
4446 private final GroupRepository groupRepository ;
4547 private final GroupMemberRepository groupMemberRepository ;
4648 private final CardSetManagerRepository cardSetManagerRepository ;
47- private final GroupService groupService ;
48- private final CardSetPolicyService cardSetPolicyService ;
49+ private final CardSetPolicyService cardSetPolicyService ;
50+ private final CardSetMetadataRepository cardSetMetadataRepository ;
4951
5052 private UserProfile validateUser (Long userId ) {
5153 return userProfileRepository .findByIdAndStatus (userId , UserStatus .ACTIVE ).orElseThrow (
@@ -93,6 +95,11 @@ public CreateCardSetResponse createCardSet(Long groupId, AuthPrinciple authPrinc
9395
9496 cardSetRepository .save (cardSet );
9597
98+ CardSetMetadata metadata = CardSetMetadata .builder ()
99+ .id (cardSet .getId ())
100+ .build ();
101+ cardSetMetadataRepository .save (metadata );
102+
96103 //카드셋 매니저도 저장
97104 CardSetManager cardSetManager = CardSetManager .builder ()
98105 .user (user )
@@ -114,11 +121,11 @@ public CreateCardSetResponse createCardSet(Long groupId, AuthPrinciple authPrinc
114121 public PagingResponse <CardSetSummaryResponse > getCardSets (CardSetSearchRequest req ) {
115122
116123 // TODO: Projection 및 카운트 쿼리 튜닝 필요, 좋아요 수 및 즐겨찾기 수 등 다양한 정렬 조건 추가 필요
117- Page <CardSet > CardSetPage = cardSetRepository .findByNameContainingAndCategory (
124+ Page <CardSet > cardSetPage = cardSetRepository .findByNameContainingAndCategory (
118125 req .getKeyword (), Category .from (req .getCategory ()), req .getPageRequest ()
119126 );
120127
121- Page <CardSetSummaryResponse > res = CardSetPage .map (CardSetSummaryResponse ::from );
128+ Page <CardSetSummaryResponse > res = cardSetPage .map (CardSetSummaryResponse ::from );
122129
123130 return PagingResponse .from (res );
124131 }
@@ -163,4 +170,52 @@ public CardSetDetailResponse updateCardSet(Long userId, Long groupId, Long cardS
163170
164171 return CardSetDetailResponse .from (cardSet );
165172 }
173+
174+ /**
175+ * 카드셋 존재 여부 확인
176+ *
177+ * @param cardSetId 존재하는지 확인할 카드셋 ID
178+ * @return 카드셋 존재 여부
179+ * @author 윤정환
180+ */
181+ public boolean existsById (Long cardSetId ) {
182+ return cardSetRepository .existsById (cardSetId );
183+ }
184+
185+ /**
186+ * 카드셋 좋아요 수를 1 증가
187+ *
188+ * @param cardSetId 좋아요 수를 증가시킬 카드셋 ID
189+ * @author 윤정환
190+ */
191+ @ Transactional
192+ public void incrementLikeCount (Long cardSetId ) {
193+ cardSetMetadataRepository .incrementLikeCount (cardSetId );
194+ }
195+
196+ /**
197+ * 카드셋 좋아요 수를 1 감소
198+ *
199+ * @param cardSetId 좋아요 수를 감소시킬 카드셋 ID
200+ * @author 윤정환
201+ */
202+ @ Transactional
203+ public void decrementLikeCount (Long cardSetId ) {
204+ cardSetMetadataRepository .decrementLikeCount (cardSetId );
205+ }
206+
207+ /**
208+ * 카드셋 ID 목록에 해당하는 카드셋 목록 조회
209+ *
210+ * @param targetIds 조회할 카드셋 ID 목록
211+ * @return 조회된 카드셋 목록
212+ * @author 윤정환
213+ */
214+ @ Transactional
215+ public List <CardSetSummaryResponse > getCardSetsByIds (List <Long > targetIds ) {
216+ // TODO: MSA로 전환시 전용 DTO로 변경 필요
217+ return cardSetRepository .findAllById (targetIds ).stream ()
218+ .map (CardSetSummaryResponse ::from )
219+ .toList ();
220+ }
166221}
0 commit comments