-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from Donut-DONationUTile/feature/mypage
Feature/mypage
- Loading branch information
Showing
13 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/zero/eight/donut/controller/MypageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/zero/eight/donut/dto/ReceiverInfoResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
2 changes: 1 addition & 1 deletion
2
...a/zero/eight/donut/dto/GiftAssignDto.java → ...ght/donut/dto/donation/GiftAssignDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/zero/eight/donut/dto/mypage/GiverInfoResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; // 통계 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; // 받은 메세지 개수 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/zero/eight/donut/repository/MessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters