Skip to content

Commit

Permalink
✨Feature/#24: 4.2 후원 횟수 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyeomjang committed Nov 18, 2024
1 parent befd8a0 commit 47f3478
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 3 deletions.
10 changes: 8 additions & 2 deletions http/onjung/OnjungControllerHttpRequest.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
### 4.1 전체 온기 통계 조회
// @no-log
GET {{host_url}}/api/v1/onjung/summaries
Authorization: Bearer {{access_token}}
GET {{host_url}}/api/v1/onjungs/summaries
Authorization: Bearer {{access_token}}

### 4.2 후원 횟수 조회
// @no-log
GET {{host_url}}/api/v1/onjungs/count
Authorization: Bearer {{access_token}}

Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package com.daon.onjung.onjung.application.controller.query;

import com.daon.onjung.core.annotation.security.AccountID;
import com.daon.onjung.core.dto.ResponseDto;
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.ReadOnjungCountUseCase;
import com.daon.onjung.onjung.application.usecase.ReadOnjungSummaryUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequiredArgsConstructor
public class OnjungQueryV1Controller {

private final ReadOnjungSummaryUseCase readOnjungSummaryUseCase;
private final ReadOnjungCountUseCase readOnjungCountUseCase;

/**
* 4.1 전체 온기 통계 조회하기
*/
@GetMapping("/api/v1/onjung/summaries")
@GetMapping("/api/v1/onjungs/summaries")
public ResponseDto<ReadOnjungSummaryResponseDto> readOnjungSummary() {
return ResponseDto.ok(readOnjungSummaryUseCase.execute());
}

/**
* 4.2 후원 횟수 조회하기
*/
@GetMapping("/api/v1/onjungs/count")
public ResponseDto<ReadOnjungCountResponseDto> readOnjungCount(
@AccountID UUID accountId
) {
return ResponseDto.ok(readOnjungCountUseCase.execute(accountId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.daon.onjung.onjung.application.dto.response;

import com.daon.onjung.core.dto.SelfValidating;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;

public class ReadOnjungCountResponseDto extends SelfValidating<ReadOnjungCountResponseDto> {

@JsonProperty("total_onjung_count")
private final Integer totalOnjungCount;

@Builder
public ReadOnjungCountResponseDto(Integer totalOnjungCount) {
this.totalOnjungCount = totalOnjungCount;
this.validateSelf();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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.ReadOnjungCountResponseDto;
import com.daon.onjung.onjung.application.usecase.ReadOnjungCountUseCase;
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 ReadOnjungCountService implements ReadOnjungCountUseCase {

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 ReadOnjungCountResponseDto execute(
UUID accountId
) {

// User 조회
User user = userRepository.findById(accountId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE));

// User의 모든 동참 내역 조회
List<Donation> donations = donationRepository.findAllByUser(user);

// User의 모든 영수증 인증 내역 조회
List<Receipt> receipts = receiptRepository.findAllByUser(user);

// User의 모든 공유 내역 조회
List<Share> shares = shareRepository.findAllByUser(user);

// Onjung 생성
Onjung onjung = onjungService.createOnjung(donations, receipts, shares);

// User의 총 온정 횟수 조회
Integer totalOnjungCount = onjungService.calculateTotalOnjungCount(onjung);

return ReadOnjungCountResponseDto.builder()
.totalOnjungCount(totalOnjungCount)
.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.ReadOnjungCountResponseDto;

import java.util.UUID;

@UseCase
public interface ReadOnjungCountUseCase {
ReadOnjungCountResponseDto execute(UUID accountId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.daon.onjung.onjung.repository.mysql;

import com.daon.onjung.account.domain.User;
import com.daon.onjung.onjung.domain.Donation;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface DonationRepository extends JpaRepository <Donation, Long> {

List<Donation> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.daon.onjung.onjung.repository.mysql;

import com.daon.onjung.account.domain.User;
import com.daon.onjung.onjung.domain.Receipt;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ReceiptRepository extends JpaRepository <Receipt, Long> {

List<Receipt> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.daon.onjung.onjung.repository.mysql;

import com.daon.onjung.account.domain.User;
import com.daon.onjung.onjung.domain.Share;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ShareRepository extends JpaRepository <Share, Long> {

List<Share> findAllByUser(User user);
}

0 comments on commit 47f3478

Please sign in to comment.