Skip to content

Commit

Permalink
refactor: #756 입찰 시 Optional 사용 로직 변경 (#757)
Browse files Browse the repository at this point in the history
* refactor: 입찰 성공 시 알림 전송 관련 Optional 사용 로직 변경

* feat: 경매의 마지막 입찰을 조회하는 기능 추가

* refactor: 입찰 시 입찰하고자 하는 내용이 유효한지 검증하는 로직 변경

* refactor: 사용하지 않는 메서드 제거
  • Loading branch information
kwonyj1022 authored Nov 23, 2023
1 parent f913455 commit 9a605d8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,8 @@ public Optional<User> findLastBidder() {

return Optional.of(lastBid.getBidder());
}

public Optional<Bid> findLastBid() {
return Optional.ofNullable(lastBid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@Transactional(readOnly = true)
Expand All @@ -47,37 +46,34 @@ public Long create(final CreateBidDto bidDto, final String auctionImageAbsoluteU
.orElseThrow(() -> new AuctionNotFoundException("해당 경매를 찾을 수 없습니다."));

final Auction auction = auctionAndImageDto.auction();

checkInvalidAuction(auction);
checkInvalidBid(auction, bidder, bidDto);

final Optional<User> previousBidder = auction.findLastBidder();

final Bid saveBid = saveAndUpdateLastBid(bidDto, auction, bidder);

publishBidNotificationEvent(auctionImageAbsoluteUrl, auctionAndImageDto, previousBidder);
auction.findLastBidder()
.ifPresent(previousBidder ->
publishBidNotificationEvent(auctionImageAbsoluteUrl, auctionAndImageDto, previousBidder));

return saveBid.getId();
return saveAndUpdateLastBid(bidDto, auction, bidder).getId();
}

private void publishBidNotificationEvent(
final String auctionImageAbsoluteUrl,
final AuctionAndImageDto auctionAndImageDto,
final Optional<User> previousBidder
final User previousBidder
) {
if (previousBidder.isEmpty()) {
return;
}

final BidDto bidDto = new BidDto(
previousBidder.get().getId(),
previousBidder.getId(),
auctionAndImageDto,
auctionImageAbsoluteUrl
);

bidEventPublisher.publishEvent(new BidNotificationEvent(bidDto));
}

private void checkInvalidAuction(final Auction auction) {
final LocalDateTime now = LocalDateTime.now();

if (auction.isClosed(now)) {
throw new InvalidAuctionToBidException("이미 종료된 경매입니다");
}
Expand All @@ -87,18 +83,18 @@ private void checkInvalidAuction(final Auction auction) {
}

private void checkInvalidBid(final Auction auction, final User bidder, final CreateBidDto bidDto) {
final Optional<Bid> lastBid = bidRepository.findLastBidByAuctionId(bidDto.auctionId());
final BidPrice bidPrice = processBidPrice(bidDto.bidPrice());

checkIsSeller(auction, bidder);

if (lastBid.isPresent()) {
checkIsNotLastBidder(lastBid.get(), bidder);
checkInvalidBidPrice(lastBid.get(), bidPrice);
return;
}
final BidPrice bidPrice = processBidPrice(bidDto.bidPrice());

checkInvalidFirstBidPrice(auction, bidPrice);
auction.findLastBid()
.ifPresentOrElse(
lastBid -> {
checkIsNotLastBidder(lastBid, bidder);
checkInvalidBidPrice(lastBid, bidPrice);
},
() -> checkInvalidFirstBidPrice(auction, bidPrice)
);
}

private BidPrice processBidPrice(final int value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import com.ddang.ddang.bid.domain.Bid;

import java.util.List;
import java.util.Optional;

public interface BidRepository {

Bid save(final Bid bid);

List<Bid> findAllByAuctionId(final Long auctionId);

Optional<Bid> findLastBidByAuctionId(final Long auctionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
Expand All @@ -23,9 +22,4 @@ public Bid save(final Bid bid) {
public List<Bid> findAllByAuctionId(final Long auctionId) {
return jpaBidRepository.findAllByAuctionIdOrderByIdAsc(auctionId);
}

@Override
public Optional<Bid> findLastBidByAuctionId(final Long auctionId) {
return jpaBidRepository.findLastBidByAuctionId(auctionId);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.ddang.ddang.auction.domain;

import static org.assertj.core.api.Assertions.assertThat;

import com.ddang.ddang.auction.domain.fixture.AuctionFixture;
import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.user.domain.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class AuctionTest extends AuctionFixture {
Expand Down Expand Up @@ -352,6 +353,43 @@ class AuctionTest extends AuctionFixture {
assertThat(actual).isEmpty();
}

@Test
void 마지막_입찰을_반환한다() {
// given
final Auction auction = Auction.builder()
.title("제목")
.seller(판매자)
.closingTime(LocalDateTime.now().minusDays(3L))
.startPrice(new Price(1_000))
.bidUnit(new BidUnit(1_000))
.build();
final Bid bid = new Bid(auction, 구매자, 유효한_입찰_금액);

auction.updateLastBid(bid);

// when
final Optional<Bid> actual = auction.findLastBid();

// then
assertThat(actual).contains(bid);
}

@Test
void 마지막_입찰이_없다면_빈_Optional을_반환한다() {
// given
final Auction auction = Auction.builder()
.title("제목")
.seller(판매자)
.closingTime(LocalDateTime.now().minusDays(3L))
.build();

// when
final Optional<Bid> actual = auction.findLastBid();

// then
assertThat(actual).isEmpty();
}

@Test
void 경매를_진행중이며_입찰자가_없는_경우_UNBIDDEN을_반환한다() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.context.annotation.Import;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -56,13 +55,4 @@ void setUp(@Autowired final JpaBidRepository jpaBidRepository) {
softAssertions.assertThat(actual.get(1).getId()).isEqualTo(경매1의_입찰2겸_마지막_입찰.getId());
});
}

@Test
void 특정_경매의_마지막_입찰을_조회한다() {
// when
final Optional<Bid> actual = bidRepository.findLastBidByAuctionId(경매1.getId());

// then
assertThat(actual.get().getId()).isEqualTo(경매1의_입찰2겸_마지막_입찰.getId());
}
}

0 comments on commit 9a605d8

Please sign in to comment.