Skip to content

Commit

Permalink
feature:#160 쿠폰 CRUD 추가 및 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
joohyun1996 committed May 14, 2024
1 parent f40c451 commit ed860f3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<BaseResponse<List<CategoryIdResponse>>> getCategoriesId();
@GetMapping("/t3t/coupon/coupons/book")
ResponseEntity<BaseResponse<CouponResponse>> getBookCoupon();

@GetMapping(value = "/t3t/bookstore/categories/{categoryId}/coupons")
ResponseEntity<BaseResponse<CategoryIdResponse>> getCategoryId(@PathVariable("categoryId") Integer categoryId);*/
@GetMapping("/t3t/coupon/coupons/category")
ResponseEntity<BaseResponse<CouponResponse>> getCategoryCoupon();

//todo 1 ) Gateway에서 쿠폰 등록

// todo 2 ) book coupon, category coupon, general coupon FeignClient 이용해 가져오기

// todo 3 ) 가져온 것 db에 넣어놓기
@GetMapping("/t3t/coupon/coupons/general")
ResponseEntity<BaseResponse<CouponResponse>> getGeneralCoupon();
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,4 +10,10 @@
@Transactional
@RequiredArgsConstructor
public class CouponDetailService {
private final CouponDetailRepository couponDetailRepository;
private final CouponAdapter couponAdapter;
/*
public void saveCouponDetail(CouponDetailRequest request){
}*/
}

0 comments on commit ed860f3

Please sign in to comment.