diff --git a/src/main/java/zero/eight/donut/controller/DonationController.java b/src/main/java/zero/eight/donut/controller/DonationController.java index bfcb0c3..fccbe8c 100644 --- a/src/main/java/zero/eight/donut/controller/DonationController.java +++ b/src/main/java/zero/eight/donut/controller/DonationController.java @@ -1,10 +1,7 @@ package zero.eight.donut.controller; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import zero.eight.donut.common.response.ApiResponse; import zero.eight.donut.dto.donation.DonateGiftRequestDto; import zero.eight.donut.dto.donation.GiftboxRequestDto; @@ -28,4 +25,9 @@ public ApiResponse assignGiftbox(@RequestBody GiftboxRequestDto giftboxReques public ApiResponse donateGift(DonateGiftRequestDto donateGiftRequestDto) throws IOException { return donationService.donateGift(donateGiftRequestDto); } + + @PatchMapping("/wallet/{giftId}") + public ApiResponse walletDonation(@PathVariable Long giftId) { + return donationService.walletDonation(giftId); + } } diff --git a/src/main/java/zero/eight/donut/service/DonationService.java b/src/main/java/zero/eight/donut/service/DonationService.java index b045ead..ba2fd36 100644 --- a/src/main/java/zero/eight/donut/service/DonationService.java +++ b/src/main/java/zero/eight/donut/service/DonationService.java @@ -7,10 +7,14 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import zero.eight.donut.common.response.ApiResponse; +import zero.eight.donut.config.jwt.AuthUtils; import zero.eight.donut.domain.Gift; +import zero.eight.donut.domain.Giver; import zero.eight.donut.domain.enums.Status; import zero.eight.donut.dto.donation.DonateGiftRequestDto; import zero.eight.donut.dto.donation.GiftboxRequestDto; +import zero.eight.donut.exception.Error; +import zero.eight.donut.exception.Success; import zero.eight.donut.repository.GiftRepository; import java.io.IOException; @@ -22,6 +26,7 @@ @Service public class DonationService { + private final AuthUtils authUtils; private final SerialDonationService donationService; private final GiftRepository giftRepository; @@ -43,4 +48,8 @@ public synchronized ApiResponse assignGiftbox(GiftboxRequestDto giftboxReques public ApiResponse donateGift(DonateGiftRequestDto requestDto) throws IOException { return donationService.donateGift(requestDto); } + + public ApiResponse walletDonation(Long giftId) { + return donationService.donateWalletGift(giftId); + } } \ No newline at end of file diff --git a/src/main/java/zero/eight/donut/service/SerialDonationService.java b/src/main/java/zero/eight/donut/service/SerialDonationService.java index 80d8658..315489e 100644 --- a/src/main/java/zero/eight/donut/service/SerialDonationService.java +++ b/src/main/java/zero/eight/donut/service/SerialDonationService.java @@ -279,4 +279,38 @@ private void sendImageToAI(Long giftId, MultipartFile giftImage){ }) .subscribe(); } + + @Transactional + public ApiResponse donateWalletGift(Long giftId) { + // 기프티콘 찾기 + Gift gift = giftRepository.findById(giftId).orElse(null); + if (gift == null) { + return ApiResponse.failure(Error.GIFT_NOT_FOUND_EXCEPTION); + } + + // 기프티콘 소유자 == 사용자 확인 + Giver giver = authUtils.getGiver(); + if (!gift.getGiver().equals(giver)) { + return ApiResponse.failure(Error.NOT_AUTHENTICATED_EXCEPTION); + } + + // 기프티콘 status 변경하기(-> UNUSED) + gift.updateStatus("UNUSED"); + giftRepository.save(gift); + + //기부자별 정보 Donation 업데이트 + Donation donation = donationRepository.findByGiver(giver); + donation.updateSumCount( + donation.getSum() + gift.getPrice().longValue(), + donation.getCount()+1L); + + //기부 통계 업데이트 + LocalDate now = LocalDate.now(); + DonationInfo donationInfo = donationInfoRepository.findDonationInfoByMonthAndYear(now.getMonthValue(), now.getYear()); + donationInfo.updateSumCount( + donationInfo.getSum() + gift.getPrice().longValue(), + donationInfo.getCount()+1L); + + return ApiResponse.success(Success.DONATE_GIFT_SUCCESS); + } } \ No newline at end of file