Skip to content

Commit eb45c83

Browse files
Refactor: 카드셋 이미지 정합성 추가
Refactor: 카드셋 이미지 정합성 추가
2 parents f6bc2ad + 1663b25 commit eb45c83

20 files changed

Lines changed: 293 additions & 158 deletions

src/main/java/project/flipnote/cardset/entity/CardSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ private CardSet(String name, Group group, Boolean publicVisible, Category catego
5858
this.imageUrl = imageUrl;
5959
}
6060

61-
public void update(CardSetUpdatePayload payload) {
61+
public void update(CardSetUpdatePayload payload, String imageUrl) {
6262
this.name = payload.name();
6363
this.publicVisible = payload.publicVisible();
6464
this.category = payload.category();
6565
this.hashtag = payload.hashtag();
66-
this.imageUrl = payload.imageUrl();
66+
this.imageUrl = imageUrl;
6767
}
6868
}

src/main/java/project/flipnote/cardset/model/CardSetDetailResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public record CardSetDetailResponse(
1313
String category,
1414
String hashtag,
1515
String imageUrl,
16+
Long imageRefId,
1617
boolean publicVisible,
1718

1819
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@@ -22,14 +23,15 @@ public record CardSetDetailResponse(
2223
LocalDateTime modifiedAt
2324
) {
2425

25-
public static CardSetDetailResponse from(CardSet cardSet) {
26+
public static CardSetDetailResponse from(CardSet cardSet, Long imageRefId) {
2627
return new CardSetDetailResponse(
2728
cardSet.getId(),
2829
cardSet.getGroup().getId(),
2930
cardSet.getName(),
3031
cardSet.getCategory().name(),
3132
cardSet.getHashtag(),
3233
cardSet.getImageUrl(),
34+
imageRefId,
3335
cardSet.getPublicVisible(),
3436
cardSet.getCreatedAt(),
3537
cardSet.getModifiedAt()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package project.flipnote.cardset.model;
2+
3+
import project.flipnote.cardset.entity.CardSet;
4+
import project.flipnote.group.entity.Category;
5+
import project.flipnote.group.entity.Group;
6+
7+
public record CardSetInfo(
8+
CardSet cardSet,
9+
Group group,
10+
String name,
11+
Category category,
12+
String hashtag,
13+
String imageUrl,
14+
Long imageRefId
15+
) {
16+
}

src/main/java/project/flipnote/cardset/model/CardSetSummaryResponse.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ public record CardSetSummaryResponse(
88
String name,
99
String category,
1010
String hashtag,
11-
String imageUrl
11+
String imageUrl,
12+
Long imageRefId
1213
) {
1314

14-
public static CardSetSummaryResponse from(CardSet cardSet) {
15+
public static CardSetSummaryResponse from(CardSetInfo cardSetInfo) {
1516
return new CardSetSummaryResponse(
16-
cardSet.getId(),
17-
cardSet.getGroup().getId(),
18-
cardSet.getName(),
19-
cardSet.getCategory().name(),
20-
cardSet.getHashtag(),
21-
cardSet.getImageUrl()
17+
cardSetInfo.cardSet().getId(),
18+
cardSetInfo.group().getId(),
19+
cardSetInfo.name(),
20+
cardSetInfo.category().name(),
21+
cardSetInfo.hashtag(),
22+
cardSetInfo.imageUrl(),
23+
cardSetInfo.imageRefId()
2224
);
2325
}
2426
}

src/main/java/project/flipnote/cardset/model/CardSetUpdatePayload.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public record CardSetUpdatePayload(
77
Boolean publicVisible,
88
Category category,
99
String hashtag,
10-
String imageUrl
10+
Long imageRefId
1111
) {
1212

1313
public static CardSetUpdatePayload from(CardSetUpdateRequest req) {
@@ -16,7 +16,7 @@ public static CardSetUpdatePayload from(CardSetUpdateRequest req) {
1616
req.publicVisible(),
1717
req.category(),
1818
req.getHashTag(),
19-
req.image()
19+
req.imageRefId()
2020
);
2121
}
2222
}

src/main/java/project/flipnote/cardset/model/CardSetUpdateRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public record CardSetUpdateRequest(
2525
@NotNull
2626
List<String> hashtag,
2727

28-
@URL
29-
String image
28+
Long imageRefId
3029
) {
3130

3231
@Schema(hidden = true)

src/main/java/project/flipnote/cardset/model/CreateCardSetRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public record CreateCardSetRequest(
2424
@NotNull
2525
List<String> hashtag,
2626

27-
@URL
28-
String image
27+
Long imageRefId
2928
) {
3029
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package project.flipnote.cardset.repository;
22

3+
import java.util.List;
4+
import java.util.Set;
5+
36
import org.springframework.data.domain.Page;
47
import org.springframework.data.domain.Pageable;
58

69
import project.flipnote.cardset.entity.CardSet;
10+
import project.flipnote.cardset.model.CardSetInfo;
711
import project.flipnote.group.entity.Category;
812

913
public interface CardSetRepositoryCustom {
1014

11-
Page<CardSet> searchByNameContainingAndCategory(
15+
Page<CardSetInfo> searchByNameContainingAndCategory(
1216
String name,
1317
Category category,
1418
Pageable pageable
1519
);
20+
21+
List<CardSetInfo> findAllByIdWithImageRefId(Set<Long> cardSets);
1622
}

src/main/java/project/flipnote/cardset/repository/CardSetRepositoryCustomImpl.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6+
import java.util.Set;
67

8+
import org.checkerframework.checker.units.qual.C;
79
import org.springframework.data.domain.Page;
810
import org.springframework.data.domain.PageImpl;
911
import org.springframework.data.domain.Pageable;
@@ -12,6 +14,7 @@
1214
import org.springframework.util.StringUtils;
1315

1416
import com.querydsl.core.types.OrderSpecifier;
17+
import com.querydsl.core.types.Projections;
1518
import com.querydsl.core.types.dsl.BooleanExpression;
1619
import com.querydsl.core.types.dsl.NumberPath;
1720
import com.querydsl.jpa.impl.JPAQuery;
@@ -22,8 +25,11 @@
2225
import project.flipnote.cardset.entity.CardSet;
2326
import project.flipnote.cardset.entity.QCardSet;
2427
import project.flipnote.cardset.entity.QCardSetMetadata;
28+
import project.flipnote.cardset.model.CardSetInfo;
2529
import project.flipnote.cardset.model.CardSetSortField;
2630
import project.flipnote.group.entity.Category;
31+
import project.flipnote.image.entity.QImageRef;
32+
import project.flipnote.image.entity.ReferenceType;
2733

2834
@Slf4j
2935
@RequiredArgsConstructor
@@ -34,9 +40,10 @@ public class CardSetRepositoryCustomImpl implements CardSetRepositoryCustom {
3440

3541
private final QCardSet cardSet = QCardSet.cardSet;
3642
private final QCardSetMetadata cardSetMetadata = QCardSetMetadata.cardSetMetadata;
43+
private final QImageRef imageRef = QImageRef.imageRef;
3744

3845
@Override
39-
public Page<CardSet> searchByNameContainingAndCategory(
46+
public Page<CardSetInfo> searchByNameContainingAndCategory(
4047
String name,
4148
Category category,
4249
Pageable pageable
@@ -71,16 +78,29 @@ public Page<CardSet> searchByNameContainingAndCategory(
7178
orders.add(cardSet.id.desc());
7279
}
7380

74-
JPAQuery<CardSet> selectQuery = queryFactory
75-
.select(cardSet)
81+
JPAQuery<CardSetInfo> selectQuery = queryFactory
82+
.select(
83+
Projections.constructor(
84+
CardSetInfo.class,
85+
cardSet,
86+
cardSet.group,
87+
cardSet.name,
88+
cardSet.category,
89+
cardSet.hashtag,
90+
cardSet.imageUrl,
91+
imageRef.id
92+
))
7693
.from(cardSet)
77-
.where(buildCardSetSearchFilterConditions(name, category));
94+
.where(buildCardSetSearchFilterConditions(name, category))
95+
.leftJoin(imageRef)
96+
.on(imageRef.referenceType.eq(ReferenceType.CARD_SET)
97+
.and(imageRef.referenceId.eq(cardSet.id)));
7898

7999
if (useMetadata) {
80100
selectQuery.leftJoin(cardSetMetadata).on(cardSet.id.eq(cardSetMetadata.id));
81101
}
82102

83-
List<CardSet> content = selectQuery
103+
List<CardSetInfo> content = selectQuery
84104
.orderBy(orders.toArray(new OrderSpecifier[0]))
85105
.offset(pageable.getOffset())
86106
.limit(pageable.getPageSize())
@@ -95,6 +115,26 @@ public Page<CardSet> searchByNameContainingAndCategory(
95115
return new PageImpl<>(content, pageable, total != null ? total : 0L);
96116
}
97117

118+
public List<CardSetInfo> findAllByIdWithImageRefId(Set<Long> cardSets) {
119+
return queryFactory.select(
120+
Projections.constructor(
121+
CardSetInfo.class,
122+
cardSet,
123+
cardSet.group,
124+
cardSet.name,
125+
cardSet.category,
126+
cardSet.hashtag,
127+
cardSet.imageUrl,
128+
imageRef.id
129+
))
130+
.from(cardSet)
131+
.where(cardSet.id.in(cardSets))
132+
.leftJoin(imageRef)
133+
.on(imageRef.referenceType.eq(ReferenceType.CARD_SET)
134+
.and(imageRef.referenceId.eq(cardSet.id)))
135+
.fetch();
136+
}
137+
98138
private OrderSpecifier<?> toOrderSpecifier(
99139
NumberPath<?> path,
100140
Sort.Order order

src/main/java/project/flipnote/cardset/service/CardSetService.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package project.flipnote.cardset.service;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import java.util.Set;
56

7+
import org.springframework.beans.factory.annotation.Value;
68
import org.springframework.data.domain.Page;
79
import org.springframework.stereotype.Service;
810
import org.springframework.transaction.annotation.Transactional;
@@ -14,6 +16,7 @@
1416
import project.flipnote.cardset.entity.CardSetMetadata;
1517
import project.flipnote.cardset.exception.CardSetErrorCode;
1618
import project.flipnote.cardset.model.CardSetDetailResponse;
19+
import project.flipnote.cardset.model.CardSetInfo;
1720
import project.flipnote.cardset.model.CardSetSearchRequest;
1821
import project.flipnote.cardset.model.CardSetSummaryResponse;
1922
import project.flipnote.cardset.model.CardSetUpdatePayload;
@@ -31,6 +34,13 @@
3134
import project.flipnote.group.exception.GroupErrorCode;
3235
import project.flipnote.group.repository.GroupMemberRepository;
3336
import project.flipnote.group.repository.GroupRepository;
37+
import project.flipnote.image.entity.Image;
38+
import project.flipnote.image.entity.ImageMeta;
39+
import project.flipnote.image.entity.ImageRef;
40+
import project.flipnote.image.entity.ReferenceType;
41+
import project.flipnote.image.exception.ImageErrorCode;
42+
import project.flipnote.image.service.ImageRefService;
43+
import project.flipnote.image.service.ImageService;
3444
import project.flipnote.user.entity.UserProfile;
3545
import project.flipnote.user.entity.UserStatus;
3646
import project.flipnote.user.exception.UserErrorCode;
@@ -49,6 +59,13 @@ public class CardSetService {
4959
private final CardSetManagerRepository cardSetManagerRepository;
5060
private final CardSetPolicyService cardSetPolicyService;
5161
private final CardSetMetadataRepository cardSetMetadataRepository;
62+
private final ImageService imageService;
63+
private final ImageRefService imageRefService;
64+
65+
@Value("${image.default.cardSet}")
66+
private String defaultCardSetImage;
67+
68+
private static final ReferenceType REFERENCE_TYPE = ReferenceType.CARD_SET;
5269

5370
private UserProfile validateUser(Long userId) {
5471
return userProfileRepository.findByIdAndStatus(userId, UserStatus.ACTIVE).orElseThrow(
@@ -85,17 +102,25 @@ public CreateCardSetResponse createCardSet(Long groupId, AuthPrinciple authPrinc
85102
? String.join(",", req.hashtag())
86103
: null;
87104

105+
//이미지 url 찾기
106+
String url = imageService.assignImageUrl(REFERENCE_TYPE, req.imageRefId());
107+
88108
CardSet cardSet = CardSet.builder()
89109
.name(req.name())
90110
.group(group)
91111
.publicVisible(req.publicVisible())
92112
.category(req.category())
93113
.hashtag(hashtags)
94-
.imageUrl(req.image())
114+
.imageUrl(url)
95115
.build();
96116

97117
cardSetRepository.save(cardSet);
98118

119+
if(req.imageRefId()!=null) {
120+
// 이미지 활성화
121+
imageService.changeUrlStatus(req.imageRefId(), REFERENCE_TYPE, cardSet.getId());
122+
}
123+
99124
CardSetMetadata metadata = CardSetMetadata.builder()
100125
.id(cardSet.getId())
101126
.build();
@@ -121,8 +146,8 @@ public CreateCardSetResponse createCardSet(Long groupId, AuthPrinciple authPrinc
121146
*/
122147
public PagingResponse<CardSetSummaryResponse> getCardSets(CardSetSearchRequest req) {
123148
// TODO: Projection 튜닝 필요
124-
Page<CardSet> cardSetPage = cardSetRepository.searchByNameContainingAndCategory(
125-
req.getKeyword(), Category.from(req.getCategory()), req.getPageRequest()
149+
Page<CardSetInfo> cardSetPage = cardSetRepository.searchByNameContainingAndCategory(
150+
req.getKeyword(), Category.from(req.getCategory()), req.getPageRequest()
126151
);
127152

128153
Page<CardSetSummaryResponse> res = cardSetPage.map(CardSetSummaryResponse::from);
@@ -144,7 +169,13 @@ public CardSetDetailResponse getCardSet(Long userId, Long groupId, Long cardSetI
144169

145170
cardSetPolicyService.validateCardSetViewable(cardSet, userId);
146171

147-
return CardSetDetailResponse.from(cardSet);
172+
Optional<ImageRef> imageRef = imageRefService.findByTypeAndReferenceId(REFERENCE_TYPE, cardSetId);
173+
174+
Long imageRefId = imageRefService.findByTypeAndReferenceId(REFERENCE_TYPE, cardSetId)
175+
.map(ImageRef::getId)
176+
.orElse(null);
177+
178+
return CardSetDetailResponse.from(cardSet, imageRefId);
148179
}
149180

150181
/**
@@ -163,12 +194,14 @@ public CardSetDetailResponse updateCardSet(Long userId, Long groupId, Long cardS
163194

164195
cardSetPolicyService.validateCardSetEditable(userId, cardSetId);
165196

197+
ImageMeta imageMeta = imageService.changeImage(REFERENCE_TYPE, cardSetId, req.imageRefId());
198+
166199
CardSetUpdatePayload updatePayload = CardSetUpdatePayload.from(req);
167-
cardSet.update(updatePayload);
200+
cardSet.update(updatePayload, imageMeta.url());
168201

169202
cardSetRepository.saveAndFlush(cardSet);
170203

171-
return CardSetDetailResponse.from(cardSet);
204+
return CardSetDetailResponse.from(cardSet, imageMeta.imageRefId());
172205
}
173206

174207
/**
@@ -214,7 +247,7 @@ public void decrementLikeCount(Long cardSetId) {
214247
@Transactional
215248
public List<CardSetSummaryResponse> getCardSetsByIds(Set<Long> targetIds) {
216249
// TODO: MSA로 전환시 전용 DTO로 변경 필요
217-
return cardSetRepository.findAllById(targetIds).stream()
250+
return cardSetRepository.findAllByIdWithImageRefId(targetIds).stream()
218251
.map(CardSetSummaryResponse::from)
219252
.toList();
220253
}
@@ -244,8 +277,8 @@ public boolean isCardSetViewable(Long cardSetId, Long userId) {
244277
@Transactional
245278
public List<CardSetSummaryResponse> findViewableCardSetsByIds(Set<Long> targetIds, Long userId) {
246279
// TODO: MSA로 전환시 전용 DTO로 변경 필요
247-
return cardSetRepository.findAllById(targetIds).stream()
248-
.filter(cardSet -> cardSetPolicyService.isCardSetViewable(cardSet, userId))
280+
return cardSetRepository.findAllByIdWithImageRefId(targetIds).stream()
281+
.filter(cardSetInfo -> cardSetPolicyService.isCardSetViewable(cardSetInfo.cardSet(), userId))
249282
.map(CardSetSummaryResponse::from)
250283
.toList();
251284
}

0 commit comments

Comments
 (0)