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/#24: 4.3 온기 간단 정보 조회 API 구현 #33

Merged
merged 1 commit into from
Nov 18, 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
4 changes: 4 additions & 0 deletions http/onjung/OnjungControllerHttpRequest.http
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +20,7 @@ public class OnjungQueryV1Controller {

private final ReadOnjungSummaryUseCase readOnjungSummaryUseCase;
private final ReadOnjungCountUseCase readOnjungCountUseCase;
private final ReadOnjungBriefUseCase readOnjungBriefUseCase;

/**
* 4.1 전체 온기 통계 조회하기
Expand All @@ -36,4 +39,14 @@ public ResponseDto<ReadOnjungCountResponseDto> readOnjungCount(
) {
return ResponseDto.ok(readOnjungCountUseCase.execute(accountId));
}

/**
* 4.3 온기 간단 정보 조회
*/
@GetMapping("/api/v1/onjungs/briefs")
public ResponseDto<ReadOnjungBriefResponseDto> readOnjungBrief(
@AccountID UUID accountId
) {
return ResponseDto.ok(readOnjungBriefUseCase.execute(accountId));
}
}
Original file line number Diff line number Diff line change
@@ -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<ReadOnjungBriefResponseDto> {

@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReadOnjungCountResponseDto> {

@JsonProperty("total_onjung_count")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Donation> donations = donationRepository.findAllByUser(user);

// 유저의 영수증 인증 내역 전체 조회
List<Receipt> receipts = receiptRepository.findAllByUser(user);

// 유저의 공유 내역 전체 조회
List<Share> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading