diff --git a/src/main/java/com/t3t/bookstoreapi/coupon/adapter/CouponAdapter.java b/src/main/java/com/t3t/bookstoreapi/coupon/adapter/CouponAdapter.java new file mode 100644 index 00000000..51cae7ca --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/coupon/adapter/CouponAdapter.java @@ -0,0 +1,47 @@ +package com.t3t.bookstoreapi.coupon.adapter; + +import com.t3t.bookstoreapi.coupon.client.CouponApiClient; +import com.t3t.bookstoreapi.coupon.exception.CouponApiClientException; +import com.t3t.bookstoreapi.coupon.model.response.CouponResponse; +import com.t3t.bookstoreapi.model.response.BaseResponse; +import feign.FeignException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +@Component +@RequiredArgsConstructor +public class CouponAdapter { + private final CouponApiClient couponApiClient; + + public CouponResponse getGeneralCoupon(){ + try{ + return Optional.ofNullable(couponApiClient.getGeneralCoupon().getBody()) + .map(BaseResponse::getData) + .orElseThrow(CouponApiClientException::new); + }catch (FeignException e){ + throw new CouponApiClientException(); + } + } + + public CouponResponse getBookCoupon(){ + try{ + return Optional.ofNullable(couponApiClient.getBookCoupon().getBody()) + .map(BaseResponse::getData) + .orElseThrow(CouponApiClientException::new); + }catch (FeignException e){ + throw new CouponApiClientException(); + } + } + + public CouponResponse getCategoryCOupon(){ + try{ + return Optional.ofNullable(couponApiClient.getCategoryCoupon().getBody()) + .map(BaseResponse::getData) + .orElseThrow(CouponApiClientException::new); + }catch (FeignException e){ + throw new CouponApiClientException(); + } + } +} diff --git a/src/main/java/com/t3t/bookstoreapi/coupon/client/CouponApiClient.java b/src/main/java/com/t3t/bookstoreapi/coupon/client/CouponApiClient.java index 08cc15c3..0712ce58 100644 --- a/src/main/java/com/t3t/bookstoreapi/coupon/client/CouponApiClient.java +++ b/src/main/java/com/t3t/bookstoreapi/coupon/client/CouponApiClient.java @@ -1,18 +1,19 @@ package com.t3t.bookstoreapi.coupon.client; +import com.t3t.bookstoreapi.coupon.model.response.CouponResponse; +import com.t3t.bookstoreapi.model.response.BaseResponse; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "CouponApiClient", url = "${t3t.feignClient.url}") public interface CouponApiClient { - /*@GetMapping(value = "/t3t/coupon/categories/coupons") - ResponseEntity>> getCategoriesId(); + @GetMapping("/t3t/coupon/coupons/book") + ResponseEntity> getBookCoupon(); - @GetMapping(value = "/t3t/bookstore/categories/{categoryId}/coupons") - ResponseEntity> getCategoryId(@PathVariable("categoryId") Integer categoryId);*/ + @GetMapping("/t3t/coupon/coupons/category") + ResponseEntity> getCategoryCoupon(); - //todo 1 ) Gateway에서 쿠폰 등록 - - // todo 2 ) book coupon, category coupon, general coupon FeignClient 이용해 가져오기 - - // todo 3 ) 가져온 것 db에 넣어놓기 + @GetMapping("/t3t/coupon/coupons/general") + ResponseEntity> getGeneralCoupon(); } \ No newline at end of file diff --git a/src/main/java/com/t3t/bookstoreapi/coupon/exception/CouponApiClientException.java b/src/main/java/com/t3t/bookstoreapi/coupon/exception/CouponApiClientException.java new file mode 100644 index 00000000..8feb4f5e --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/coupon/exception/CouponApiClientException.java @@ -0,0 +1,11 @@ +package com.t3t.bookstoreapi.coupon.exception; + +public class CouponApiClientException extends RuntimeException{ + private static final String DEFAULT_MESSAGES = "쿠폰을 가져오지 못했습니다"; + public CouponApiClientException(String message) { + super(message); + } + public CouponApiClientException(){ + super(DEFAULT_MESSAGES); + } +} diff --git a/src/main/java/com/t3t/bookstoreapi/coupon/model/response/CouponResponse.java b/src/main/java/com/t3t/bookstoreapi/coupon/model/response/CouponResponse.java new file mode 100644 index 00000000..bae86c36 --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/coupon/model/response/CouponResponse.java @@ -0,0 +1,12 @@ +package com.t3t.bookstoreapi.coupon.model.response; + +import lombok.*; + +@Builder +@Getter@ToString +@EqualsAndHashCode +@NoArgsConstructor +@AllArgsConstructor +public class CouponResponse { + private String couponId; +} diff --git a/src/main/java/com/t3t/bookstoreapi/coupon/service/CouponDetailService.java b/src/main/java/com/t3t/bookstoreapi/coupon/service/CouponDetailService.java index 70458a5e..2ececed6 100644 --- a/src/main/java/com/t3t/bookstoreapi/coupon/service/CouponDetailService.java +++ b/src/main/java/com/t3t/bookstoreapi/coupon/service/CouponDetailService.java @@ -1,5 +1,7 @@ package com.t3t.bookstoreapi.coupon.service; +import com.t3t.bookstoreapi.coupon.adapter.CouponAdapter; +import com.t3t.bookstoreapi.coupon.repository.CouponDetailRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -8,4 +10,10 @@ @Transactional @RequiredArgsConstructor public class CouponDetailService { + private final CouponDetailRepository couponDetailRepository; + private final CouponAdapter couponAdapter; +/* + public void saveCouponDetail(CouponDetailRequest request){ + + }*/ }