Skip to content

Commit

Permalink
Merge pull request #65 from Donut-DONationUTile/feature/mypage
Browse files Browse the repository at this point in the history
Feature/mypage
  • Loading branch information
Kang1221 authored Apr 28, 2024
2 parents def15cb + f9b56a4 commit cec44b5
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/main/java/zero/eight/donut/controller/MypageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package zero.eight.donut.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.service.MypageService;

@RequiredArgsConstructor
@RequestMapping("/api/mypage")
@RestController
public class MypageController {
private final MypageService mypageService;

@GetMapping("/giver")
public ApiResponse<?> giverInfo() {
return mypageService.getGiverMypage();
}

@GetMapping("/receiver")
public ApiResponse<?> receiverInfo() {
return mypageService.getReceiverMypage();
}
}
4 changes: 4 additions & 0 deletions src/main/java/zero/eight/donut/domain/Giver.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ public class Giver extends BaseTimeEntity {
//신고
@OneToMany(mappedBy = "giver", fetch = FetchType.LAZY)
private List<Report> reportList = new ArrayList<>();

//메세지
@OneToMany(mappedBy = "giver", fetch = FetchType.LAZY)
private List<Message> messagesList = new ArrayList<>();
}

4 changes: 4 additions & 0 deletions src/main/java/zero/eight/donut/domain/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public class Message {
@JoinColumn(name = "gift_id")
private Gift gift;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "giver_id")
private Giver giver;

}
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;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zero.eight.donut.dto;
package zero.eight.donut.dto.donation;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package zero.eight.donut.dto.mypage;

import lombok.Builder;

@Builder
public class GiverInfoResponseDto {
private int years; // 기부 기간(연)
private double donation; // 총 기부 금액
private StatsDto stats; // 통계
}
12 changes: 12 additions & 0 deletions src/main/java/zero/eight/donut/dto/mypage/StatsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package zero.eight.donut.dto.mypage;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class StatsDto {
private int unreceived; // 기부한 것 중 할당 안된 것
private int received; // 기부한 것 중 할당 된 것
private int msg; // 받은 메세지 개수
}
2 changes: 2 additions & 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,8 @@ 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"),
HOME_RECEIVER_SUCCESS(HttpStatus.OK, "Get request for receiver'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);
}
6 changes: 6 additions & 0 deletions src/main/java/zero/eight/donut/repository/GiftRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ public interface GiftRepository extends JpaRepository<Gift, Long> {

@Query(value = "SELECT * FROM gift g WHERE g.auto_donation = true AND g.status = 'stored' AND g.giver_id = :giverId AND g.due_date >= :today", nativeQuery = true)
List<Gift> findAllByGiverAndStatusAndDueDateAfterOrToday(@Param("giverId") Long giverId, @Param("today") LocalDateTime today);

@Query(value = "SELECT COUNT(g) FROM gift g WHERE g.giver_id = :giverId AND g.is_assigned = false", nativeQuery = true)
int findNotAssignedByGiverIdAndIsAssigned(@Param("giverId") Long giverId);

@Query(value = "SELECT COUNT(g) FROM gift g WHERE g.giver_id = :giverId AND g.is_assigned = true", nativeQuery = true)
int findIsAssignedByGiverIdAndIsAssigned(@Param("giverId") Long giverId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package zero.eight.donut.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import zero.eight.donut.domain.Message;

public interface MessageRepository extends JpaRepository<Message,Long> {
Message findByGiftId(Long giftId);

@Query(value = "SELECT COUNT(m) FROM message m WHERE m.giver_id = :giverId", nativeQuery = true)
int countByGiverId(@Param("giverId") Long giverId);
}
86 changes: 86 additions & 0 deletions src/main/java/zero/eight/donut/service/MypageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package zero.eight.donut.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
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.*;

import java.time.Duration;
import java.time.LocalDateTime;

@RequiredArgsConstructor
@Service
public class MypageService {

private final AuthUtils authUtils;
private final GiftRepository giftRepository;
private final DonationRepository donationRepository;
private final DonationInfoRepository donationInfoRepository;
private final MessageRepository messageRepository;
private final BenefitRepository benefitRepository;

public ApiResponse<?> getGiverMypage() {
// 기부 기간(연) 계산
Giver giver = authUtils.getGiver();
LocalDateTime now = LocalDateTime.now();
Duration duration = Duration.between(giver.getCreatedAt(), now); // 현재 시간과 기부자 계정 생성 시간 비교
long days = duration.toDays(); // Duration 객체의 총 일(day) 수를 가져옴
int years = (int) (days / 365); // 평년을 기준으로 연수 계산

// 총 기부 금액 계산
double donation = 0d;
Long donationInfo = donationRepository.getSumByGiverId(giver.getId());
if (donationInfo != null) {
donation = donationInfo.doubleValue();
}

// 기부-할당x 기프티콘 수 계산
int unreceived = giftRepository.findNotAssignedByGiverIdAndIsAssigned(giver.getId());

// 기부-할당o 기프티콘 수 계산
int received = giftRepository.findIsAssignedByGiverIdAndIsAssigned(giver.getId());

// 받은 메세지 수 계산
int msg = messageRepository.countByGiverId(giver.getId());

// DTO 생성 및 반환
StatsDto statsDto = StatsDto.builder()
.unreceived(unreceived)
.received(received)
.msg(msg)
.build();

GiverInfoResponseDto responseDto = GiverInfoResponseDto.builder()
.years(years)
.donation(donation)
.stats(statsDto)
.build();

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.config.jwt.AuthUtils;
import zero.eight.donut.domain.*;
import zero.eight.donut.dto.GiftAssignDto;
import zero.eight.donut.dto.donation.GiftAssignDto;
import zero.eight.donut.dto.auth.Role;
import zero.eight.donut.dto.donation.DonateGiftRequestDto;
import zero.eight.donut.dto.donation.GiftValueDto;
Expand Down

0 comments on commit cec44b5

Please sign in to comment.