11package project .flipnote .like .service ;
22
33import java .time .LocalDateTime ;
4- import java .util .List ;
54import java .util .Map ;
65import java .util .Set ;
7- import java .util .function .Function ;
86import java .util .stream .Collectors ;
97
108import org .springframework .context .ApplicationEventPublisher ;
1311import org .springframework .stereotype .Service ;
1412import org .springframework .transaction .annotation .Transactional ;
1513
16- import jakarta .annotation .PostConstruct ;
1714import lombok .RequiredArgsConstructor ;
1815import lombok .extern .slf4j .Slf4j ;
1916import project .flipnote .common .exception .BizException ;
2320import project .flipnote .like .entity .Like ;
2421import project .flipnote .like .entity .LikeTargetType ;
2522import project .flipnote .like .exception .LikeErrorCode ;
26- import project .flipnote .like .model .LikeResponse ;
27- import project .flipnote .like .model .LikeSearchRequest ;
28- import project .flipnote .like .model .LikeTargetResponse ;
23+ import project .flipnote .like .model .response . LikeResponse ;
24+ import project .flipnote .like .model .request . LikeSearchRequest ;
25+ import project .flipnote .like .model .response . LikeTargetResponse ;
2926import project .flipnote .like .repository .LikeRepository ;
30- import project .flipnote .like .service .fetcher .LikeTargetFetcher ;
3127
3228@ Slf4j
3329@ RequiredArgsConstructor
@@ -38,15 +34,8 @@ public class LikeService {
3834 private final LikeRepository likeRepository ;
3935 private final ApplicationEventPublisher eventPublisher ;
4036 private final LikePolicyService likePolicyService ;
41- private final List <LikeTargetFetcher <?>> fetchers ;
42-
43- private Map <LikeTargetType , LikeTargetFetcher <?>> fetcherMap ;
44-
45- @ PostConstruct
46- public void init () {
47- this .fetcherMap = this .fetchers .stream ()
48- .collect (Collectors .toMap (LikeTargetFetcher ::getTargetType , Function .identity ()));
49- }
37+ private final LikeTargetFetchService <LikeTargetResponse > likeTargetFetchService ;
38+ private final LikeReader likeReader ;
5039
5140 /**
5241 * 좋아요 추가
@@ -58,7 +47,7 @@ public void init() {
5847 */
5948 @ Transactional
6049 public void addLike (Long userId , LikeTargetType targetType , Long targetId ) {
61- likePolicyService .validateTargetExists (targetType , targetId );
50+ likePolicyService .validateTargetExists (targetType , targetId , userId );
6251 likePolicyService .validateNotAlreadyLiked (targetType , targetId , userId );
6352
6453 Like like = Like .builder ()
@@ -86,8 +75,7 @@ public void addLike(Long userId, LikeTargetType targetType, Long targetId) {
8675 */
8776 @ Transactional
8877 public void removeLike (Long userId , LikeTargetType targetType , Long targetId ) {
89- Like like = likeRepository .findByTargetTypeAndTargetIdAndUserId (targetType , targetId , userId )
90- .orElseThrow (() -> new BizException (LikeErrorCode .LIKE_NOT_FOUND ));
78+ Like like = likeReader .findByTargetAndUserId (targetType , targetId , userId );
9179
9280 likeRepository .delete (like );
9381
@@ -100,11 +88,10 @@ public void removeLike(Long userId, LikeTargetType targetType, Long targetId) {
10088 * @param userId 좋아요 누른 목록을 조회하는 회원의 ID
10189 * @param targetType 조회할 좋아요 대상 타입
10290 * @param req 페이징 및 검색 조건이 포함된 요청 정보
103- * @param <T> 좋아요 대상의 상세 정보를 담은 DTO 타입 (LikeTargetResponse 하위 타입)
10491 * @return 페이징된 좋아요 누른 목록
10592 * @author 윤정환
10693 */
107- public < T extends LikeTargetResponse > PagingResponse <LikeResponse <T >> getLikes (
94+ public PagingResponse <LikeResponse <LikeTargetResponse >> getLikes (
10895 Long userId ,
10996 LikeTargetType targetType ,
11097 LikeSearchRequest req
@@ -114,14 +101,9 @@ public <T extends LikeTargetResponse> PagingResponse<LikeResponse<T>> getLikes(
114101 .collect (Collectors .toMap (Like ::getTargetId , Like ::getCreatedAt ));
115102 Set <Long > targetIds = likedAtMap .keySet ();
116103
117- // TODO: 제네릭이 아닌 타입 별로 엔드포인트를 따로 만드는게 좋으려나 고민중, 현재 방법을 유지하면서 더 나은 구조 알고싶음...
118- LikeTargetFetcher <T > fetcher = (LikeTargetFetcher <T >)fetcherMap .get (targetType );
119- if (fetcher == null ) {
120- throw new BizException (LikeErrorCode .INVALID_LIKE_TYPE );
121- }
122-
123- Map <Long , T > targetMap = fetcher .fetchByIds (targetIds );
124- Page <LikeResponse <T >> content = likePage
104+ Map <Long , LikeTargetResponse > targetMap
105+ = likeTargetFetchService .fetchByTypeAndIds (targetType , targetIds , userId );
106+ Page <LikeResponse <LikeTargetResponse >> content = likePage
125107 .map (like -> new LikeResponse <>(targetMap .get (like .getTargetId ()), likedAtMap .get (like .getTargetId ())));
126108
127109 return PagingResponse .from (content );
0 commit comments