Skip to content

Commit 679c484

Browse files
Fix: 이미지 url 조회 수정
Fix: 이미지 url 조회 수정
2 parents dfe3fb3 + 683cc3f commit 679c484

7 files changed

Lines changed: 172 additions & 45 deletions

File tree

src/main/java/flipnote/group/application/service/ChangeGroupService.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package flipnote.group.application.service;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.stereotype.Service;
45
import org.springframework.transaction.annotation.Transactional;
56

@@ -8,16 +9,21 @@
89
import flipnote.group.application.port.in.ChangeGroupUseCase;
910
import flipnote.group.application.port.in.command.ChangeGroupCommand;
1011
import flipnote.group.application.port.in.result.ChangeGroupResult;
12+
import flipnote.group.application.port.in.result.CreateGroupResult;
1113
import flipnote.group.domain.model.member.GroupMemberRole;
1214
import flipnote.group.domain.policy.BusinessException;
1315
import flipnote.group.domain.policy.ErrorCode;
1416
import flipnote.group.infrastructure.persistence.jpa.GroupRepository;
17+
import flipnote.image.grpc.v1.ActivateImageRequest;
1518
import flipnote.image.grpc.v1.GetUrlByReferenceRequest;
1619
import flipnote.image.grpc.v1.GetUrlByReferenceResponse;
1720
import flipnote.image.grpc.v1.ImageCommandServiceGrpc;
1821
import flipnote.image.grpc.v1.Type;
22+
import io.grpc.StatusRuntimeException;
1923
import lombok.RequiredArgsConstructor;
24+
import lombok.extern.slf4j.Slf4j;
2025

26+
@Slf4j
2127
@Service
2228
@RequiredArgsConstructor
2329
public class ChangeGroupService implements ChangeGroupUseCase {
@@ -26,6 +32,9 @@ public class ChangeGroupService implements ChangeGroupUseCase {
2632
private final GroupRoleRepositoryAdapter groupRoleRepository;
2733
private final ImageCommandServiceGrpc.ImageCommandServiceBlockingStub imageCommandServiceStub;
2834

35+
@Value("${image.default.group}")
36+
private String GROUP_DEFAULT_URL;
37+
2938
/**
3039
* 그룹 수정
3140
* @param cmd
@@ -48,15 +57,47 @@ public ChangeGroupResult change(ChangeGroupCommand cmd) {
4857

4958
entity.change(cmd);
5059

51-
// gRPC로 image 서비스에 url 조회
52-
GetUrlByReferenceRequest request = GetUrlByReferenceRequest.newBuilder()
53-
.setReferenceType(Type.GROUP)
54-
.setReferenceId(cmd.groupId())
55-
.build();
60+
// // gRPC로 image 서비스에 url 조회
61+
// GetUrlByReferenceRequest request = GetUrlByReferenceRequest.newBuilder()
62+
// .setReferenceType(Type.GROUP)
63+
// .setReferenceId(cmd.groupId())
64+
// .build();
65+
//
66+
// GetUrlByReferenceResponse response = imageCommandServiceStub.getUrlByReference(request);
67+
// String imageUrl = response.getImageUrl();
68+
//
69+
// return ChangeGroupResult.of(entity, imageUrl);
70+
71+
if (cmd.imageRefId() != null) {
72+
ActivateImageRequest request = ActivateImageRequest.newBuilder()
73+
.setImageRefId(cmd.imageRefId())
74+
.setReferenceType(Type.GROUP)
75+
.setReferenceId(cmd.groupId())
76+
.build();
77+
// gRPC 호출
78+
79+
try {
80+
imageCommandServiceStub.activateImage(request);
81+
} catch (StatusRuntimeException e) {
82+
switch (e.getStatus().getCode()) {
83+
case NOT_FOUND -> throw new BusinessException(ErrorCode.IMAGE_NOT_FOUND);
84+
case INVALID_ARGUMENT -> throw new BusinessException(ErrorCode.IMAGE_INVALID_REQUEST);
85+
case INTERNAL -> throw new BusinessException(ErrorCode.IMAGE_SERVER_ERROR);
86+
default -> throw new BusinessException(ErrorCode.IMAGE_SERVICE_ERROR);
87+
}
88+
}
5689

57-
GetUrlByReferenceResponse response = imageCommandServiceStub.getUrlByReference(request);
58-
String imageUrl = response.getImageUrl();
90+
GetUrlByReferenceRequest requestUrl = GetUrlByReferenceRequest.newBuilder()
91+
.setReferenceType(Type.GROUP)
92+
.setReferenceId(cmd.groupId())
93+
.build();
94+
95+
GetUrlByReferenceResponse response = imageCommandServiceStub.getUrlByReference(requestUrl);
96+
String imageUrl = response.getImageUrl();
97+
98+
return ChangeGroupResult.of(entity, imageUrl);
99+
}
59100

60-
return ChangeGroupResult.of(entity, imageUrl);
101+
return ChangeGroupResult.of(entity, GROUP_DEFAULT_URL);
61102
}
62103
}

src/main/java/flipnote/group/application/service/CreateGroupService.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import flipnote.image.grpc.v1.Type;
2323
import io.grpc.StatusRuntimeException;
2424
import lombok.RequiredArgsConstructor;
25+
import lombok.extern.slf4j.Slf4j;
2526

27+
@Slf4j
2628
@Service
2729
@RequiredArgsConstructor
2830
public class CreateGroupService implements CreateGroupUseCase {
@@ -49,27 +51,34 @@ public CreateGroupResult create(CreateGroupCommand cmd) {
4951
//그룹 역할 생성
5052
RoleEntity role = groupRoleRepository.create(groupId);
5153

54+
log.debug("{}", role.getId());
55+
5256
//생성자 오너 역할로 저장
5357
GroupMemberEntity groupMember = GroupMemberEntity.create(groupId, cmd.userId(), role);
5458

5559
groupMemberRepository.save(groupMember);
5660

57-
// gRPC로 image 서비스에 url 조회
58-
ActivateImageRequest request = ActivateImageRequest.newBuilder()
59-
.setImageRefId(cmd.imageRefId())
60-
.setReferenceType(Type.GROUP)
61-
.setReferenceId(groupId)
62-
.build();
61+
log.debug("{}", groupMember.getId());
62+
63+
if (cmd.imageRefId() != null) {
64+
ActivateImageRequest request = ActivateImageRequest.newBuilder()
65+
.setImageRefId(cmd.imageRefId())
66+
.setReferenceType(Type.GROUP)
67+
.setReferenceId(groupId)
68+
.build();
69+
// gRPC 호출
6370

64-
try {
65-
imageCommandServiceStub.activateImage(request);
66-
} catch (StatusRuntimeException e) {
67-
switch (e.getStatus().getCode()) {
68-
case NOT_FOUND -> throw new BusinessException(ErrorCode.IMAGE_NOT_FOUND);
69-
case INVALID_ARGUMENT -> throw new BusinessException(ErrorCode.IMAGE_INVALID_REQUEST);
70-
case INTERNAL -> throw new BusinessException(ErrorCode.IMAGE_SERVER_ERROR);
71-
default -> throw new BusinessException(ErrorCode.IMAGE_SERVICE_ERROR);
71+
try {
72+
imageCommandServiceStub.activateImage(request);
73+
} catch (StatusRuntimeException e) {
74+
switch (e.getStatus().getCode()) {
75+
case NOT_FOUND -> throw new BusinessException(ErrorCode.IMAGE_NOT_FOUND);
76+
case INVALID_ARGUMENT -> throw new BusinessException(ErrorCode.IMAGE_INVALID_REQUEST);
77+
case INTERNAL -> throw new BusinessException(ErrorCode.IMAGE_SERVER_ERROR);
78+
default -> throw new BusinessException(ErrorCode.IMAGE_SERVICE_ERROR);
79+
}
7280
}
81+
return CreateGroupResult.of(groupId);
7382
}
7483

7584
return CreateGroupResult.of(groupId);

src/main/java/flipnote/group/application/service/FindGroupService.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package flipnote.group.application.service;
22

3+
import java.util.Collections;
34
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Objects;
47
import java.util.Optional;
58

9+
import org.springframework.beans.factory.annotation.Value;
610
import org.springframework.stereotype.Service;
711
import org.springframework.transaction.annotation.Transactional;
812

@@ -18,6 +22,8 @@
1822
import flipnote.group.domain.policy.ErrorCode;
1923
import flipnote.image.grpc.v1.GetUrlByReferenceRequest;
2024
import flipnote.image.grpc.v1.GetUrlByReferenceResponse;
25+
import flipnote.image.grpc.v1.GetUrlsByIdsRequest;
26+
import flipnote.image.grpc.v1.GetUrlsByIdsResponse;
2127
import flipnote.image.grpc.v1.ImageCommandServiceGrpc;
2228
import flipnote.image.grpc.v1.Type;
2329
import io.grpc.StatusRuntimeException;
@@ -32,6 +38,9 @@ public class FindGroupService implements FindGroupUseCase {
3238
private final GroupMemberRepositoryPort groupMemberRepository;
3339
private final ImageCommandServiceGrpc.ImageCommandServiceBlockingStub imageCommandServiceStub;
3440

41+
@Value("${image.default.group}")
42+
private String GROUP_DEFAULT_URL;
43+
3544
/**
3645
* 하나의 그룹에 대한 정보 조회
3746
* @param cmd
@@ -51,6 +60,11 @@ public FindGroupResult findGroup(FindGroupCommand cmd) {
5160
GroupEntity group = groupRepository.findById(cmd.groupId());
5261

5362
// gRPC로 image 서비스에 url 조회
63+
64+
if(group.getImageRefId()==null) {
65+
return FindGroupResult.of(group, GROUP_DEFAULT_URL);
66+
}
67+
5468
GetUrlByReferenceRequest request = GetUrlByReferenceRequest.newBuilder()
5569
.setReferenceType(Type.GROUP)
5670
.setReferenceId(cmd.groupId())
@@ -80,12 +94,13 @@ public FindGroupResult findGroup(FindGroupCommand cmd) {
8094
*/
8195
@Override
8296
public CursorPagingResponseDto<GroupInfo> findAllGroup(Long userId, GroupListRequestDto req) {
83-
8497
List<GroupInfo> groups = groupRepository.findAllByCursor(
8598
req.getCursorId(),
8699
req.getCategory(),
87100
req.getSize());
88101

102+
enrichGroupsWithImageUrl(groups);
103+
89104
return createGroupInfoCursorPagingResponse(req, groups);
90105
}
91106

@@ -97,13 +112,14 @@ public CursorPagingResponseDto<GroupInfo> findAllGroup(Long userId, GroupListReq
97112
*/
98113
@Override
99114
public CursorPagingResponseDto<GroupInfo> findMyGroup(Long userId, GroupListRequestDto req) {
100-
101115
List<GroupInfo> groups = groupRepository.findAllByCursorAndUserId(
102116
req.getCursorId(),
103117
req.getCategory(),
104118
req.getSize(),
105119
userId);
106120

121+
enrichGroupsWithImageUrl(groups);
122+
107123
return createGroupInfoCursorPagingResponse(req, groups);
108124
}
109125

@@ -115,13 +131,14 @@ public CursorPagingResponseDto<GroupInfo> findMyGroup(Long userId, GroupListRequ
115131
*/
116132
@Override
117133
public CursorPagingResponseDto<GroupInfo> findCreatedGroup(Long userId, GroupListRequestDto req) {
118-
119134
List<GroupInfo> groups = groupRepository.findAllByCursorAndCreatedUserId(
120135
req.getCursorId(),
121136
req.getCategory(),
122137
req.getSize(),
123138
userId);
124139

140+
enrichGroupsWithImageUrl(groups);
141+
125142
return createGroupInfoCursorPagingResponse(req, groups);
126143
}
127144

@@ -136,8 +153,44 @@ private CursorPagingResponseDto<GroupInfo> createGroupInfoCursorPagingResponse(G
136153
groups = groups.subList(0, req.getSize());
137154
}
138155

139-
Long nextCursor = hasNext ? groups.get(groups.size() - 1).groupId() : null;
156+
Long nextCursor = hasNext ? groups.get(groups.size() - 1).getGroupId() : null;
140157

141158
return CursorPagingResponseDto.of(groups, hasNext, nextCursor);
142159
}
160+
161+
private void enrichGroupsWithImageUrl(List<GroupInfo> groups) {
162+
List<Long> imageRefIds = groups.stream()
163+
.map(GroupInfo::getImageRefId)
164+
.filter(Objects::nonNull)
165+
.toList();
166+
167+
Map<Long, String> imageUrlMap = Collections.emptyMap();
168+
169+
if (!imageRefIds.isEmpty()) {
170+
GetUrlsByIdsRequest request = GetUrlsByIdsRequest.newBuilder()
171+
.addAllIds(imageRefIds)
172+
.build();
173+
174+
try {
175+
GetUrlsByIdsResponse response = imageCommandServiceStub.getUrlsByIds(request);
176+
imageUrlMap = response.getImageUrlsMap();
177+
178+
} catch (StatusRuntimeException e) {
179+
switch (e.getStatus().getCode()) {
180+
case NOT_FOUND -> throw new BusinessException(ErrorCode.IMAGE_NOT_FOUND);
181+
case INVALID_ARGUMENT -> throw new BusinessException(ErrorCode.IMAGE_INVALID_REQUEST);
182+
case INTERNAL -> throw new BusinessException(ErrorCode.IMAGE_SERVER_ERROR);
183+
default -> throw new BusinessException(ErrorCode.IMAGE_SERVICE_ERROR);
184+
}
185+
}
186+
}
187+
188+
// imageUrl 세팅 (null이면 기본 이미지)
189+
Map<Long, String> finalImageUrlMap = imageUrlMap;
190+
groups.forEach(group -> group.setImageUrl(
191+
group.getImageRefId() == null
192+
? GROUP_DEFAULT_URL
193+
: finalImageUrlMap.getOrDefault(group.getImageRefId(), GROUP_DEFAULT_URL)
194+
));
195+
}
143196
}
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package flipnote.group.domain.model.group;
22

3-
public record GroupInfo(
4-
Long groupId,
5-
String name,
6-
String description,
7-
Category category,
8-
Long imageRefId) {
9-
public static GroupInfo from(Long groupId, String name, String description, Category category, Long imageRefId) {
10-
return new GroupInfo(groupId, name, description, category, imageRefId);
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public class GroupInfo {
10+
Long groupId;
11+
String name;
12+
String description;
13+
Category category;
14+
Long imageRefId;
15+
String imageUrl;
16+
17+
public void setImageUrl(String imageUrl) {
18+
this.imageUrl = imageUrl;
1119
}
1220
}

src/main/java/flipnote/group/infrastructure/persistence/querydsl/GroupRepositoryImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.querydsl.core.BooleanBuilder;
88
import com.querydsl.core.types.Projections;
9+
import com.querydsl.core.types.dsl.Expressions;
910
import com.querydsl.jpa.impl.JPAQueryFactory;
1011

1112
import flipnote.group.adapter.out.entity.QGroupEntity;
@@ -53,7 +54,8 @@ public List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageS
5354
group.name,
5455
group.description,
5556
group.category,
56-
group.imageRefId
57+
group.imageRefId,
58+
Expressions.nullExpression(String.class)
5759
))
5860
.from(group)
5961
.where(where)
@@ -92,7 +94,8 @@ public List<GroupInfo> findAllByCursorAndUserId(Long lastId, Category category,
9294
group.name,
9395
group.description,
9496
group.category,
95-
group.imageRefId
97+
group.imageRefId,
98+
Expressions.nullExpression(String.class)
9699
))
97100
.from(groupMember)
98101
.join(group).on(group.id.eq(groupMember.groupId))
@@ -120,7 +123,8 @@ public List<GroupInfo> findAllByCursorAndCreatedUserId(Long lastId, Category cat
120123
group.name,
121124
group.description,
122125
group.category,
123-
group.imageRefId
126+
group.imageRefId,
127+
Expressions.nullExpression(String.class)
124128
))
125129
.from(group)
126130
.join(groupMember).on(groupMember.groupId.eq(group.id))

0 commit comments

Comments
 (0)