Skip to content

Commit

Permalink
feat: 마이페이지(수혜자) 조회 구현
Browse files Browse the repository at this point in the history
- 수혜자의 마이페이지 조회를 구현하였습니다.
  • Loading branch information
Ganghee-Lee-0522 committed Apr 27, 2024
1 parent c4fe37c commit f9b56a4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public class MypageController {
public ApiResponse<?> giverInfo() {
return mypageService.getGiverMypage();
}

@GetMapping("/receiver")
public ApiResponse<?> receiverInfo() {
return mypageService.getReceiverMypage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zero.eight.donut.dto;

import lombok.Builder;

@Builder
public class ReceiverInfoResponseDto {
private double total;
}
1 change: 1 addition & 0 deletions src/main/java/zero/eight/donut/exception/Success.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum Success {
SUCCESS(HttpStatus.OK, "Request successfully processed"),

// 200 OK SUCCESS
MYPAGE_RECEIVER_SUCCESS(HttpStatus.OK, "Get request for receiver's info completed successfully"),
MYPAGE_GIVER_SUCCESS(HttpStatus.OK, "Get request for giver's info completed successfully"),
GET_WALLET_SUCCESS(HttpStatus.OK, "Success in getting wallet info"),
HOME_GIVER_SUCCESS(HttpStatus.OK, "Get request for giver's home info completed successfully"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import zero.eight.donut.domain.Benefit;
import zero.eight.donut.domain.Receiver;
Expand All @@ -13,4 +14,7 @@ public interface BenefitRepository extends JpaRepository<Benefit, Long> {

@Query("SELECT b FROM Benefit b WHERE b.receiver.id =?1 AND b.year = ?2 AND b.month= ?3")
Benefit findByReceiverIdAndThisMonth(Long receiverId, Integer year, Integer month);

@Query(value = "SELECT SUM(b.sum) FROM benefit b WHERE b.receiver_id = :receiverId", nativeQuery = true)
Integer sumBenefitByReceiverId(@Param("receiverId") Long receiverId);
}
26 changes: 22 additions & 4 deletions src/main/java/zero/eight/donut/service/MypageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.config.jwt.AuthUtils;
import zero.eight.donut.domain.Giver;
import zero.eight.donut.domain.Receiver;
import zero.eight.donut.dto.ReceiverInfoResponseDto;
import zero.eight.donut.dto.home.giver.GiverHomeResponseDto;
import zero.eight.donut.dto.mypage.GiverInfoResponseDto;
import zero.eight.donut.dto.mypage.StatsDto;
import zero.eight.donut.exception.Success;
import zero.eight.donut.repository.DonationInfoRepository;
import zero.eight.donut.repository.DonationRepository;
import zero.eight.donut.repository.GiftRepository;
import zero.eight.donut.repository.MessageRepository;
import zero.eight.donut.repository.*;

import java.time.Duration;
import java.time.LocalDateTime;
Expand All @@ -26,6 +25,7 @@ public class MypageService {
private final DonationRepository donationRepository;
private final DonationInfoRepository donationInfoRepository;
private final MessageRepository messageRepository;
private final BenefitRepository benefitRepository;

public ApiResponse<?> getGiverMypage() {
// 기부 기간(연) 계산
Expand Down Expand Up @@ -57,6 +57,7 @@ public ApiResponse<?> getGiverMypage() {
.received(received)
.msg(msg)
.build();

GiverInfoResponseDto responseDto = GiverInfoResponseDto.builder()
.years(years)
.donation(donation)
Expand All @@ -65,4 +66,21 @@ public ApiResponse<?> getGiverMypage() {

return ApiResponse.success(Success.MYPAGE_GIVER_SUCCESS, responseDto);
}

public ApiResponse<?> getReceiverMypage() {
// 수혜 금액 계산
Receiver receiver = authUtils.getReceiver();
double total = 0d;
Integer summed = benefitRepository.sumBenefitByReceiverId(receiver.getId());
if (summed != null) {
total = summed.doubleValue();
}

// DTO 생성 및 반환
ReceiverInfoResponseDto responseDto = ReceiverInfoResponseDto.builder()
.total(total)
.build();

return ApiResponse.success(Success.MYPAGE_RECEIVER_SUCCESS, responseDto);
}
}

0 comments on commit f9b56a4

Please sign in to comment.