Skip to content

Commit 038d453

Browse files
committed
Refactor: 코드 리뷰 사항 반영
- 기관 프로필 조회 응답값중 선호 물품 엔티티를 직접 응답하는것이 아닌 Dto로 전환하여 응답하도록 수정과 그에 따른 usecase, service 수정, 선호물품 응답 Dto 생성 - 단위 테스트 코드 작성과 검증 완료
1 parent e2aa112 commit 038d453

File tree

9 files changed

+133
-13
lines changed

9 files changed

+133
-13
lines changed

src/main/java/com/somemore/center/dto/response/CenterProfileResponseDto.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ public record CenterProfileResponseDto(
1818
String imgUrl,
1919
String introduce,
2020
String homepageLink,
21-
List<PreferItem> preferItems
21+
List<PreferItemResponseDto> preferItems
2222
) {
23-
public static CenterProfileResponseDto of(Center center, List<PreferItem> preferItems) {
23+
public static CenterProfileResponseDto of(Center center, List<PreferItemResponseDto> preferItemDtos) {
2424
return CenterProfileResponseDto.builder()
2525
.centerId(center.getId())
2626
.name(center.getName())
2727
.contactNumber(center.getContactNumber())
2828
.imgUrl(center.getImgUrl())
2929
.introduce(center.getIntroduce())
3030
.homepageLink(center.getHomepageLink())
31-
.preferItems(preferItems)
31+
.preferItems(preferItemDtos)
3232
.build();
3333
}
3434
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.somemore.center.dto.response;
2+
3+
import com.somemore.center.domain.PreferItem;
4+
import lombok.Builder;
5+
6+
import java.util.UUID;
7+
8+
@Builder
9+
public record PreferItemResponseDto (
10+
Long id,
11+
UUID centerId,
12+
String itemName
13+
){
14+
public static PreferItemResponseDto from(PreferItem preferItem) {
15+
return PreferItemResponseDto.builder()
16+
.id(preferItem.getId())
17+
.centerId(preferItem.getCenterId())
18+
.itemName(preferItem.getItemName())
19+
.build();
20+
}
21+
}

src/main/java/com/somemore/center/repository/PreferItemRepository.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.somemore.center.domain.PreferItem;
44
import org.springframework.data.jpa.repository.JpaRepository;
5-
import org.springframework.data.repository.query.Param;
65
import org.springframework.stereotype.Repository;
76

87
import java.util.List;
@@ -11,6 +10,6 @@
1110
@Repository
1211
public interface PreferItemRepository extends JpaRepository<PreferItem, Long> {
1312

14-
List<PreferItem> findByCenterId(@Param("centerId") UUID centerId);
13+
List<PreferItem> findByCenterId(UUID centerId);
1514

1615
}

src/main/java/com/somemore/center/service/query/CenterQueryService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.somemore.center.service.query;
2+
23
import com.somemore.center.domain.Center;
3-
import com.somemore.center.domain.PreferItem;
44
import com.somemore.center.dto.response.CenterProfileResponseDto;
5+
import com.somemore.center.dto.response.PreferItemResponseDto;
56
import com.somemore.center.repository.CenterRepository;
67
import com.somemore.center.usecase.query.CenterQueryUseCase;
78
import com.somemore.center.usecase.query.PreferItemQueryUseCase;
@@ -27,9 +28,9 @@ public class CenterQueryService implements CenterQueryUseCase {
2728
public CenterProfileResponseDto getCenterProfileByCenterId(UUID centerId) {
2829

2930
Center center = getCenterById(centerId);
30-
List<PreferItem> preferItems = preferItemQueryUseCase.getPreferItemsByCenterId(centerId);
31+
List<PreferItemResponseDto> preferItemDtos = preferItemQueryUseCase.getPreferItemDtosByCenterId(centerId);
3132

32-
return CenterProfileResponseDto.of(center, preferItems);
33+
return CenterProfileResponseDto.of(center, preferItemDtos);
3334
}
3435

3536
@Override

src/main/java/com/somemore/center/service/query/PreferItemQueryService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.somemore.center.service.query;
22

33
import com.somemore.center.domain.PreferItem;
4+
import com.somemore.center.dto.response.PreferItemResponseDto;
45
import com.somemore.center.repository.PreferItemRepository;
56
import com.somemore.center.usecase.query.PreferItemQueryUseCase;
67
import lombok.RequiredArgsConstructor;
@@ -17,8 +18,21 @@ public class PreferItemQueryService implements PreferItemQueryUseCase {
1718

1819
private final PreferItemRepository preferItemRepository;
1920

21+
@Override
22+
public List<PreferItemResponseDto> getPreferItemDtosByCenterId(UUID centerId) {
23+
List<PreferItem> preferItems = getPreferItemsByCenterId(centerId);
24+
return preferItemConvertToDtos(preferItems);
25+
}
26+
27+
//프론트와 의논후 private으로 전환 예정
2028
@Override
2129
public List<PreferItem> getPreferItemsByCenterId(UUID centerId) {
2230
return preferItemRepository.findByCenterId(centerId);
2331
}
32+
33+
private static List<PreferItemResponseDto> preferItemConvertToDtos(List<PreferItem> preferItems) {
34+
return preferItems.stream()
35+
.map(PreferItemResponseDto::from)
36+
.toList();
37+
}
2438
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.somemore.center.usecase.query;
22

33
import com.somemore.center.domain.PreferItem;
4+
import com.somemore.center.dto.response.PreferItemResponseDto;
45

56
import java.util.List;
67
import java.util.UUID;
78

89
public interface PreferItemQueryUseCase {
10+
List<PreferItemResponseDto> getPreferItemDtosByCenterId(UUID centerId);
911
List<PreferItem> getPreferItemsByCenterId(UUID centerId);
1012
}

src/test/java/com/somemore/center/repository/PreferItemRepositoryTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ class PreferItemRepositoryTest extends IntegrationTestSupport {
1717
@Autowired
1818
private PreferItemRepository preferItemRepository;
1919

20+
21+
private UUID centerId = UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1");
22+
private UUID centerId1 = UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1");
23+
private UUID centerId2 = UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a2");
24+
private UUID centerId3 = UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a3");
25+
2026
@DisplayName("기관의 id로 선호물품을 검색할 수 있다.")
2127
@Test
2228
void findByCenterId() {
2329

2430
//given
25-
PreferItem preferItem = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1"), "어린이 동화책");
26-
PreferItem preferItem1 = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1"), "간식");
27-
PreferItem preferItem2 = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a2"), "수건");
28-
PreferItem preferItem3 = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a3"), "식재료");
31+
PreferItem preferItem = PreferItem.create(centerId, "어린이 동화책");
32+
PreferItem preferItem1 = PreferItem.create(centerId1, "간식");
33+
PreferItem preferItem2 = PreferItem.create(centerId2, "수건");
34+
PreferItem preferItem3 = PreferItem.create(centerId3, "식재료");
2935
preferItemRepository.saveAll(List.of(preferItem, preferItem1, preferItem2, preferItem3));
3036

3137
//when
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.somemore.center.service.query;
2+
3+
import com.somemore.center.domain.PreferItem;
4+
import com.somemore.center.dto.response.PreferItemResponseDto;
5+
import com.somemore.center.repository.PreferItemRepository;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.junit.jupiter.MockitoExtension;
11+
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.mockito.Mockito.*;
17+
18+
@ExtendWith(MockitoExtension.class)
19+
class PreferItemQueryServiceTest {
20+
21+
@Mock
22+
private PreferItemRepository preferItemRepository;
23+
24+
@InjectMocks
25+
private PreferItemQueryService preferItemQueryService;
26+
27+
@Test
28+
void getPreferItemDtosByCenterId() {
29+
// Given
30+
UUID centerId = UUID.randomUUID();
31+
PreferItem preferItem1 = createPreferItem(centerId, "item1");
32+
PreferItem preferItem2 = createPreferItem(centerId, "item2");
33+
List<PreferItem> preferItems = List.of(preferItem1, preferItem2);
34+
35+
when(preferItemRepository.findByCenterId(centerId)).thenReturn(preferItems);
36+
37+
// When
38+
List<PreferItemResponseDto> result = preferItemQueryService.getPreferItemDtosByCenterId(centerId);
39+
40+
// Then
41+
assertThat(result).hasSize(2);
42+
assertThat(result.get(0).centerId()).isEqualTo(centerId);
43+
assertThat(result.get(0).itemName()).isEqualTo("item1");
44+
45+
assertThat(result.get(1).centerId()).isEqualTo(centerId);
46+
assertThat(result.get(1).itemName()).isEqualTo("item2");
47+
48+
verify(preferItemRepository, times(1)).findByCenterId(centerId);
49+
}
50+
51+
@Test
52+
void getPreferItemsByCenterId() {
53+
// Given
54+
UUID centerId = UUID.randomUUID();
55+
PreferItem preferItem1 = createPreferItem(centerId, "item1");
56+
PreferItem preferItem2 = createPreferItem(centerId, "item2");
57+
List<PreferItem> expectedItems = List.of(preferItem1, preferItem2);
58+
59+
when(preferItemRepository.findByCenterId(centerId)).thenReturn(expectedItems);
60+
61+
// When
62+
List<PreferItem> result = preferItemQueryService.getPreferItemsByCenterId(centerId);
63+
64+
// Then
65+
assertThat(result).hasSize(2).isEqualTo(expectedItems);
66+
verify(preferItemRepository, times(1)).findByCenterId(centerId);
67+
}
68+
69+
private PreferItem createPreferItem(UUID centerId, String itemName) {
70+
return PreferItem.builder()
71+
.centerId(centerId)
72+
.itemName(itemName)
73+
.build();
74+
}
75+
}

src/test/resources/application-test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ spring:
1919

2020
sql:
2121
init:
22-
mode: never
22+
mode: never
23+
24+
JWT_SECRET: 965b005bea929b5da041a3c8cf24ce0130880eb438942c159039f210aac07cc6857394c1253db2f4673f81e9485415764a086971620149072329179b800ea8e7

0 commit comments

Comments
 (0)