-
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 #50 from LeejuButU/feat/service/BidHistory
BidHistory Service 작성
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/LeejuButU/BidCycle/domain/bidHistory/repository/BidHistoryRepository.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 |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package LeejuButU.BidCycle.domain.bidHistory.repository; | ||
|
||
import LeejuButU.BidCycle.domain.bidHistory.domain.BidHistory; | ||
import LeejuButU.BidCycle.domain.product.domain.Product; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface BidHistoryRepository extends JpaRepository<BidHistory, Long> { | ||
@Query("select b " + | ||
" from BidHistory b " + | ||
"where b.product = :product " + | ||
"order by b.bidPrice desc " + | ||
"limit 1") | ||
BidHistory findMostHighPriceBidHistoryInProduct(@Param("product") Product product); | ||
|
||
List<BidHistory> findAllByProduct(Product product); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/LeejuButU/BidCycle/domain/bidHistory/service/BidHistoryService.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,60 @@ | ||
package LeejuButU.BidCycle.domain.bidHistory.service; | ||
|
||
import LeejuButU.BidCycle.domain.bidHistory.domain.BidHistory; | ||
import LeejuButU.BidCycle.domain.bidHistory.repository.BidHistoryRepository; | ||
import LeejuButU.BidCycle.domain.member.domain.Member; | ||
import LeejuButU.BidCycle.domain.member.repository.MemberRepository; | ||
import LeejuButU.BidCycle.domain.product.domain.Product; | ||
import LeejuButU.BidCycle.domain.product.repository.ProductRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class BidHistoryService { | ||
private final BidHistoryRepository bidHistoryRepository; | ||
private final ProductRepository productRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
// 입찰하기, 가장 최근 입찰보다 반드시 높은 가격을 불러야 함 | ||
@Transactional | ||
public BidHistory bidProduct(Long bidderId, Long productId, long price) { | ||
Member bidder = memberRepository.findById(bidderId).orElseThrow( | ||
() -> new IllegalArgumentException("존재하지 않는 멤버입니다.") | ||
); | ||
Product product = productRepository.findById(productId).orElseThrow( | ||
() -> new IllegalArgumentException("존재하지 않는 상품입니다.") | ||
); | ||
|
||
validateBidPriceIsGreaterThanBefore(product, price); | ||
|
||
BidHistory bidHistory = BidHistory.builder() | ||
.bidder(bidder) | ||
.product(product) | ||
.bidPrice(price) | ||
.build(); | ||
bidHistoryRepository.save(bidHistory); | ||
return bidHistory; | ||
} | ||
|
||
// 입찰 취소 | ||
public void deleteBidHistory(Long bidHistoryId) { | ||
bidHistoryRepository.deleteById(bidHistoryId); | ||
} | ||
|
||
// 상품 하나에 대한 입찰 내역 데이터 조회 | ||
public List<BidHistory> findAllBidHistoryFromProduct(Product product) { | ||
return bidHistoryRepository.findAllByProduct(product); | ||
} | ||
|
||
private void validateBidPriceIsGreaterThanBefore(Product product, long price) { | ||
BidHistory bidHistory = bidHistoryRepository.findMostHighPriceBidHistoryInProduct(product); | ||
if (price <= bidHistory.getBidPrice()) { | ||
throw new IllegalArgumentException("가격은 반드시 이전 가격보다 높아야 합니다."); | ||
} | ||
} | ||
} |