diff --git a/src/main/java/com/somemore/domains/center/controller/CenterQueryApiController.java b/src/main/java/com/somemore/center/controller/CenterQueryApiController.java similarity index 77% rename from src/main/java/com/somemore/domains/center/controller/CenterQueryApiController.java rename to src/main/java/com/somemore/center/controller/CenterQueryApiController.java index 7f6b7d685..ca85094b1 100644 --- a/src/main/java/com/somemore/domains/center/controller/CenterQueryApiController.java +++ b/src/main/java/com/somemore/center/controller/CenterQueryApiController.java @@ -1,7 +1,7 @@ -package com.somemore.domains.center.controller; +package com.somemore.center.controller; -import com.somemore.domains.center.dto.response.CenterProfileResponseDto; -import com.somemore.domains.center.usecase.query.CenterQueryUseCase; +import com.somemore.center.dto.response.CenterProfileResponseDto; +import com.somemore.center.usecase.NEWCenterQueryUseCase; import com.somemore.global.common.response.ApiResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -19,13 +19,13 @@ @Tag(name = "Center Query API", description = "기관 관련 조회 API를 제공합니다.") public class CenterQueryApiController { - private final CenterQueryUseCase centerQueryUseCase; + private final NEWCenterQueryUseCase centerQueryUseCase; @Operation(summary = "기관 프로필 조회 API") @GetMapping("/profile/{centerId}") public ApiResponse getCenterProfile(@PathVariable UUID centerId) { - CenterProfileResponseDto responseDto = centerQueryUseCase.getCenterProfileByCenterId(centerId); + CenterProfileResponseDto responseDto = centerQueryUseCase.getCenterProfileById(centerId); return ApiResponse.ok(200, responseDto, "기관 프로필 조회 성공"); } diff --git a/src/main/java/com/somemore/center/dto/response/CenterProfileResponseDto.java b/src/main/java/com/somemore/center/dto/response/CenterProfileResponseDto.java new file mode 100644 index 000000000..f8497d355 --- /dev/null +++ b/src/main/java/com/somemore/center/dto/response/CenterProfileResponseDto.java @@ -0,0 +1,54 @@ +package com.somemore.center.dto.response; + +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import com.somemore.center.repository.record.CenterProfileDto; +import com.somemore.domains.center.dto.response.PreferItemResponseDto; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; + +import java.util.List; +import java.util.UUID; + +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +@Builder +public record CenterProfileResponseDto( + + @Schema(description = "기관 ID", example = "123e4567-e89b-12d3-a456-426614174000") + UUID id, + + @Schema(description = "유저 ID", example = "123e4567-e89b-12d3-a456-426614174000") + UUID userId, + + @Schema(description = "기관 홈페이지 링크", example = "https://fitnesscenter.com") + String homepageUrl, + + @Schema(description = "기관 이름", example = "서울 도서관") + String name, + + @Schema(description = "연락처", example = "010-xxxx-xxxx") + String contactNumber, + + @Schema(description = "기관 프로필 이미지 URL", example = "https://example.com/images/center.jpg") + String imgUrl, + + @Schema(description = "기관 소개", example = "저희 도서관은 유명해요") + String introduce, + + @Schema(description = "선호 물품 리스트") + List preferItems +) { + public static CenterProfileResponseDto of(CenterProfileDto centerProfileDto, UserProfileDto userProfileDto, List preferItems) { + return CenterProfileResponseDto.builder() + .id(centerProfileDto.id()) + .userId(centerProfileDto.userId()) + .homepageUrl(centerProfileDto.homepageUrl()) + .name(userProfileDto.name()) + .contactNumber(userProfileDto.contactNumber()) + .imgUrl(userProfileDto.imgUrl()) + .introduce(userProfileDto.introduce()) + .preferItems(preferItems) + .build(); + } +} diff --git a/src/main/java/com/somemore/center/repository/NEWCenterRepository.java b/src/main/java/com/somemore/center/repository/NEWCenterRepository.java index 4c0cb9d82..c8c0d4718 100644 --- a/src/main/java/com/somemore/center/repository/NEWCenterRepository.java +++ b/src/main/java/com/somemore/center/repository/NEWCenterRepository.java @@ -1,6 +1,7 @@ package com.somemore.center.repository; import com.somemore.center.domain.NEWCenter; +import com.somemore.center.repository.record.CenterProfileDto; import java.util.Optional; import java.util.UUID; @@ -12,4 +13,6 @@ public interface NEWCenterRepository { Optional findById(UUID id); Optional findByUserId(UUID userId); + + Optional findCenterProfileById(UUID centerId); } diff --git a/src/main/java/com/somemore/center/repository/NEWCenterRepositoryImpl.java b/src/main/java/com/somemore/center/repository/NEWCenterRepositoryImpl.java index 608435c3f..7f6944945 100644 --- a/src/main/java/com/somemore/center/repository/NEWCenterRepositoryImpl.java +++ b/src/main/java/com/somemore/center/repository/NEWCenterRepositoryImpl.java @@ -1,8 +1,11 @@ package com.somemore.center.repository; +import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; import com.somemore.center.domain.NEWCenter; import com.somemore.center.domain.QNEWCenter; +import com.somemore.center.repository.record.CenterProfileDto; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; @@ -13,14 +16,14 @@ @RequiredArgsConstructor public class NEWCenterRepositoryImpl implements NEWCenterRepository { - private final NEWCenterJpaRepository NEWCenterJpaRepository; + private final NEWCenterJpaRepository centerJpaRepository; private final JPAQueryFactory queryFactory; private static final QNEWCenter center = QNEWCenter.nEWCenter; @Override public NEWCenter save(NEWCenter center) { - return NEWCenterJpaRepository.save(center); + return centerJpaRepository.save(center); } @Override @@ -29,7 +32,8 @@ public Optional findById(UUID id) { queryFactory.selectFrom(center) .where( center.id.eq(id), - center.deleted.eq(false)) + isNotDeleted() + ) .fetchOne() ); } @@ -40,9 +44,31 @@ public Optional findByUserId(UUID userId) { queryFactory.selectFrom(center) .where( center.userId.eq(userId), - center.deleted.eq(false)) + isNotDeleted() + ) .fetchOne() ); } + + @Override + public Optional findCenterProfileById(UUID centerId) { + return Optional.ofNullable( + queryFactory.select(Projections.constructor(CenterProfileDto.class, + center.id, + center.userId, + center.homepageUrl)) + .from(center) + .where( + center.id.eq(centerId), + isNotDeleted() + ) + .fetchOne() + ); + } + + private BooleanExpression isNotDeleted() { + return center.deleted.isFalse(); + } + } diff --git a/src/main/java/com/somemore/center/repository/record/CenterProfileDto.java b/src/main/java/com/somemore/center/repository/record/CenterProfileDto.java new file mode 100644 index 000000000..ac74a7eaf --- /dev/null +++ b/src/main/java/com/somemore/center/repository/record/CenterProfileDto.java @@ -0,0 +1,10 @@ +package com.somemore.center.repository.record; + +import java.util.UUID; + +public record CenterProfileDto( + UUID id, + UUID userId, + String homepageUrl +) { +} diff --git a/src/main/java/com/somemore/center/service/NEWCenterQueryService.java b/src/main/java/com/somemore/center/service/NEWCenterQueryService.java index 01dfe7397..b7cd0b777 100644 --- a/src/main/java/com/somemore/center/service/NEWCenterQueryService.java +++ b/src/main/java/com/somemore/center/service/NEWCenterQueryService.java @@ -1,14 +1,21 @@ package com.somemore.center.service; import com.somemore.center.domain.NEWCenter; +import com.somemore.center.dto.response.CenterProfileResponseDto; import com.somemore.center.repository.NEWCenterRepository; +import com.somemore.center.repository.record.CenterProfileDto; import com.somemore.center.usecase.NEWCenterQueryUseCase; +import com.somemore.domains.center.dto.response.PreferItemResponseDto; +import com.somemore.domains.center.usecase.query.PreferItemQueryUseCase; import com.somemore.global.exception.ExceptionMessage; import com.somemore.global.exception.NoSuchElementException; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; +import com.somemore.user.usecase.UserQueryUseCase; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.UUID; @Service @@ -16,6 +23,8 @@ @Transactional(readOnly = true) public class NEWCenterQueryService implements NEWCenterQueryUseCase { + private final UserQueryUseCase userQueryUseCase; + private final PreferItemQueryUseCase preferItemQueryUseCase; private final NEWCenterRepository centerRepository; @Override @@ -28,4 +37,19 @@ public NEWCenter getByUserId(UUID userId) { public UUID getIdByUserId(UUID userId) { return getByUserId(userId).getId(); } + + @Override + public CenterProfileResponseDto getCenterProfileById(UUID centerId) { + + CenterProfileDto centerProfileDto = centerRepository.findCenterProfileById(centerId) + .orElseThrow(() -> new NoSuchElementException(ExceptionMessage.NOT_EXISTS_CENTER)); + + UserProfileDto userProfileDto = userQueryUseCase.getUserProfileByUserId(centerProfileDto.userId()); + + List preferItems = preferItemQueryUseCase.getPreferItemDtosByCenterId(centerProfileDto.id()); + + return CenterProfileResponseDto.of(centerProfileDto, userProfileDto, preferItems); + } + + } diff --git a/src/main/java/com/somemore/center/usecase/NEWCenterQueryUseCase.java b/src/main/java/com/somemore/center/usecase/NEWCenterQueryUseCase.java index 9131eee7f..ad99ee9fa 100644 --- a/src/main/java/com/somemore/center/usecase/NEWCenterQueryUseCase.java +++ b/src/main/java/com/somemore/center/usecase/NEWCenterQueryUseCase.java @@ -1,6 +1,7 @@ package com.somemore.center.usecase; import com.somemore.center.domain.NEWCenter; +import com.somemore.center.dto.response.CenterProfileResponseDto; import java.util.UUID; @@ -9,4 +10,6 @@ public interface NEWCenterQueryUseCase { NEWCenter getByUserId(UUID userId); UUID getIdByUserId(UUID userId); + + CenterProfileResponseDto getCenterProfileById(UUID centerId); } diff --git a/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepository.java b/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepository.java index ec73318f1..6511ff36d 100644 --- a/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepository.java +++ b/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepository.java @@ -1,6 +1,7 @@ package com.somemore.user.repository.usercommonattribute; import com.somemore.user.domain.UserCommonAttribute; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; import java.util.Optional; import java.util.UUID; @@ -12,4 +13,6 @@ public interface UserCommonAttributeRepository { UserCommonAttribute save(UserCommonAttribute userCommonAttribute); Optional findIsCustomizedByUserId(UUID userId); + + Optional findUserProfileByUserId(UUID userId); } diff --git a/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImpl.java b/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImpl.java index b76c478e6..e34c8d1c6 100644 --- a/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImpl.java +++ b/src/main/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImpl.java @@ -1,9 +1,11 @@ package com.somemore.user.repository.usercommonattribute; +import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; import com.somemore.user.domain.QUserCommonAttribute; import com.somemore.user.domain.UserCommonAttribute; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; @@ -46,6 +48,26 @@ public Optional findIsCustomizedByUserId(UUID userId) { ); } + @Override + public Optional findUserProfileByUserId(UUID userId) { + return Optional.ofNullable( + queryFactory.select(Projections.constructor(UserProfileDto.class, + userCommonAttribute.id, + userCommonAttribute.userId, + userCommonAttribute.name, + userCommonAttribute.contactNumber, + userCommonAttribute.imgUrl, + userCommonAttribute.introduce + )) + .from(userCommonAttribute) + .where( + eqUserId(userId), + isNotDeleted() + ) + .fetchOne() + ); + } + private static BooleanExpression eqUserId(UUID userId) { return userCommonAttribute.userId.eq(userId); } diff --git a/src/main/java/com/somemore/user/repository/usercommonattribute/record/UserProfileDto.java b/src/main/java/com/somemore/user/repository/usercommonattribute/record/UserProfileDto.java new file mode 100644 index 000000000..a5ef351a9 --- /dev/null +++ b/src/main/java/com/somemore/user/repository/usercommonattribute/record/UserProfileDto.java @@ -0,0 +1,13 @@ +package com.somemore.user.repository.usercommonattribute.record; + +import java.util.UUID; + +public record UserProfileDto( + Long id, + UUID userId, + String name, + String contactNumber, + String imgUrl, + String introduce +) { +} diff --git a/src/main/java/com/somemore/user/service/UserQueryService.java b/src/main/java/com/somemore/user/service/UserQueryService.java index 2ce9ebf30..af4391d39 100644 --- a/src/main/java/com/somemore/user/service/UserQueryService.java +++ b/src/main/java/com/somemore/user/service/UserQueryService.java @@ -6,6 +6,7 @@ import com.somemore.user.domain.UserRole; import com.somemore.user.repository.user.UserRepository; import com.somemore.user.repository.usercommonattribute.UserCommonAttributeRepository; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; import com.somemore.user.usecase.UserQueryUseCase; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -60,4 +61,11 @@ public boolean isDuplicateAccountId(String accountId) { return userRepository.existsByAccountId(accountId); } + @Override + public UserProfileDto getUserProfileByUserId(UUID userId) { + + return userCommonAttributeRepository.findUserProfileByUserId(userId) + .orElseThrow(() -> new NoSuchElementException(NOT_EXIST_USER)); + } + } diff --git a/src/main/java/com/somemore/user/usecase/UserQueryUseCase.java b/src/main/java/com/somemore/user/usecase/UserQueryUseCase.java index f41056b07..0f69da41f 100644 --- a/src/main/java/com/somemore/user/usecase/UserQueryUseCase.java +++ b/src/main/java/com/somemore/user/usecase/UserQueryUseCase.java @@ -3,6 +3,7 @@ import com.somemore.user.domain.User; import com.somemore.user.domain.UserCommonAttribute; import com.somemore.user.domain.UserRole; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; import java.util.UUID; @@ -19,4 +20,6 @@ public interface UserQueryUseCase { boolean getIsCustomizedByUserId(UUID userId); boolean isDuplicateAccountId(String accountId); + + UserProfileDto getUserProfileByUserId(UUID userId); } diff --git a/src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java b/src/test/java/com/somemore/center/controller/CenterQueryApiControllerTest.java similarity index 63% rename from src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java rename to src/test/java/com/somemore/center/controller/CenterQueryApiControllerTest.java index ea408c8f6..fa77d59d5 100644 --- a/src/test/java/com/somemore/domains/center/controller/CenterQueryApiControllerTest.java +++ b/src/test/java/com/somemore/center/controller/CenterQueryApiControllerTest.java @@ -1,4 +1,4 @@ -package com.somemore.domains.center.controller; +package com.somemore.center.controller; import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER; import static org.mockito.Mockito.times; @@ -8,8 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import com.somemore.domains.center.dto.response.CenterProfileResponseDto; -import com.somemore.domains.center.usecase.query.CenterQueryUseCase; +import com.somemore.center.dto.response.CenterProfileResponseDto; +import com.somemore.center.usecase.NEWCenterQueryUseCase; import com.somemore.global.exception.BadRequestException; import com.somemore.support.ControllerTestSupport; import java.util.List; @@ -23,7 +23,7 @@ class CenterQueryApiControllerTest extends ControllerTestSupport { @MockBean - protected CenterQueryUseCase centerQueryUseCase; + protected NEWCenterQueryUseCase centerQueryUseCase; private UUID centerId; private CenterProfileResponseDto responseDto; @@ -32,12 +32,13 @@ class CenterQueryApiControllerTest extends ControllerTestSupport { void setUp() { centerId = UUID.randomUUID(); responseDto = CenterProfileResponseDto.builder() - .centerId(centerId) + .id(centerId) + .userId(UUID.randomUUID()) + .homepageUrl("http://example.com") .name("Test Center") .contactNumber("010-1234-5678") .imgUrl("http://example.com/image.jpg") .introduce("This is a test center.") - .homepageLink("http://example.com") .preferItems(List.of()) .build(); } @@ -46,9 +47,9 @@ void setUp() { @Test void getCenterProfile() throws Exception { // given - when(centerQueryUseCase.getCenterProfileByCenterId(centerId)).thenReturn(responseDto); + when(centerQueryUseCase.getCenterProfileById(centerId)).thenReturn(responseDto); - // when // then + // when & then mockMvc.perform( get("/api/center/profile/{centerId}", centerId) .contentType(MediaType.APPLICATION_JSON) @@ -56,39 +57,36 @@ void getCenterProfile() throws Exception { .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value("200")) .andExpect(jsonPath("$.message").value("기관 프로필 조회 성공")) - .andExpect(jsonPath("$.data.center_id").value(centerId.toString())) // center_id로 수정 - .andExpect(jsonPath("$.data.name").value("Test Center")) - .andExpect(jsonPath("$.data.contact_number").value( - "010-1234-5678")) // contact_number로 수정 - .andExpect(jsonPath("$.data.img_url").value( - "http://example.com/image.jpg")) // img_url로 수정 - .andExpect(jsonPath("$.data.introduce").value("This is a test center.")) - .andExpect(jsonPath("$.data.homepage_link").value( - "http://example.com")) // homepage_link로 수정 - .andExpect(jsonPath("$.data.prefer_items").isArray()); // prefer_items로 수정 + .andExpect(jsonPath("$.data.id").value(centerId.toString())) + .andExpect(jsonPath("$.data.user_id").value(responseDto.userId().toString())) + .andExpect(jsonPath("$.data.homepage_url").value(responseDto.homepageUrl())) + .andExpect(jsonPath("$.data.name").value(responseDto.name())) + .andExpect(jsonPath("$.data.contact_number").value(responseDto.contactNumber())) + .andExpect(jsonPath("$.data.img_url").value(responseDto.imgUrl())) + .andExpect(jsonPath("$.data.introduce").value(responseDto.introduce())) + .andExpect(jsonPath("$.data.prefer_items").isArray()); - verify(centerQueryUseCase, times(1)).getCenterProfileByCenterId(centerId); + verify(centerQueryUseCase, times(1)).getCenterProfileById(centerId); } - @DisplayName("존재하지 않는 기관 ID로 조회 시 예외가 발생한다. (controller)") + @DisplayName("존재하지 않는 유저 ID로 조회 시 예외가 발생한다. (controller)") @Test void getCenterProfile_NotFound() throws Exception { // given - UUID nonExistentCenterId = UUID.randomUUID(); - when(centerQueryUseCase.getCenterProfileByCenterId(nonExistentCenterId)) + UUID nonExistentuserId = UUID.randomUUID(); + when(centerQueryUseCase.getCenterProfileById(nonExistentuserId)) .thenThrow(new BadRequestException(NOT_EXISTS_CENTER.getMessage())); - // when // then + // when & then mockMvc.perform( - get("/api/center/profile/{centerId}", nonExistentCenterId) + get("/api/center/profile/{centerId}", nonExistentuserId) .contentType(MediaType.APPLICATION_JSON) ) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.status").value("400")) .andExpect(jsonPath("$.detail").value("존재하지 않는 기관입니다.")); - verify(centerQueryUseCase, times(1)).getCenterProfileByCenterId(nonExistentCenterId); + verify(centerQueryUseCase, times(1)).getCenterProfileById(nonExistentuserId); } } - diff --git a/src/test/java/com/somemore/center/repository/NEWCenterRepositoryImplTest.java b/src/test/java/com/somemore/center/repository/NEWCenterRepositoryImplTest.java index e18244c5d..650797179 100644 --- a/src/test/java/com/somemore/center/repository/NEWCenterRepositoryImplTest.java +++ b/src/test/java/com/somemore/center/repository/NEWCenterRepositoryImplTest.java @@ -1,12 +1,14 @@ package com.somemore.center.repository; import com.somemore.center.domain.NEWCenter; +import com.somemore.center.repository.record.CenterProfileDto; import com.somemore.support.IntegrationTestSupport; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.Optional; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -36,4 +38,37 @@ void saveVolunteerByUserId() { .isEqualTo(centerById); } + + @DisplayName("기관 아이디로 기관 프로필 정보를 가져올 수 있다.") + @Test + void findCenterProfileByUserId() { + + // given + UUID userId = UUID.randomUUID(); + NEWCenter center = NEWCenter.createDefault(userId); + centerRepository.save(center); + + // when + Optional result = centerRepository.findCenterProfileById(center.getId()); + + // then + assertThat(result).isPresent(); + assertThat(result.get()) + .extracting("id", "userId") + .containsExactly(center.getId(), userId); + } + + @DisplayName("존재하지 않는 유저 아이디로 기관 정보 조회시 빈 값을 반환한다.") + @Test + void findCenterProfileByUserId_NoResult() { + + // given + UUID nonExistentCenterId = UUID.randomUUID(); + + // when + Optional result = centerRepository.findCenterProfileById(nonExistentCenterId); + + // then + assertThat(result).isEmpty(); + } } diff --git a/src/test/java/com/somemore/center/service/NEWCenterQueryServiceTest.java b/src/test/java/com/somemore/center/service/NEWCenterQueryServiceTest.java index e388f43c3..1f219027d 100644 --- a/src/test/java/com/somemore/center/service/NEWCenterQueryServiceTest.java +++ b/src/test/java/com/somemore/center/service/NEWCenterQueryServiceTest.java @@ -1,16 +1,24 @@ package com.somemore.center.service; import com.somemore.center.domain.NEWCenter; +import com.somemore.center.dto.response.CenterProfileResponseDto; import com.somemore.center.repository.NEWCenterRepository; +import com.somemore.domains.center.domain.PreferItem; +import com.somemore.domains.center.dto.response.PreferItemResponseDto; +import com.somemore.domains.center.repository.preferitem.PreferItemJpaRepository; +import com.somemore.domains.center.usecase.query.PreferItemQueryUseCase; import com.somemore.support.IntegrationTestSupport; -import org.junit.jupiter.api.BeforeEach; +import com.somemore.user.domain.UserCommonAttribute; +import com.somemore.user.repository.usercommonattribute.UserCommonAttributeRepository; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.UUID; +import static com.somemore.user.domain.UserRole.CENTER; import static org.assertj.core.api.Assertions.assertThat; @Transactional @@ -22,29 +30,81 @@ class NEWCenterQueryServiceTest extends IntegrationTestSupport{ @Autowired private NEWCenterRepository centerRepository; - UUID userId; - NEWCenter center; + @Autowired + private PreferItemQueryUseCase preferItemQueryUseCase; - @BeforeEach - void setUp() { - userId = UUID.randomUUID(); - center = NEWCenter.createDefault(userId); - centerRepository.save(center); - } + @Autowired + private UserCommonAttributeRepository userCommonAttributeRepository; + + @Autowired + private PreferItemJpaRepository preferItemRepository; @Test @DisplayName("사용자 ID로 기관을 조회한다") void getByUserId() { + + //given + UUID userId = UUID.randomUUID(); + NEWCenter center = NEWCenter.createDefault(userId); + centerRepository.save(center); + + //when NEWCenter foundCenter = centerQueryService.getByUserId(userId); + //then assertThat(foundCenter).isEqualTo(center); } @Test @DisplayName("사용자 ID로 기관 ID를 조회한다") void getIdByUserId() { + + //given + UUID userId = UUID.randomUUID(); + NEWCenter center = NEWCenter.createDefault(userId); + centerRepository.save(center); + + // when UUID foundCenterId = centerQueryService.getIdByUserId(userId); + //then assertThat(foundCenterId).isEqualTo(center.getId()); } + + @DisplayName("기관의 유저 ID로 기관 정보를 조회할 수 있다.") + @Test + void getCenterProfileByUserId() { + + //given + UUID userId = UUID.randomUUID(); + NEWCenter center = NEWCenter.createDefault(userId); + centerRepository.save(center); + + UserCommonAttribute userCommonAttribute = UserCommonAttribute.createDefault(userId, CENTER); + userCommonAttributeRepository.save(userCommonAttribute); + + PreferItem preferItem = PreferItem.create(center.getId(), "어린이 동화책"); + PreferItem preferItem1 = PreferItem.create(center.getId(), "간식"); + preferItemRepository.saveAll(List.of(preferItem, preferItem1)); + + List preferItems = preferItemQueryUseCase.getPreferItemDtosByCenterId(center.getId()); + + //when + CenterProfileResponseDto result = centerQueryService.getCenterProfileById(center.getId()); + + //then + assertThat(result).isNotNull(); + assertThat(result) + .extracting("id", "userId", "homepageUrl", "name", "contactNumber", "imgUrl", "introduce", "preferItems") + .containsExactly( + center.getId(), + userId, + center.getHomepageUrl(), + userCommonAttribute.getName(), + userCommonAttribute.getContactNumber(), + userCommonAttribute.getImgUrl(), + userCommonAttribute.getIntroduce(), + preferItems + ); + } } diff --git a/src/test/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImplTest.java b/src/test/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImplTest.java index 8293a11e3..fda71de8c 100644 --- a/src/test/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImplTest.java +++ b/src/test/java/com/somemore/user/repository/usercommonattribute/UserCommonAttributeRepositoryImplTest.java @@ -4,11 +4,13 @@ import com.somemore.support.IntegrationTestSupport; import com.somemore.user.domain.UserCommonAttribute; import com.somemore.user.domain.UserRole; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.Optional; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -39,4 +41,27 @@ void saveUserCommonAttribute() { assertThat(savedUserCommonAttribute.getImgUrl()).isEqualTo(ImageUploadService.DEFAULT_IMAGE_URL); assertThat(savedUserCommonAttribute.isCustomized()).isFalse(); } + + @DisplayName("유저 ID로 프로필에 필요한 유저 정보를 조회할 수 있다.") + @Test + void findUserProfileByUserId() { + // given + UUID userId = UUID.randomUUID(); + UserCommonAttribute userCommonAttribute = UserCommonAttribute.createDefault(userId, UserRole.VOLUNTEER); + userCommonAttributeRepository.save(userCommonAttribute); + + // when + Optional result = userCommonAttributeRepository.findUserProfileByUserId(userId); + + // then + assertThat(result).isPresent(); + UserProfileDto userProfile = result.get(); + + assertThat(userProfile.id()).isNotNull(); + assertThat(userProfile.userId()).isEqualTo(userId); + assertThat(userProfile.name()).isEqualTo(userCommonAttribute.getName()); + assertThat(userProfile.contactNumber()).isEqualTo(userCommonAttribute.getContactNumber()); + assertThat(userProfile.imgUrl()).isEqualTo(userCommonAttribute.getImgUrl()); + assertThat(userProfile.introduce()).isEqualTo(userCommonAttribute.getIntroduce()); + } } diff --git a/src/test/java/com/somemore/user/service/UserQueryServiceTest.java b/src/test/java/com/somemore/user/service/UserQueryServiceTest.java index 11c3aa0cc..d7522570f 100644 --- a/src/test/java/com/somemore/user/service/UserQueryServiceTest.java +++ b/src/test/java/com/somemore/user/service/UserQueryServiceTest.java @@ -8,12 +8,15 @@ import com.somemore.user.dto.UserAuthInfo; import com.somemore.user.repository.user.UserRepository; import com.somemore.user.repository.usercommonattribute.UserCommonAttributeRepository; +import com.somemore.user.repository.usercommonattribute.record.UserProfileDto; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.UUID; + import static org.assertj.core.api.Assertions.assertThat; @Transactional @@ -108,4 +111,28 @@ void getIsCustomizedByUserId_ReturnsFalse_WhenDefaultValue() { // then assertThat(isCustomized).isFalse(); } + + @DisplayName("유저 id로 유저 프로필에 필요한 공통 속성을 조회할 수 있다.") + @Test + void getUserProfileByUserId() { + + //given + UUID userId = UUID.randomUUID(); + UserRole role = UserRole.VOLUNTEER; + + UserCommonAttribute userCommonAttribute1 = UserCommonAttribute.createDefault(userId,role); + userCommonAttributeRepository.save(userCommonAttribute1); + + //when + UserProfileDto result = userQueryService.getUserProfileByUserId(userId); + + //then + assertThat(result).isNotNull(); + assertThat(result.id()).isNotNull(); + assertThat(result.userId()).isEqualTo(userId); + assertThat(result.name()).isEqualTo(userCommonAttribute1.getName()); + assertThat(result.contactNumber()).isEqualTo(userCommonAttribute1.getContactNumber()); + assertThat(result.imgUrl()).isEqualTo(userCommonAttribute1.getImgUrl()); + assertThat(result.introduce()).isEqualTo(userCommonAttribute1.getIntroduce()); + } }