Skip to content

Commit

Permalink
Merge pull request #110 from nhnacademy-be5-T3Team/feature/coupon
Browse files Browse the repository at this point in the history
쿠폰 발급 관련 뷰 개발
  • Loading branch information
joohyun1996 authored May 16, 2024
2 parents 031ba7e + bf6b89e commit 04a0c75
Show file tree
Hide file tree
Showing 25 changed files with 729 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.t3t.frontserver.admin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class CouponController {
@GetMapping("/admin/coupons/register")
public String getRegisterView(){
return "admin/page/couponRegister";
}

@GetMapping("/admin/coupons/usageHistory")
public String getUsage(){
return "admin/page/couponHistory";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.t3t.frontserver.coupon.adapter;

import com.t3t.frontserver.coupon.client.CouponApiClient;
import com.t3t.frontserver.member.exception.CouponApiClientException;
import com.t3t.frontserver.util.FeignClientUtils;
import feign.FeignException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CouponAdapter {
private final CouponApiClient couponApiClient;

/**
* 회원이 도서 쿠폰 발급받기 위해 사용하는 api
* @author joohyun1996(이주현)
*/
public String registerBookCouponByMember(){
try{
couponApiClient.registerBookCoupon();
return "쿠폰이 등록되었습니다";
}catch(FeignException e){
throw new CouponApiClientException("도서쿠폰 등록에 실패하였습니다 " + FeignClientUtils.getMessageFromFeignException(e));
}
}

/**
* 회원이 카테고리 쿠폰 발급받기 위해 사용하는 api
* @author joohyun1996(이주현)
*/
public String registerCategoryCouponByMember(){
try{
couponApiClient.registerCategoryCoupon();
return "쿠폰이 등록되었습니다";
}catch(FeignException e){
throw new CouponApiClientException("카테고리쿠폰 등록에 실패하였습니다 " + FeignClientUtils.getMessageFromFeignException(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.t3t.frontserver.coupon.client;

import com.t3t.frontserver.model.response.BaseResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;


@FeignClient(name = "couponApiClient", url = "${t3t.feignClient.url}")
public interface CouponApiClient {

@PostMapping("/at/bookstore/members/coupons/book")
ResponseEntity<BaseResponse<Void>> registerBookCoupon();

@PostMapping("/at/bookstore/members/coupons/category")
ResponseEntity<BaseResponse<Void>> registerCategoryCoupon();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.t3t.frontserver.coupon.controller;

import com.t3t.frontserver.coupon.service.CouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class CouponRestController {
private final CouponService couponService;

@PostMapping("/coupons/book")
public void registerBookCouponByMember(){
couponService.registerBookCoupon();
}

@PostMapping("/coupons/category")
public void registerCategoryCouponByMember(){
couponService.registerCategoryCoupon();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.t3t.frontserver.coupon.model.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDate;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CouponDetailFindResponse {
public String couponId;
public BigDecimal discountRate;
public BigDecimal discountFee;
public LocalDate couponExpireDate;
public String couponType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.t3t.frontserver.coupon.model.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CouponDetailResponse {
public String couponId;
public String couponUseType;
public LocalDateTime couponUseDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.t3t.frontserver.coupon.service;

import com.t3t.frontserver.coupon.adapter.CouponAdapter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CouponService {
private final CouponAdapter couponAdapter;

public String registerBookCoupon(){
return couponAdapter.registerBookCouponByMember();
}

public String registerCategoryCoupon(){
return couponAdapter.registerCategoryCouponByMember();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.t3t.frontserver.member.adaptor;

import com.t3t.frontserver.coupon.model.response.CouponDetailFindResponse;
import com.t3t.frontserver.coupon.model.response.CouponDetailResponse;
import com.t3t.frontserver.member.client.MemberApiClient;
import com.t3t.frontserver.member.exception.CouponApiClientException;
import com.t3t.frontserver.member.exception.MemberApiClientException;
import com.t3t.frontserver.member.model.dto.MemberAddressDto;
import com.t3t.frontserver.member.model.request.MemberPasswordModifyRequest;
import com.t3t.frontserver.member.model.request.MemberRegistrationRequest;
import com.t3t.frontserver.member.model.response.MemberAdminResponse;
import com.t3t.frontserver.member.model.response.MemberInfoResponse;
import com.t3t.frontserver.member.model.response.MemberRegistrationResponse;
import com.t3t.frontserver.model.response.BaseResponse;
Expand Down Expand Up @@ -128,4 +132,53 @@ public void verifyMemberActivationCode(long memberId, String code) {
throw new MemberApiClientException("회원 활성화 코드 검증에 실패하였습니다. " + FeignClientUtils.getMessageFromFeignException(e));
}
}

/**
* 회원 목록 이름으로 조회
* @param name
* @author joohyun1996(이주현)
*/
public List<MemberAdminResponse> findMemberByName(String name) {
try {
return Optional.ofNullable(memberApiClient.findMemberByName(name).getBody())
.map(BaseResponse::getData)
.orElseThrow(MemberApiClientException::new);
} catch (FeignException e) {
throw new MemberApiClientException("회원 이름 목록 조회에 실패하였습니다. " + FeignClientUtils.getMessageFromFeignException(e));
}
}

/**
* 회원에게 관리자가 쿠폰 등록
* @author joohyun1996(이주현)
*/

public String registerCouponToMemberByAdmin(String couponType, Long memberId){
try{
memberApiClient.registerCouponToMemberByAdmin(couponType, memberId);
return "쿠폰 등록에 성공하였습니다";
}catch (FeignException e){
throw new CouponApiClientException("쿠폰 등록에 실패하였습니다 " + FeignClientUtils.getMessageFromFeignException(e));
}
}

public List<CouponDetailResponse> findAllCouponsByMemberId(){
try {
return Optional.ofNullable(memberApiClient.findAllCoupon().getBody())
.map(BaseResponse::getData)
.orElseThrow(CouponApiClientException::new);
} catch (FeignException e) {
throw new CouponApiClientException("쿠폰 목록 조회에 실패하였습니다. " + FeignClientUtils.getMessageFromFeignException(e));
}
}

public CouponDetailFindResponse getCouponDetails(String id){
try {
return Optional.ofNullable(memberApiClient.getCouponDetails(id).getBody())
.map(BaseResponse::getData)
.orElseThrow(CouponApiClientException::new);
} catch (FeignException e) {
throw new CouponApiClientException("쿠폰 세부사항 조회에 실패하였습니다. " + FeignClientUtils.getMessageFromFeignException(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.t3t.frontserver.member.client;

import com.t3t.frontserver.coupon.model.response.CouponDetailFindResponse;
import com.t3t.frontserver.coupon.model.response.CouponDetailResponse;
import com.t3t.frontserver.member.model.dto.MemberAddressDto;
import com.t3t.frontserver.member.model.request.MemberPasswordModifyRequest;
import com.t3t.frontserver.member.model.request.MemberRegistrationRequest;
import com.t3t.frontserver.member.model.response.MemberAdminResponse;
import com.t3t.frontserver.member.model.response.MemberInfoResponse;
import com.t3t.frontserver.member.model.response.MemberRegistrationResponse;
import com.t3t.frontserver.model.response.BaseResponse;
Expand Down Expand Up @@ -37,6 +40,14 @@ public interface MemberApiClient {
@GetMapping(value = "/t3t/bookstore/members/{memberId}")
ResponseEntity<BaseResponse<MemberInfoResponse>> getMemberById(@PathVariable("memberId") long memberId);

/**
* 회원 목록 이름으로 조회
* @param name
* @author joohyun1996(이주현)
*/
@GetMapping(value = "/at/bookstore/members")
ResponseEntity<BaseResponse<List<MemberAdminResponse>>> findMemberByName(@RequestParam("name") String name);

/**
* 회원 식별자로 특정 회원이 등록한 모든 회원 주소 정보들을 조회하는 API
*
Expand Down Expand Up @@ -83,4 +94,19 @@ public interface MemberApiClient {
@PostMapping("/t3t/bookstore/members/{memberId}/codes?type=verify")
BaseResponse<Void> verifyMemberActivationCertCode(@PathVariable("memberId") Long memberId, @RequestParam("value") String code);


/**
* 관리자가 회원에게 쿠폰 등록하기 위한 API
*
* @author joohyun1996(이주현)
*/
@PostMapping("/at/bookstore/members/coupons/{memberId}/{couponType}")
BaseResponse<Void> registerCouponToMemberByAdmin(@PathVariable("couponType") String couponType,
@PathVariable("memberId") Long memberId);

@GetMapping("/at/bookstore/members/coupons")
ResponseEntity<BaseResponse<List<CouponDetailResponse>>> findAllCoupon();

@GetMapping("/at/coupon/details/{couponId}")
ResponseEntity<BaseResponse<CouponDetailFindResponse>> getCouponDetails(@PathVariable("couponId") String id);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.t3t.frontserver.member.controller;

import com.t3t.frontserver.auth.util.SecurityContextUtils;
import com.t3t.frontserver.coupon.model.response.CouponDetailFindResponse;
import com.t3t.frontserver.coupon.model.response.CouponDetailResponse;
import com.t3t.frontserver.member.model.request.MemberPasswordModifyRequest;
import com.t3t.frontserver.member.model.request.MemberRegistrationRequest;
import com.t3t.frontserver.member.service.MemberService;
Expand All @@ -15,6 +17,8 @@
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
Expand All @@ -34,6 +38,20 @@ public String registerView(Model model) {
return "main/page/register";
}

@GetMapping("/members/coupons")
public String couponView(Model model){
List<CouponDetailResponse> responseList = memberService.findAllCouponsByMemberId();
List<CouponDetailFindResponse> couponDetailFindResponseList = new ArrayList<>();
for (CouponDetailResponse couponDetailResponse : responseList) {
CouponDetailFindResponse response = memberService.findCouponDetails(couponDetailResponse.getCouponId());
response.setCouponId(couponDetailResponse.getCouponId());

couponDetailFindResponseList.add(response);
}
model.addAttribute("couponList", couponDetailFindResponseList);
return "main/page/coupon";
}

/**
* 회원 가입 요청 처리
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.t3t.frontserver.member.controller;

import com.t3t.frontserver.member.model.response.MemberAdminResponse;
import com.t3t.frontserver.member.service.MemberService;
import com.t3t.frontserver.model.response.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class MemberRestController {
private final MemberService memberService;

@GetMapping("/members")
public ResponseEntity<List<MemberAdminResponse>> findAllMembersByName(@RequestParam("name") String name){
BaseResponse<List<MemberAdminResponse>> data = new BaseResponse<List<MemberAdminResponse>>().data(memberService.findMemberByName(name));

return ResponseEntity.status(HttpStatus.OK).body(data.getData());
}

@PostMapping("/coupon/{couponType}/{memberId}")
public ResponseEntity<BaseResponse<Void>> registerCouponToUserByAdmin(@PathVariable("couponType") String couponType,
@PathVariable("memberId") Long memberId){
return ResponseEntity.status(HttpStatus.CREATED).body(new BaseResponse<Void>().message(memberService.registCouponToMemberByAdmin(couponType, memberId)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.t3t.frontserver.member.exception;

public class CouponApiClientException extends RuntimeException{
private static final String DEFAULT_MESSAGES = "쿠폰 발급에 실패하였습니다";

public CouponApiClientException() {
super(DEFAULT_MESSAGES);
}

public CouponApiClientException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.t3t.frontserver.member.model.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MemberAdminResponse {
public Long id;
public String name;
public String email;
}
Loading

0 comments on commit 04a0c75

Please sign in to comment.