Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 이동봉사자 마이페이지 메인 정보 조회 API 수정 #184

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pawwithu.connectdog.domain.application.dto.response.*;
import com.pawwithu.connectdog.domain.application.entity.Application;
import com.pawwithu.connectdog.domain.application.entity.ApplicationStatus;
import com.querydsl.core.Tuple;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand All @@ -18,8 +19,6 @@ public interface CustomApplicationRepository {
List<ApplicationIntermediaryProgressingResponse> getIntermediaryProgressingApplications(Long intermediaryId, Pageable pageable);
List<ApplicationVolunteerCompletedResponse> getVolunteerCompletedApplications(Long volunteerId, Pageable pageable);
List<ApplicationIntermediaryCompletedResponse> getIntermediaryCompletedApplications(Long intermediaryId, Pageable pageable);

// 진행한 이동봉사 건수
Long getCountOfCompletedApplications(Long id);
List<Tuple> getCountOfApplicationsByStatus(Long id);
boolean existsByPostIdAndPostStatus(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.pawwithu.connectdog.domain.application.entity.Application;
import com.pawwithu.connectdog.domain.application.entity.ApplicationStatus;
import com.pawwithu.connectdog.domain.application.repository.CustomApplicationRepository;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -170,15 +171,14 @@ public List<ApplicationIntermediaryCompletedResponse> getIntermediaryCompletedAp
.fetch();
}

// 진행한 이동봉사 건수
@Override
public Long getCountOfCompletedApplications(Long id) {
public List<Tuple> getCountOfApplicationsByStatus(Long id) {
return queryFactory
.select(application.count())
.select(application.status, application.count())
.from(application)
.where(application.volunteer.id.eq(id)
.and(application.status.eq(ApplicationStatus.COMPLETED)))
.fetchOne();
.where(application.volunteer.id.eq(id))
.groupBy(application.status)
.fetch();
}

@Override
Expand All @@ -190,4 +190,5 @@ public boolean existsByPostIdAndPostStatus(Long postId) {
.and(application.status.ne(ApplicationStatus.REJECTED)))
.fetchOne() != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ResponseEntity<Void> additionalAuth(@AuthenticationPrincipal UserDetails
return ResponseEntity.noContent().build();
}

@Operation(summary = "마이페이지 기본 정보 조회 API", description = "마이페이지 기본 정보를 조회합니다.",
@Operation(summary = "마이페이지 메인 정보 조회 API", description = "마이페이지 메인 화면의 닉네임, 프로필 이미지, 봉사 현황 요약 조회를 조회합니다.",
responses = {@ApiResponse(responseCode = "200", description = "마이페이지 기본 정보 조회 성공")
, @ApiResponse(responseCode = "400"
, description = "M1, 해당 이동봉사자를 찾을 수 없습니다."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.pawwithu.connectdog.domain.volunteer.dto.response;

public record VolunteerGetMyInfoResponse(Integer profileImageNum, String nickname, Long completedCount, Long reviewCount, Long dogStatusCount) {
public static VolunteerGetMyInfoResponse of(Integer profileImageNum, String nickname, Long completedCount, Long reviewCount, Long dogStatusCount) {
return new VolunteerGetMyInfoResponse(profileImageNum, nickname, completedCount, reviewCount, dogStatusCount);
public record VolunteerGetMyInfoResponse(Integer profileImageNum, String nickname,
Long waitingCount, Long progressingCount, Long completedCount, Long reviewCount) {

public static VolunteerGetMyInfoResponse of(Integer profileImageNum, String nickname,
Long waitingCount, Long progressingCount, Long completedCount, Long reviewCount) {
return new VolunteerGetMyInfoResponse(profileImageNum, nickname, waitingCount, progressingCount, completedCount, reviewCount);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pawwithu.connectdog.domain.volunteer.service;

import com.pawwithu.connectdog.domain.application.entity.ApplicationStatus;
import com.pawwithu.connectdog.domain.application.repository.CustomApplicationRepository;
import com.pawwithu.connectdog.domain.badge.repository.CustomVolunteerBadgeRepository;
import com.pawwithu.connectdog.domain.bookmark.repository.CustomBookmarkRepository;
Expand All @@ -13,6 +14,7 @@
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository;
import com.pawwithu.connectdog.error.ErrorCode;
import com.pawwithu.connectdog.error.exception.custom.BadRequestException;
import com.querydsl.core.Tuple;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -21,6 +23,7 @@

import java.util.List;

import static com.pawwithu.connectdog.domain.application.entity.QApplication.application;
import static com.pawwithu.connectdog.error.ErrorCode.ALREADY_EXIST_NICKNAME;
import static com.pawwithu.connectdog.error.ErrorCode.VOLUNTEER_NOT_FOUND;

Expand Down Expand Up @@ -54,16 +57,25 @@ public void additionalAuth(String email, AdditionalAuthRequest request) {
public VolunteerGetMyInfoResponse getMyInfo(String email) {
Volunteer volunteer = volunteerRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));

// 진행한 이동봉사 건수
Long completedCount = customApplicationRepository.getCountOfCompletedApplications(volunteer.getId());

// 봉사 후기 건수
Long waitingCount = 0L;
Long progressingCount = 0L;
Long completedCount = 0L;
Long reviewCount = customReviewRepository.getVolunteerCountOfReviews(volunteer.getId());

// 입양 근황 건수
Long dogStatusCount = customDogStatusRepository.getVolunteerCountOfDogStatuses(volunteer.getId());
List<Tuple> applicationCounts = customApplicationRepository.getCountOfApplicationsByStatus(volunteer.getId());
for (Tuple tuple : applicationCounts) {
ApplicationStatus status = tuple.get(application.status);
Long count = tuple.get(application.count());

switch (status) {
case WAITING -> waitingCount = count;
case PROGRESSING -> progressingCount = count;
case COMPLETED -> completedCount = count;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 중개자 홈 화면 정보 조회할 때

Map<PostStatus, Long> countOfPostStatus = customPostRepository.getCountOfPostStatus(intermediary.getId(), null);
        IntermediaryGetHomeResponse response = IntermediaryGetHomeResponse.of(
                intermediary,
                countOfPostStatus.getOrDefault(PostStatus.RECRUITING, 0L),
                countOfPostStatus.getOrDefault(PostStatus.WAITING, 0L),
                countOfPostStatus.getOrDefault(PostStatus.PROGRESSING, 0L),
                countOfPostStatus.getOrDefault(PostStatus.COMPLETED, 0L));

Map으로 가져왔었는데 Tuple로도 이렇게 한 번에 매핑해서 넘겨줄 수 있을까요?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 중개자 홈 화면에도 건수 조회하는 기능이 있었죠!

조금 찾아보니, Map은 키-값 쌍을 저장하기 때문에 키를 사용하여 값을 찾을 때 편리한 장점이 있고 Tuple은 간단하고 빠르게 여러 값을 그룹화할 때 사용하는 장점이 주요 장점인 것 같아요.

건수 3~4가지를 가져오기 때문에 큰 차이는 없어 보입니다! 덕분에 Tuple에서 바로 Map 만들어 가져오는 방법을 리마인드했습니다 👍


VolunteerGetMyInfoResponse response = VolunteerGetMyInfoResponse.of(volunteer.getProfileImageNum(), volunteer.getNickname(), completedCount, reviewCount, dogStatusCount);
VolunteerGetMyInfoResponse response = VolunteerGetMyInfoResponse.of(volunteer.getProfileImageNum(), volunteer.getNickname(),
waitingCount, progressingCount, completedCount, reviewCount);
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void setUp() {
@Test
void 이동봉사자_마이페이지_기본_정보_조회() throws Exception {
// given
VolunteerGetMyInfoResponse response = VolunteerGetMyInfoResponse.of(1, "하노정", 1L, 3L, 5L);
VolunteerGetMyInfoResponse response = VolunteerGetMyInfoResponse.of(1, "하노정", 1L, 3L, 5L, 4L);

// when
given(volunteerService.getMyInfo(any())).willReturn(response);
Expand Down
Loading