-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from nhnacademy-be7-heukbaekbook/feature/coupon
Feature/coupon
- Loading branch information
Showing
25 changed files
with
435 additions
and
233 deletions.
There are no files selected for viewing
17 changes: 0 additions & 17 deletions
17
...a/com/nhnacademy/heukbaekbookshop/couponset/coupon/controller/MemberCouponController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
.../com/nhnacademy/heukbaekbookshop/couponset/coupon/dto/response/CouponHistoryResponse.java
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
...a/com/nhnacademy/heukbaekbookshop/couponset/coupon/dto/response/MemberCouponResponse.java
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
.../com/nhnacademy/heukbaekbookshop/couponset/coupon/repository/CouponHistoryRepository.java
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
...in/java/com/nhnacademy/heukbaekbookshop/couponset/coupon/service/MemberCouponService.java
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
...om/nhnacademy/heukbaekbookshop/couponset/coupon/service/impl/MemberCouponServiceImpl.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
...nacademy/heukbaekbookshop/couponset/couponhistory/controller/CouponHistoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...m/nhnacademy/heukbaekbookshop/couponset/couponhistory/dto/mapper/CouponHistoryMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...nhnacademy/heukbaekbookshop/couponset/couponhistory/dto/request/CouponHistoryRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) {} |
15 changes: 15 additions & 0 deletions
15
...nacademy/heukbaekbookshop/couponset/couponhistory/dto/response/CouponHistoryResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) {} | ||
|
||
|
13 changes: 13 additions & 0 deletions
13
...nacademy/heukbaekbookshop/couponset/couponhistory/repository/CouponHistoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
11 changes: 11 additions & 0 deletions
11
...com/nhnacademy/heukbaekbookshop/couponset/couponhistory/service/CouponHistoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
52 changes: 52 additions & 0 deletions
52
...ademy/heukbaekbookshop/couponset/couponhistory/service/impl/CouponHistoryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.