Skip to content

Commit

Permalink
Merge pull request #163 from nhnacademy-be7-heukbaekbook/feature/coupon
Browse files Browse the repository at this point in the history
Feature/coupon
  • Loading branch information
cramfull authored Nov 25, 2024
2 parents b24fe69 + 530aa06 commit 68bac6b
Show file tree
Hide file tree
Showing 25 changed files with 435 additions and 233 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
import com.nhnacademy.heukbaekbookshop.couponset.coupon.domain.Coupon;
import com.nhnacademy.heukbaekbookshop.couponset.coupon.dto.request.CouponRequest;
import com.nhnacademy.heukbaekbookshop.couponset.coupon.dto.response.CouponResponse;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.domain.CouponHistory;
import com.nhnacademy.heukbaekbookshop.couponset.couponpolicy.domain.CouponPolicy;
import com.nhnacademy.heukbaekbookshop.couponset.couponpolicy.dto.mapper.CouponPolicyMapper;
import com.nhnacademy.heukbaekbookshop.couponset.membercoupon.domain.MemberCoupon;
import com.nhnacademy.heukbaekbookshop.couponset.membercoupon.dto.response.MemberCouponResponse;
import com.nhnacademy.heukbaekbookshop.memberset.member.domain.Member;
import com.nhnacademy.heukbaekbookshop.order.domain.OrderBook;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.time.LocalDateTime;


public class CouponMapper {

Expand Down Expand Up @@ -65,4 +72,38 @@ public static CategoryCoupon toCategoryCouponEntity(CouponRequest couponRequest,
category);
}

public static MemberCoupon toMemberCouponEntity(Member member, Coupon coupon, int availableDate){
return MemberCoupon.builder()
.member(member)
.coupon(coupon)
.issuedAt(LocalDateTime.now())
.expirationAt(LocalDateTime.now().plusDays(availableDate)).build();
}

public static MemberCouponResponse fromMemberCouponEntity(MemberCoupon memberCoupon) {
return new MemberCouponResponse(
memberCoupon.getId(),
memberCoupon.getCoupon().getId(),
memberCoupon.isCouponUsed(),
memberCoupon.getIssuedAt(),
memberCoupon.getExpirationAt()
);
}

public static CouponHistory toCouponHistoryEntity(MemberCoupon memberCoupon, OrderBook orderBook) {
return new CouponHistory(
null,
memberCoupon,
LocalDateTime.now(),
orderBook,
memberCoupon.getCoupon()
);
}


public static Page<MemberCouponResponse> fromMemberCouponPageableEntity(Page<MemberCoupon> memberCoupons) {
return memberCoupons.map(CouponMapper::fromMemberCouponEntity);
}
}


This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.controller;

import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.request.CouponHistoryRequest;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.response.CouponHistoryResponse;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.service.CouponHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/coupons/histories")
public class CouponHistoryController {
private final CouponHistoryService couponHistoryService;

/**
* 사용자 쿠폰 사용 내역 조회
*
* @param memberId 회원 ID
* @param pageable 페이징 정보
* @return 쿠폰 사용 내역 목록
*/
@GetMapping("/members/{memberId}")
public ResponseEntity<Page<CouponHistoryResponse>> getCouponHistoriesByUser(
@PathVariable Long memberId,
Pageable pageable
) {
Page<CouponHistoryResponse> histories = couponHistoryService.getCouponHistoryByCustomerId(memberId, pageable);
return ResponseEntity.ok(histories);
}

/**
* 쿠폰 사용 기록 생성
*
* @param couponHistoryRequest 쿠폰 사용 요청 DTO
* @return 성공 여부
*/
@PostMapping
public ResponseEntity<Void> createCouponHistory(@RequestBody CouponHistoryRequest couponHistoryRequest) {
couponHistoryService.createCouponHistory(couponHistoryRequest);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.nhnacademy.heukbaekbookshop.couponset.coupon.domain;
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.domain;

import com.nhnacademy.heukbaekbookshop.couponset.coupon.domain.Coupon;
import com.nhnacademy.heukbaekbookshop.couponset.membercoupon.domain.MemberCoupon;
import com.nhnacademy.heukbaekbookshop.order.domain.OrderBook;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "coupons_histories")
Expand All @@ -38,4 +38,9 @@ public class CouponHistory {
})
private OrderBook orderBook;

@ManyToOne
@JoinColumn(name = "coupon_id", referencedColumnName = "coupon_id")
private Coupon coupon;
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.mapper;

import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.domain.CouponHistory;
import com.nhnacademy.heukbaekbookshop.couponset.membercoupon.domain.MemberCoupon;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.response.CouponHistoryResponse;
import com.nhnacademy.heukbaekbookshop.order.domain.OrderBook;

import java.time.LocalDateTime;

public class CouponHistoryMapper {
public static CouponHistoryResponse toResponse(CouponHistory couponHistory) {
return new CouponHistoryResponse(
couponHistory.getId(),
couponHistory.getMemberCoupon().getId(),
couponHistory.getMemberCoupon().getMember().getId(),
couponHistory.getMemberCoupon().getCoupon().getId(),
couponHistory.getUsedAt(),
couponHistory.getOrderBook().getBook().getId(),
couponHistory.getOrderBook().getOrder().getId()
);
}

public static CouponHistory toCouponHistoryEntity(MemberCoupon memberCoupon, OrderBook orderBook) {
return new CouponHistory(
null,
memberCoupon,
LocalDateTime.now(),
orderBook,
memberCoupon.getCoupon()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.request;

public record CouponHistoryRequest(
Long memberCouponId,
Long orderId,
Long bookId
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.response;

import java.time.LocalDateTime;

public record CouponHistoryResponse(
Long couponHistoryId,
Long memberCouponId,
Long memberId,
Long couponId,
LocalDateTime usedAt,
Long bookId,
Long orderId
) {}


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.domain.CouponHistory;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CouponHistoryRepository extends JpaRepository<CouponHistory, Long> {
// 사용자 ID를 기준으로 쿠폰 사용 내역 조회
Page<CouponHistory> findByMemberCoupon_Member_Id(Pageable pageable, Long memberId);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.service;

import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.request.CouponHistoryRequest;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.response.CouponHistoryResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface CouponHistoryService {
void createCouponHistory(CouponHistoryRequest couponHistoryRequest);
Page<CouponHistoryResponse> getCouponHistoryByCustomerId(Long memberId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.nhnacademy.heukbaekbookshop.couponset.couponhistory.service.impl;

import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.domain.CouponHistory;
import com.nhnacademy.heukbaekbookshop.couponset.membercoupon.domain.MemberCoupon;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.mapper.CouponHistoryMapper;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.request.CouponHistoryRequest;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.dto.response.CouponHistoryResponse;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.repository.CouponHistoryRepository;
import com.nhnacademy.heukbaekbookshop.couponset.membercoupon.repository.MemberCouponRepository;
import com.nhnacademy.heukbaekbookshop.couponset.couponhistory.service.CouponHistoryService;
import com.nhnacademy.heukbaekbookshop.order.domain.OrderBook;
import com.nhnacademy.heukbaekbookshop.order.repository.OrderBookRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class CouponHistoryServiceImpl implements CouponHistoryService {
private final CouponHistoryRepository couponHistoryRepository;
private final OrderBookRepository orderBookRepository;
private final MemberCouponRepository memberCouponRepository;

@Override
@Transactional
public void createCouponHistory(CouponHistoryRequest couponHistoryRequest) {
MemberCoupon memberCoupon = memberCouponRepository.findById(couponHistoryRequest.memberCouponId())
.orElseThrow(() -> new IllegalStateException("해당 ID의 회원 쿠폰을 찾을 수 없습니다 : " +
couponHistoryRequest.memberCouponId()));

OrderBook orderBook = orderBookRepository.findByOrderIdAndBookId(
couponHistoryRequest.orderId(), couponHistoryRequest.bookId());
if (orderBook == null) {
throw new IllegalStateException("해당 주문 ID에 대한 주문 도서를 찾을 수 없습니다 : " +
couponHistoryRequest.orderId() + " 해당 도서 ID에 대한 주문 도서를 찾을 수 없습니다 : " + couponHistoryRequest.bookId());
}

CouponHistory couponHistory = CouponHistoryMapper.toCouponHistoryEntity(memberCoupon, orderBook);

couponHistoryRepository.save(couponHistory);
}


@Override
@Transactional(readOnly = true)
public Page<CouponHistoryResponse> getCouponHistoryByCustomerId(Long memberId, Pageable pageable) {
return couponHistoryRepository.findByMemberCoupon_Member_Id(pageable, memberId)
.map(CouponHistoryMapper::toResponse);
}
}
Loading

0 comments on commit 68bac6b

Please sign in to comment.