diff --git a/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java b/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java index c21c1f5..6c12477 100644 --- a/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java +++ b/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java @@ -1,6 +1,7 @@ package com.nice.petudio.api.controller.gift; import com.nice.petudio.api.controller.gift.dto.GiftGenerateResponse; +import com.nice.petudio.api.controller.gift.dto.GiftsRetrieveResponse; import com.nice.petudio.api.controller.gift.service.GiftCodeGenerator; import com.nice.petudio.api.controller.gift.service.GiftCommandService; import com.nice.petudio.api.controller.gift.service.GiftQueryService; @@ -11,6 +12,7 @@ import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -37,4 +39,11 @@ public ApiResponse useGift(@MemberId final Long memberId, @RequestParam final giftCommandService.useGift(memberId, giftCode); return ApiResponse.success(); } + + @Operation(summary = "[인증] 기프트 조회") + @Auth + @GetMapping("/gifts") + public ApiResponse findGifts(@MemberId final Long memberId) { + return ApiResponse.success(giftQueryService.findGiftsByMemberId(memberId)); + } } diff --git a/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftRetrieveResponse.java b/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftRetrieveResponse.java new file mode 100644 index 0000000..d516168 --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftRetrieveResponse.java @@ -0,0 +1,10 @@ +package com.nice.petudio.api.controller.gift.dto; + +import java.time.LocalDateTime; + +public record GiftRetrieveResponse(String giftCode, LocalDateTime expiredAt) { + + public static GiftRetrieveResponse of(String giftCode, LocalDateTime expiredAt) { + return new GiftRetrieveResponse(giftCode, expiredAt); + } +} diff --git a/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftsRetrieveResponse.java b/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftsRetrieveResponse.java new file mode 100644 index 0000000..0bf6865 --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftsRetrieveResponse.java @@ -0,0 +1,6 @@ +package com.nice.petudio.api.controller.gift.dto; + +import java.util.List; + +public record GiftsRetrieveResponse(List giftsInfo){ +} diff --git a/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java index 377e28b..e4d007b 100644 --- a/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java +++ b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java @@ -1,5 +1,11 @@ package com.nice.petudio.api.controller.gift.service; +import com.nice.petudio.api.controller.gift.dto.GiftRetrieveResponse; +import com.nice.petudio.api.controller.gift.dto.GiftsRetrieveResponse; +import com.nice.petudio.domain.gift.Gift; +import com.nice.petudio.domain.gift.repository.GiftRepository; +import java.util.ArrayList; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -8,4 +14,16 @@ @Transactional(readOnly = true) @RequiredArgsConstructor public class GiftQueryService { + private final GiftRepository giftRepository; + + public GiftsRetrieveResponse findGiftsByMemberId(final Long memberId) { + List gifts = giftRepository.findUsableGiftsByBuyerId(memberId); + + List giftsInfo = new ArrayList<>(); + for (Gift gift : gifts) { + giftsInfo.add(GiftRetrieveResponse.of(gift.getCode(), gift.getExpiredAt())); + } + + return new GiftsRetrieveResponse(giftsInfo); + } } diff --git a/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryCustom.java b/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryCustom.java index 2f016bf..96164cf 100644 --- a/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryCustom.java +++ b/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryCustom.java @@ -1,8 +1,10 @@ package com.nice.petudio.domain.gift.repository; import com.nice.petudio.domain.gift.Gift; +import java.util.List; import java.util.Optional; public interface GiftRepositoryCustom { Optional findByGiftCode(String giftCode); + List findUsableGiftsByBuyerId(Long memberId); } diff --git a/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryImpl.java b/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryImpl.java index 5cccd02..bb22d04 100644 --- a/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryImpl.java +++ b/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryImpl.java @@ -4,6 +4,8 @@ import com.nice.petudio.domain.gift.Gift; import com.querydsl.jpa.impl.JPAQueryFactory; +import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; @@ -18,4 +20,13 @@ public Optional findByGiftCode(String giftCode) { .where(gift.code.eq(giftCode)) .fetchOne()); } + + @Override + public List findUsableGiftsByBuyerId(Long memberId) { + return queryFactory.selectFrom(gift) + .where(gift.buyerId.eq(memberId)) + .where(gift.isUsed.eq(false)) + .where(gift.expiredAt.gt(LocalDateTime.now())) + .fetch(); + } }