Skip to content

Commit

Permalink
Merge pull request #64 from Donut-DONationUTile/feature/donation/seri…
Browse files Browse the repository at this point in the history
…alize

feat: 월렛 화면에서 바로 기부하기 구현
  • Loading branch information
Kang1221 authored Apr 27, 2024
2 parents 1c173b2 + 8cd835d commit def15cb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/zero/eight/donut/service/DonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +26,7 @@
@Service
public class DonationService {

private final AuthUtils authUtils;
private final SerialDonationService donationService;
private final GiftRepository giftRepository;

Expand All @@ -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);
}
}
34 changes: 34 additions & 0 deletions src/main/java/zero/eight/donut/service/SerialDonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit def15cb

Please sign in to comment.