Skip to content

Commit

Permalink
Merge pull request #50 from LeejuButU/feat/service/BidHistory
Browse files Browse the repository at this point in the history
BidHistory Service 작성
  • Loading branch information
ezeun authored Apr 21, 2024
2 parents cd4042e + d0ea348 commit f906d02
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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);
}
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("가격은 반드시 이전 가격보다 높아야 합니다.");
}
}
}

0 comments on commit f906d02

Please sign in to comment.