Skip to content

Commit d060988

Browse files
committed
feat: 기관 프로필 조회를 위한 기관 정보 조회 메서드 구현
- 필요한 컬럼 record 생성 - queryDsl 생성 - 조회 유스케이스 구현 - 테스트및 검증 완료
1 parent a303fe4 commit d060988

File tree

8 files changed

+210
-13
lines changed

8 files changed

+210
-13
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.repository.record.CenterProfileDto;
6+
import com.somemore.domains.center.dto.response.PreferItemResponseDto;
7+
import com.somemore.user.repository.usercommonattribute.record.UserProfileDto;
8+
import lombok.Builder;
9+
10+
import java.util.List;
11+
import java.util.UUID;
12+
13+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
14+
@Builder
15+
public record CenterProfileResponseDto(
16+
UUID id,
17+
UUID userId,
18+
String homepageUrl,
19+
String name,
20+
String contactNumber,
21+
String imgUrl,
22+
String introduce,
23+
List<PreferItemResponseDto> preferItems
24+
) {
25+
public static CenterProfileResponseDto of(CenterProfileDto centerProfileDto, UserProfileDto userProfileDto, List<PreferItemResponseDto> preferItems) {
26+
return CenterProfileResponseDto.builder()
27+
.id(centerProfileDto.id())
28+
.userId(centerProfileDto.userId())
29+
.homepageUrl(centerProfileDto.homepageUrl())
30+
.name(userProfileDto.name())
31+
.contactNumber(userProfileDto.contactNumber())
32+
.imgUrl(userProfileDto.imgUrl())
33+
.introduce(userProfileDto.introduce())
34+
.preferItems(preferItems)
35+
.build();
36+
}
37+
}

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

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

33
import com.somemore.center.domain.NEWCenter;
4+
import com.somemore.center.repository.record.CenterProfileDto;
45

56
import java.util.Optional;
67
import java.util.UUID;
@@ -12,4 +13,6 @@ public interface NEWCenterRepository {
1213
Optional<NEWCenter> findById(UUID id);
1314

1415
Optional<NEWCenter> findByUserId(UUID userId);
16+
17+
Optional<CenterProfileDto> findCenterProfileByUserId(UUID userId);
1518
}

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.somemore.center.repository;
22

3+
import com.querydsl.core.types.Projections;
4+
import com.querydsl.core.types.dsl.BooleanExpression;
35
import com.querydsl.jpa.impl.JPAQueryFactory;
46
import com.somemore.center.domain.NEWCenter;
57
import com.somemore.center.domain.QNEWCenter;
8+
import com.somemore.center.repository.record.CenterProfileDto;
69
import lombok.RequiredArgsConstructor;
710
import org.springframework.stereotype.Repository;
811

@@ -13,14 +16,14 @@
1316
@RequiredArgsConstructor
1417
public class NEWCenterRepositoryImpl implements NEWCenterRepository {
1518

16-
private final NEWCenterJpaRepository NEWCenterJpaRepository;
19+
private final NEWCenterJpaRepository centerJpaRepository;
1720
private final JPAQueryFactory queryFactory;
1821

1922
private static final QNEWCenter center = QNEWCenter.nEWCenter;
2023

2124
@Override
2225
public NEWCenter save(NEWCenter center) {
23-
return NEWCenterJpaRepository.save(center);
26+
return centerJpaRepository.save(center);
2427
}
2528

2629
@Override
@@ -29,7 +32,8 @@ public Optional<NEWCenter> findById(UUID id) {
2932
queryFactory.selectFrom(center)
3033
.where(
3134
center.id.eq(id),
32-
center.deleted.eq(false))
35+
isNotDeleted()
36+
)
3337
.fetchOne()
3438
);
3539
}
@@ -40,9 +44,30 @@ public Optional<NEWCenter> findByUserId(UUID userId) {
4044
queryFactory.selectFrom(center)
4145
.where(
4246
center.userId.eq(userId),
43-
center.deleted.eq(false))
47+
isNotDeleted()
48+
)
4449
.fetchOne()
4550

4651
);
4752
}
53+
54+
public Optional<CenterProfileDto> findCenterProfileByUserId(UUID userId) {
55+
return Optional.ofNullable(
56+
queryFactory.select(Projections.constructor(CenterProfileDto.class,
57+
center.id,
58+
center.userId,
59+
center.homepageUrl))
60+
.from(center)
61+
.where(
62+
center.userId.eq(userId),
63+
isNotDeleted()
64+
)
65+
.fetchOne()
66+
);
67+
}
68+
69+
private BooleanExpression isNotDeleted() {
70+
return center.deleted.isFalse();
71+
}
72+
4873
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.somemore.center.repository.record;
2+
3+
import java.util.UUID;
4+
5+
public record CenterProfileDto(
6+
UUID id,
7+
UUID userId,
8+
String homepageUrl
9+
) {
10+
}

src/main/java/com/somemore/center/service/NEWCenterQueryService.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package com.somemore.center.service;
22

33
import com.somemore.center.domain.NEWCenter;
4+
import com.somemore.center.dto.response.CenterProfileResponseDto;
45
import com.somemore.center.repository.NEWCenterRepository;
6+
import com.somemore.center.repository.record.CenterProfileDto;
57
import com.somemore.center.usecase.NEWCenterQueryUseCase;
8+
import com.somemore.domains.center.dto.response.PreferItemResponseDto;
9+
import com.somemore.domains.center.usecase.query.PreferItemQueryUseCase;
610
import com.somemore.global.exception.ExceptionMessage;
711
import com.somemore.global.exception.NoSuchElementException;
12+
import com.somemore.user.repository.usercommonattribute.record.UserProfileDto;
13+
import com.somemore.user.usecase.UserQueryUseCase;
814
import lombok.RequiredArgsConstructor;
915
import org.springframework.stereotype.Service;
1016
import org.springframework.transaction.annotation.Transactional;
1117

18+
import java.util.List;
1219
import java.util.UUID;
1320

1421
@Service
1522
@RequiredArgsConstructor
1623
@Transactional(readOnly = true)
1724
public class NEWCenterQueryService implements NEWCenterQueryUseCase {
1825

26+
private final UserQueryUseCase userQueryUseCase;
27+
private final PreferItemQueryUseCase preferItemQueryUseCase;
1928
private final NEWCenterRepository centerRepository;
2029

2130
@Override
@@ -28,4 +37,19 @@ public NEWCenter getByUserId(UUID userId) {
2837
public UUID getIdByUserId(UUID userId) {
2938
return getByUserId(userId).getId();
3039
}
40+
41+
@Override
42+
public CenterProfileResponseDto getCenterProfileByUserId(UUID userId) {
43+
44+
CenterProfileDto centerProfileDto = centerRepository.findCenterProfileByUserId(userId)
45+
.orElseThrow(() -> new NoSuchElementException(ExceptionMessage.NOT_EXISTS_CENTER));
46+
47+
UserProfileDto userProfileDto = userQueryUseCase.getUserProfileByUserId(userId);
48+
49+
List<PreferItemResponseDto> preferItems = preferItemQueryUseCase.getPreferItemDtosByCenterId(centerProfileDto.id());
50+
51+
return CenterProfileResponseDto.of(centerProfileDto, userProfileDto, preferItems);
52+
}
53+
54+
3155
}

src/main/java/com/somemore/center/usecase/NEWCenterQueryUseCase.java

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

33
import com.somemore.center.domain.NEWCenter;
4+
import com.somemore.center.dto.response.CenterProfileResponseDto;
45

56
import java.util.UUID;
67

@@ -9,4 +10,6 @@ public interface NEWCenterQueryUseCase {
910
NEWCenter getByUserId(UUID userId);
1011

1112
UUID getIdByUserId(UUID userId);
13+
14+
CenterProfileResponseDto getCenterProfileByUserId(UUID userId);
1215
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.somemore.center.repository;
22

33
import com.somemore.center.domain.NEWCenter;
4+
import com.somemore.center.repository.record.CenterProfileDto;
45
import com.somemore.support.IntegrationTestSupport;
56
import org.junit.jupiter.api.DisplayName;
67
import org.junit.jupiter.api.Test;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.transaction.annotation.Transactional;
910

11+
import java.util.Optional;
1012
import java.util.UUID;
1113

1214
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,4 +38,37 @@ void saveVolunteerByUserId() {
3638
.isEqualTo(centerById);
3739

3840
}
41+
42+
@DisplayName("유저 아이디로 기관 정보를 가져올 수 있다.")
43+
@Test
44+
void findCenterProfileByUserId() {
45+
46+
// given
47+
UUID userId = UUID.randomUUID();
48+
NEWCenter center = NEWCenter.createDefault(userId);
49+
centerRepository.save(center);
50+
51+
// when
52+
Optional<CenterProfileDto> result = centerRepository.findCenterProfileByUserId(userId);
53+
54+
// then
55+
assertThat(result).isPresent();
56+
assertThat(result.get())
57+
.extracting("id", "userId")
58+
.containsExactly(center.getId(), userId);
59+
}
60+
61+
@DisplayName("존재하지 않는 유저 아이디로 기관 정보 조회시 빈 값을 반환한다.")
62+
@Test
63+
void findCenterProfileByUserId_NoResult() {
64+
65+
// given
66+
UUID nonExistentUserId = UUID.randomUUID();
67+
68+
// when
69+
Optional<CenterProfileDto> result = centerRepository.findCenterProfileByUserId(nonExistentUserId);
70+
71+
// then
72+
assertThat(result).isEmpty();
73+
}
3974
}
Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package com.somemore.center.service;
22

33
import com.somemore.center.domain.NEWCenter;
4+
import com.somemore.center.dto.response.CenterProfileResponseDto;
45
import com.somemore.center.repository.NEWCenterRepository;
6+
import com.somemore.domains.center.domain.PreferItem;
7+
import com.somemore.domains.center.dto.response.PreferItemResponseDto;
8+
import com.somemore.domains.center.repository.preferitem.PreferItemJpaRepository;
9+
import com.somemore.domains.center.usecase.query.PreferItemQueryUseCase;
510
import com.somemore.support.IntegrationTestSupport;
6-
import org.junit.jupiter.api.BeforeEach;
11+
import com.somemore.user.domain.UserCommonAttribute;
12+
import com.somemore.user.repository.usercommonattribute.UserCommonAttributeRepository;
713
import org.junit.jupiter.api.DisplayName;
814
import org.junit.jupiter.api.Test;
915
import org.springframework.beans.factory.annotation.Autowired;
1016
import org.springframework.transaction.annotation.Transactional;
1117

18+
import java.util.List;
1219
import java.util.UUID;
1320

21+
import static com.somemore.user.domain.UserRole.CENTER;
1422
import static org.assertj.core.api.Assertions.assertThat;
1523

1624
@Transactional
@@ -22,29 +30,81 @@ class NEWCenterQueryServiceTest extends IntegrationTestSupport{
2230
@Autowired
2331
private NEWCenterRepository centerRepository;
2432

25-
UUID userId;
26-
NEWCenter center;
33+
@Autowired
34+
private PreferItemQueryUseCase preferItemQueryUseCase;
2735

28-
@BeforeEach
29-
void setUp() {
30-
userId = UUID.randomUUID();
31-
center = NEWCenter.createDefault(userId);
32-
centerRepository.save(center);
33-
}
36+
@Autowired
37+
private UserCommonAttributeRepository userCommonAttributeRepository;
38+
39+
@Autowired
40+
private PreferItemJpaRepository preferItemRepository;
3441

3542
@Test
3643
@DisplayName("사용자 ID로 기관을 조회한다")
3744
void getByUserId() {
45+
46+
//given
47+
UUID userId = UUID.randomUUID();
48+
NEWCenter center = NEWCenter.createDefault(userId);
49+
centerRepository.save(center);
50+
51+
//when
3852
NEWCenter foundCenter = centerQueryService.getByUserId(userId);
3953

54+
//then
4055
assertThat(foundCenter).isEqualTo(center);
4156
}
4257

4358
@Test
4459
@DisplayName("사용자 ID로 기관 ID를 조회한다")
4560
void getIdByUserId() {
61+
62+
//given
63+
UUID userId = UUID.randomUUID();
64+
NEWCenter center = NEWCenter.createDefault(userId);
65+
centerRepository.save(center);
66+
67+
// when
4668
UUID foundCenterId = centerQueryService.getIdByUserId(userId);
4769

70+
//then
4871
assertThat(foundCenterId).isEqualTo(center.getId());
4972
}
73+
74+
@DisplayName("기관의 유저 ID로 기관 정보를 조회할 수 있다.")
75+
@Test
76+
void getCenterProfileByUserId() {
77+
78+
//given
79+
UUID userId = UUID.randomUUID();
80+
NEWCenter center = NEWCenter.createDefault(userId);
81+
centerRepository.save(center);
82+
83+
UserCommonAttribute userCommonAttribute = UserCommonAttribute.createDefault(userId, CENTER);
84+
userCommonAttributeRepository.save(userCommonAttribute);
85+
86+
PreferItem preferItem = PreferItem.create(center.getId(), "어린이 동화책");
87+
PreferItem preferItem1 = PreferItem.create(center.getId(), "간식");
88+
preferItemRepository.saveAll(List.of(preferItem, preferItem1));
89+
90+
List<PreferItemResponseDto> preferItems = preferItemQueryUseCase.getPreferItemDtosByCenterId(center.getId());
91+
92+
//when
93+
CenterProfileResponseDto result = centerQueryService.getCenterProfileByUserId(userId);
94+
95+
//then
96+
assertThat(result).isNotNull();
97+
assertThat(result)
98+
.extracting("id", "userId", "homepageUrl", "name", "contactNumber", "imgUrl", "introduce", "preferItems")
99+
.containsExactly(
100+
center.getId(),
101+
userId,
102+
center.getHomepageUrl(),
103+
userCommonAttribute.getName(),
104+
userCommonAttribute.getContactNumber(),
105+
userCommonAttribute.getImgUrl(),
106+
userCommonAttribute.getIntroduce(),
107+
preferItems
108+
);
109+
}
50110
}

0 commit comments

Comments
 (0)