Skip to content

Commit

Permalink
feat: 기프트 조회 기능 구현 #44
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Feb 6, 2024
1 parent ba6464b commit 068049c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<GiftsRetrieveResponse> findGifts(@MemberId final Long memberId) {
return ApiResponse.success(giftQueryService.findGiftsByMemberId(memberId));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nice.petudio.api.controller.gift.dto;

import java.util.List;

public record GiftsRetrieveResponse(List<GiftRetrieveResponse> giftsInfo){
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,4 +14,16 @@
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class GiftQueryService {
private final GiftRepository giftRepository;

public GiftsRetrieveResponse findGiftsByMemberId(final Long memberId) {
List<Gift> gifts = giftRepository.findUsableGiftsByBuyerId(memberId);

List<GiftRetrieveResponse> giftsInfo = new ArrayList<>();
for (Gift gift : gifts) {
giftsInfo.add(GiftRetrieveResponse.of(gift.getCode(), gift.getExpiredAt()));
}

return new GiftsRetrieveResponse(giftsInfo);
}
}
Original file line number Diff line number Diff line change
@@ -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<Gift> findByGiftCode(String giftCode);
List<Gift> findUsableGiftsByBuyerId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,4 +20,13 @@ public Optional<Gift> findByGiftCode(String giftCode) {
.where(gift.code.eq(giftCode))
.fetchOne());
}

@Override
public List<Gift> findUsableGiftsByBuyerId(Long memberId) {
return queryFactory.selectFrom(gift)
.where(gift.buyerId.eq(memberId))
.where(gift.isUsed.eq(false))
.where(gift.expiredAt.gt(LocalDateTime.now()))
.fetch();
}
}

0 comments on commit 068049c

Please sign in to comment.