diff --git a/http/onjung/OnjungControllerHttpRequest.http b/http/onjung/OnjungControllerHttpRequest.http index 00aa2a0..01b62fc 100644 --- a/http/onjung/OnjungControllerHttpRequest.http +++ b/http/onjung/OnjungControllerHttpRequest.http @@ -8,3 +8,7 @@ Authorization: Bearer {{access_token}} GET {{host_url}}/api/v1/onjungs/count Authorization: Bearer {{access_token}} +### 4.3 온기 간단 정보 조회 +// @no-log +GET {{host_url}}/api/v1/onjungs/briefs +Authorization: Bearer {{access_token}} \ No newline at end of file diff --git a/src/main/java/com/daon/onjung/onjung/application/controller/query/OnjungQueryV1Controller.java b/src/main/java/com/daon/onjung/onjung/application/controller/query/OnjungQueryV1Controller.java index d378eda..326093e 100644 --- a/src/main/java/com/daon/onjung/onjung/application/controller/query/OnjungQueryV1Controller.java +++ b/src/main/java/com/daon/onjung/onjung/application/controller/query/OnjungQueryV1Controller.java @@ -2,8 +2,10 @@ import com.daon.onjung.core.annotation.security.AccountID; import com.daon.onjung.core.dto.ResponseDto; +import com.daon.onjung.onjung.application.dto.response.ReadOnjungBriefResponseDto; import com.daon.onjung.onjung.application.dto.response.ReadOnjungCountResponseDto; import com.daon.onjung.onjung.application.dto.response.ReadOnjungSummaryResponseDto; +import com.daon.onjung.onjung.application.usecase.ReadOnjungBriefUseCase; import com.daon.onjung.onjung.application.usecase.ReadOnjungCountUseCase; import com.daon.onjung.onjung.application.usecase.ReadOnjungSummaryUseCase; import lombok.RequiredArgsConstructor; @@ -18,6 +20,7 @@ public class OnjungQueryV1Controller { private final ReadOnjungSummaryUseCase readOnjungSummaryUseCase; private final ReadOnjungCountUseCase readOnjungCountUseCase; + private final ReadOnjungBriefUseCase readOnjungBriefUseCase; /** * 4.1 전체 온기 통계 조회하기 @@ -36,4 +39,14 @@ public ResponseDto readOnjungCount( ) { return ResponseDto.ok(readOnjungCountUseCase.execute(accountId)); } + + /** + * 4.3 온기 간단 정보 조회 + */ + @GetMapping("/api/v1/onjungs/briefs") + public ResponseDto readOnjungBrief( + @AccountID UUID accountId + ) { + return ResponseDto.ok(readOnjungBriefUseCase.execute(accountId)); + } } diff --git a/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungBriefResponseDto.java b/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungBriefResponseDto.java new file mode 100644 index 0000000..2d42df3 --- /dev/null +++ b/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungBriefResponseDto.java @@ -0,0 +1,24 @@ +package com.daon.onjung.onjung.application.dto.response; + +import com.daon.onjung.core.dto.SelfValidating; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class ReadOnjungBriefResponseDto extends SelfValidating { + + @JsonProperty("total_onjung_count") + private final Integer totalOnjungCount; + + @JsonProperty("total_onjung_amount") + private final Integer totalOnjungAmount; + + @Builder + public ReadOnjungBriefResponseDto(Integer totalOnjungCount, Integer totalOnjungAmount) { + this.totalOnjungCount = totalOnjungCount; + this.totalOnjungAmount = totalOnjungAmount; + + this.validateSelf(); + } +} diff --git a/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungCountResponseDto.java b/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungCountResponseDto.java index 4509a83..315f2f1 100644 --- a/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungCountResponseDto.java +++ b/src/main/java/com/daon/onjung/onjung/application/dto/response/ReadOnjungCountResponseDto.java @@ -3,7 +3,9 @@ import com.daon.onjung.core.dto.SelfValidating; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Builder; +import lombok.Getter; +@Getter public class ReadOnjungCountResponseDto extends SelfValidating { @JsonProperty("total_onjung_count") diff --git a/src/main/java/com/daon/onjung/onjung/application/service/ReadOnjungBriefService.java b/src/main/java/com/daon/onjung/onjung/application/service/ReadOnjungBriefService.java new file mode 100644 index 0000000..49775c6 --- /dev/null +++ b/src/main/java/com/daon/onjung/onjung/application/service/ReadOnjungBriefService.java @@ -0,0 +1,67 @@ +package com.daon.onjung.onjung.application.service; + +import com.daon.onjung.account.domain.User; +import com.daon.onjung.account.repository.mysql.UserRepository; +import com.daon.onjung.core.exception.error.ErrorCode; +import com.daon.onjung.core.exception.type.CommonException; +import com.daon.onjung.onjung.application.dto.response.ReadOnjungBriefResponseDto; +import com.daon.onjung.onjung.application.usecase.ReadOnjungBriefUseCase; +import com.daon.onjung.onjung.domain.Donation; +import com.daon.onjung.onjung.domain.Onjung; +import com.daon.onjung.onjung.domain.Receipt; +import com.daon.onjung.onjung.domain.Share; +import com.daon.onjung.onjung.domain.service.OnjungService; +import com.daon.onjung.onjung.repository.mysql.DonationRepository; +import com.daon.onjung.onjung.repository.mysql.ReceiptRepository; +import com.daon.onjung.onjung.repository.mysql.ShareRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class ReadOnjungBriefService implements ReadOnjungBriefUseCase { + + private final UserRepository userRepository; + private final DonationRepository donationRepository; + private final ReceiptRepository receiptRepository; + private final ShareRepository shareRepository; + + private final OnjungService onjungService; + + @Override + @Transactional(readOnly = true) + public ReadOnjungBriefResponseDto execute(UUID accountId) { + + // 유저 조회 + User user = userRepository.findById(accountId) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); + + // 유저의 기부 내역 전체 조회 + List donations = donationRepository.findAllByUser(user); + + // 유저의 영수증 인증 내역 전체 조회 + List receipts = receiptRepository.findAllByUser(user); + + // 유저의 공유 내역 전체 조회 + List shares = shareRepository.findAllByUser(user); + + // 온정 생성 + Onjung onjung = onjungService.createOnjung(donations, receipts, shares); + + // 온정 총 개수 계산 + Integer totalOnjungCount = onjungService.calculateTotalOnjungCount(onjung); + + // 온정 총 금액 계산 + Integer totalOnjungAmount = onjungService.calculateTotalOnjungAmount(onjung); + + return ReadOnjungBriefResponseDto.builder() + .totalOnjungCount(totalOnjungCount) + .totalOnjungAmount(totalOnjungAmount) + .build(); + } + +} diff --git a/src/main/java/com/daon/onjung/onjung/application/usecase/ReadOnjungBriefUseCase.java b/src/main/java/com/daon/onjung/onjung/application/usecase/ReadOnjungBriefUseCase.java new file mode 100644 index 0000000..8f67d08 --- /dev/null +++ b/src/main/java/com/daon/onjung/onjung/application/usecase/ReadOnjungBriefUseCase.java @@ -0,0 +1,11 @@ +package com.daon.onjung.onjung.application.usecase; + +import com.daon.onjung.core.annotation.bean.UseCase; +import com.daon.onjung.onjung.application.dto.response.ReadOnjungBriefResponseDto; + +import java.util.UUID; + +@UseCase +public interface ReadOnjungBriefUseCase { + ReadOnjungBriefResponseDto execute(UUID accountId); +} diff --git a/src/main/java/com/daon/onjung/onjung/domain/service/OnjungService.java b/src/main/java/com/daon/onjung/onjung/domain/service/OnjungService.java index 3849aaf..b623be1 100644 --- a/src/main/java/com/daon/onjung/onjung/domain/service/OnjungService.java +++ b/src/main/java/com/daon/onjung/onjung/domain/service/OnjungService.java @@ -49,4 +49,14 @@ public Integer calculateTotalUniqueOnjungUserCount(Onjung onjung) { return uniqueUsers.size(); } + + public Integer calculateTotalOnjungAmount(Onjung onjung) { + return onjung.getDonations().stream() + .map(Donation::getDonationAmount) + .reduce(0, Integer::sum) + + onjung.getReceipts().stream() + .map(Receipt::getPaymentAmount) + .reduce(0, Integer::sum) + + onjung.getShares().size() * 100; + } }