Skip to content

Commit c9dbfb6

Browse files
committed
feat: 기관 프로필 조회 기능 구현
- 기관 id로 기관 정보와 기관이 등록한 선호 물품을 조회하는 메서드 구현 - 레포지토리에 기관 id를 통한 단건 조회 메서드 추가 - 기관과 선호물품을 함께 반환하기 위한 응답Dto 구현 - 테스트 코드 작성및 검증 완료
1 parent 7006605 commit c9dbfb6

File tree

8 files changed

+164
-13
lines changed

8 files changed

+164
-13
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.somemore.center.dto.response;
2+
3+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
4+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5+
import com.somemore.center.domain.Center;
6+
import com.somemore.center.domain.PreferItem;
7+
import lombok.Builder;
8+
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
13+
@Builder
14+
public record CenterProfileResponseDto(
15+
UUID centerId,
16+
String name,
17+
String contactNumber,
18+
String imgUrl,
19+
String introduce,
20+
String homepageLink,
21+
List<PreferItem> preferItems
22+
) {
23+
public static CenterProfileResponseDto of(Center center, List<PreferItem> preferItems) {
24+
return CenterProfileResponseDto.builder()
25+
.centerId(center.getId())
26+
.name(center.getName())
27+
.contactNumber(center.getContactNumber())
28+
.imgUrl(center.getImgUrl())
29+
.introduce(center.getIntroduce())
30+
.homepageLink(center.getHomepageLink())
31+
.preferItems(preferItems)
32+
.build();
33+
}
34+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.Optional;
78
import java.util.UUID;
89

910
@Repository
@@ -14,4 +15,6 @@ public interface CenterRepository extends JpaRepository<Center, UUID> {
1415
default boolean doesNotExistById(UUID id) {
1516
return !existsById(id);
1617
}
18+
19+
Optional<Center> findCenterById(UUID id);
1720
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
package com.somemore.center.service.query;
2-
2+
import com.somemore.center.domain.Center;
3+
import com.somemore.center.domain.PreferItem;
4+
import com.somemore.center.dto.response.CenterProfileResponseDto;
35
import com.somemore.center.repository.CenterRepository;
46
import com.somemore.center.usecase.query.CenterQueryUseCase;
7+
import com.somemore.center.usecase.query.PreferItemQueryUseCase;
58
import com.somemore.global.exception.BadRequestException;
69
import lombok.RequiredArgsConstructor;
710
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
812

13+
import java.util.List;
914
import java.util.UUID;
1015

1116
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER;
1217

1318
@RequiredArgsConstructor
19+
@Transactional(readOnly = true)
1420
@Service
1521
public class CenterQueryService implements CenterQueryUseCase {
1622

23+
private final PreferItemQueryUseCase preferItemQueryUseCase;
1724
private final CenterRepository centerRepository;
1825

1926
@Override
20-
public void validateCenterExists(UUID id) {
27+
public CenterProfileResponseDto getCenterProfileByCenterId(UUID centerId) {
28+
29+
Center center = getCenterById(centerId);
30+
List<PreferItem> preferItems = preferItemQueryUseCase.getPreferItemsByCenterId(centerId);
31+
32+
return CenterProfileResponseDto.of(center, preferItems);
33+
}
2134

35+
private Center getCenterById(UUID centerId) {
36+
return centerRepository.findCenterById(centerId)
37+
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_CENTER.getMessage()));
38+
}
39+
40+
@Override
41+
public void validateCenterExists(UUID id) {
2242
if (centerRepository.doesNotExistById(id)) {
2343
throw new BadRequestException(NOT_EXISTS_CENTER.getMessage());
2444
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.somemore.center.service.query;
2+
3+
import com.somemore.center.domain.PreferItem;
4+
import com.somemore.center.repository.PreferItemRepository;
5+
import com.somemore.center.usecase.query.PreferItemQueryUseCase;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
@RequiredArgsConstructor
13+
@Service
14+
public class PreferItemQueryService implements PreferItemQueryUseCase {
15+
16+
private final PreferItemRepository preferItemRepository;
17+
18+
@Override
19+
public List<PreferItem> getPreferItemsByCenterId(UUID centerId) {
20+
return preferItemRepository.findByCenterId(centerId);
21+
}
22+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.somemore.center.usecase.query;
22

3+
import com.somemore.center.dto.response.CenterProfileResponseDto;
4+
35
import java.util.UUID;
46

57
public interface CenterQueryUseCase {
68

7-
void validateCenterExists(UUID id);
9+
CenterProfileResponseDto getCenterProfileByCenterId(UUID centerId);
10+
void validateCenterExists(UUID centerId);
811
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.somemore.center.usecase.query;
2+
3+
import com.somemore.center.domain.PreferItem;
4+
5+
import java.util.List;
6+
import java.util.UUID;
7+
8+
public interface PreferItemQueryUseCase {
9+
List<PreferItem> getPreferItemsByCenterId(UUID centerId);
10+
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.jupiter.api.Test;
88
import org.springframework.beans.factory.annotation.Autowired;
99

10+
import java.util.Optional;
1011
import java.util.UUID;
1112

1213
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@@ -17,7 +18,7 @@ class CenterRepositoryTest extends IntegrationTestSupport {
1718
@Autowired
1819
private CenterRepository centerRepository;
1920

20-
@DisplayName("기관 id로 기관 프로필을 조회할 수 있다.")
21+
@DisplayName("기관 id로 기관 정보를 조회할 수 있다. (Repository)")
2122
@Test
2223
void findById() {
2324
//given
@@ -33,17 +34,17 @@ void findById() {
3334
centerRepository.save(center);
3435

3536
//when
36-
Center foundCenter = centerRepository.findById(center.getId()).orElse(null);
37+
Optional<Center> foundCenter = centerRepository.findCenterById(center.getId());
3738

3839
//then
3940
assertThat(foundCenter).isNotNull();
40-
assertThat(foundCenter.getName()).isEqualTo("기본 기관 이름");
41-
assertThat(foundCenter.getContactNumber()).isEqualTo("010-1234-5678");
42-
assertThat(foundCenter.getImgUrl()).isEqualTo("http://example.com/image.jpg");
43-
assertThat(foundCenter.getIntroduce()).isEqualTo("기관 소개 내용");
44-
assertThat(foundCenter.getHomepageLink()).isEqualTo("http://example.com");
45-
assertThat(foundCenter.getAccountId()).isEqualTo("account123");
46-
assertThat(foundCenter.getAccountPw()).isEqualTo("password123");
41+
assertThat(foundCenter.get().getName()).isEqualTo("기본 기관 이름");
42+
assertThat(foundCenter.get().getContactNumber()).isEqualTo("010-1234-5678");
43+
assertThat(foundCenter.get().getImgUrl()).isEqualTo("http://example.com/image.jpg");
44+
assertThat(foundCenter.get().getIntroduce()).isEqualTo("기관 소개 내용");
45+
assertThat(foundCenter.get().getHomepageLink()).isEqualTo("http://example.com");
46+
assertThat(foundCenter.get().getAccountId()).isEqualTo("account123");
47+
assertThat(foundCenter.get().getAccountPw()).isEqualTo("password123");
4748
}
4849

4950
@DisplayName("기관 id로 기관이 존재하는지 확인할 수 있다.")

src/test/java/com/somemore/center/service/query/CenterQueryServiceTest.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,86 @@
22

33
import com.somemore.IntegrationTestSupport;
44
import com.somemore.center.domain.Center;
5+
import com.somemore.center.domain.PreferItem;
6+
import com.somemore.center.dto.response.CenterProfileResponseDto;
57
import com.somemore.center.repository.CenterRepository;
8+
import com.somemore.center.repository.PreferItemRepository;
69
import com.somemore.global.exception.BadRequestException;
710
import com.somemore.global.exception.ExceptionMessage;
11+
import jakarta.transaction.Transactional;
812
import org.assertj.core.api.ThrowableAssert;
913
import org.junit.jupiter.api.DisplayName;
1014
import org.junit.jupiter.api.Test;
1115
import org.springframework.beans.factory.annotation.Autowired;
1216

17+
import java.util.List;
1318
import java.util.UUID;
1419

20+
import static org.assertj.core.api.Assertions.assertThat;
1521
import static org.assertj.core.api.Assertions.assertThatCode;
1622
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1723

24+
@Transactional
1825
class CenterQueryServiceTest extends IntegrationTestSupport {
1926

2027
@Autowired
2128
private CenterQueryService centerQueryService;
2229

2330
@Autowired
2431
private CenterRepository centerRepository;
32+
33+
@Autowired
34+
private PreferItemRepository preferItemRepository;
35+
36+
@DisplayName("기관 Id로 기관 프로필을 조회할 수 있다. (service)")
37+
@Test
38+
void getCenterProfileById() {
39+
// given
40+
Center center = Center.create(
41+
"기본 기관 이름",
42+
"010-1234-5678",
43+
"http://example.com/image.jpg",
44+
"기관 소개 내용",
45+
"http://example.com",
46+
"account123",
47+
"password123"
48+
);
49+
Center foundCenter = centerRepository.save(center);
50+
51+
PreferItem preferItem = PreferItem.create(foundCenter.getId(), "어린이 동화책");
52+
PreferItem preferItem1 = PreferItem.create(foundCenter.getId(), "간식");
53+
preferItemRepository.saveAll(List.of(preferItem, preferItem1));
54+
55+
// when
56+
CenterProfileResponseDto responseDto = centerQueryService.getCenterProfileByCenterId(foundCenter.getId());
57+
58+
// then
59+
assertThat(responseDto)
60+
.extracting(
61+
"centerId",
62+
"name",
63+
"contactNumber",
64+
"imgUrl",
65+
"introduce",
66+
"homepageLink"
67+
)
68+
.containsExactly(
69+
foundCenter.getId(),
70+
"기본 기관 이름",
71+
"010-1234-5678",
72+
"http://example.com/image.jpg",
73+
"기관 소개 내용",
74+
"http://example.com"
75+
);
76+
77+
assertThat(responseDto.preferItems())
78+
.hasSize(2)
79+
.extracting("itemName")
80+
.containsExactly("어린이 동화책", "간식");
81+
}
82+
2583

26-
@DisplayName("존재하지 않는 기관 ID로 조회 시 예외가 발생한다")
84+
@DisplayName("존재하지 않는 기관 ID를 검증할 수 있다.")
2785
@Test
2886
void validateNonExistentCenter() {
2987
// given

0 commit comments

Comments
 (0)