Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/180-fix-bidding #182

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,44 @@ public void bid(Long loginMemberId, Long artWorkId, Long price) {
ArtWork artWork = getArtWorkOrThrow(artWorkId);
Member member = getMemberOrThrow(loginMemberId);

Long topPrice = getTopPrice(artWork);
Bidding bidding = biddingRepository.findByArtWorkAndMember(artWork, member)
.orElseGet(() -> saveBidding(artWork, member));
// μž‘ν’ˆμ— λŒ€ν•œ 졜초 응찰일 경우
if (!biddingRepository.existsByArtWorkId(artWork.getId())) {

bidding.raisePrice(topPrice, price);
eventPublisher.publishEvent(new ArtWorkEvent(member, artWork, bidding, NotificationCode.SUGGEST_BID));
eventPublisher.publishEvent(new ArtWorkEvent(member, artWork, null, NotificationCode.STILL_BID));
// μ‹œμž‘κ°€μ— 맞좰 응찰할 경우
if(price.equals(artWork.getPrice())) {

Bidding firstBidding = Bidding.builder().member(member).artWork(artWork).price(price).build();
biddingRepository.save(firstBidding);
validateAuctionPeriod(artWork.getAuction());
return;

} // μ‹œμž‘κ°€λ³΄λ‹€ 높은 가격에 응찰할 경우
else if (price > artWork.getPrice()) {

if (!isValidPrice(artWork.getPrice(), price)) {
throw new CustomException(ErrorCode.NOT_VALID_BID);
}

Bidding firstBidding = Bidding.builder().member(member).artWork(artWork).price(price).build();
biddingRepository.save(firstBidding);
validateAuctionPeriod(artWork.getAuction());
return;
}

throw new CustomException(ErrorCode.NOT_VALID_BID);

}else{

Long topPrice = getTopPrice(artWork);
Bidding bidding = biddingRepository.findByArtWorkAndMember(artWork, member)
.orElseGet(() -> saveBidding(artWork, member));

validateAuctionPeriod(artWork.getAuction());

bidding.raisePrice(topPrice, price);
eventPublisher.publishEvent(new ArtWorkEvent(member, artWork, bidding, NotificationCode.SUGGEST_BID));
eventPublisher.publishEvent(new ArtWorkEvent(member, artWork, null, NotificationCode.STILL_BID));
}
}

private ArtWork getArtWorkOrThrow(Long artWorkId) {
Expand Down Expand Up @@ -187,7 +218,6 @@ private Bidding saveBidding(ArtWork artWork, Member member) {
.build());

bidding.setArtWork(artWork);
bidding.validateAuctionPeriod();

return bidding;
}
Expand Down Expand Up @@ -266,6 +296,7 @@ public BiddingListResponse getBiddingList(Long artWorkId) {
List<Bidding> biddingList = biddingRepository.findAllByArtWorkOrderByPriceDesc(artWork);



return BiddingListResponse.builder()
.artWork(BiddingListResponse.ArtWorkDto.of(artWork, getTopPrice(artWork), fileManager.getFullPath(artWork.getMainImage())))
.auction(BiddingListResponse.AuctionDto.from(artWork.getAuction()))
Expand Down Expand Up @@ -388,4 +419,40 @@ public ArtWorkPurchasedListResponseDto getMyBidding(Member member) {
.biddingList(biddingDtoList)
.build();
}

private boolean isValidPrice(Long topPrice, Long price) {

long priceDifference = price - topPrice;

if (topPrice < 300_000) {
if (priceDifference >= 20_000) {
return true;
}
} else if (topPrice < 1_000_000) {
if (priceDifference >= 50_000) {
return true;
}
} else if (topPrice < 3_000_000) {
if (priceDifference >= 100_000) {
return true;
}
} else if (topPrice < 5_000_000) {
if (priceDifference >= 200_000) {
return true;
}
} else {
if (priceDifference >= 500_000) {
return true;
}
}

return false;
}

public void validateAuctionPeriod(Auction auction) {

if (!auction.getStatus().equals(AuctionStatus.PROCESSING.getType())) {
throw new CustomException(ErrorCode.NOT_VALID_AUCTION_PERIOD);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sptp.backend.bidding.repository;

import com.sptp.backend.art_work.repository.ArtWork;
import com.sptp.backend.auction.repository.AuctionStatus;
import com.sptp.backend.common.entity.BaseEntity;
import com.sptp.backend.common.exception.CustomException;
import com.sptp.backend.common.exception.ErrorCode;
Expand All @@ -12,6 +13,7 @@
import lombok.experimental.SuperBuilder;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
Expand All @@ -35,7 +37,8 @@ public class Bidding extends BaseEntity {
private Long price;

public void validateAuctionPeriod() {
if (!artWork.getAuction().isValidPeriod(getCreatedDate())) {

if (!artWork.getAuction().getStatus().equals(AuctionStatus.PROCESSING.getType())) {
throw new CustomException(ErrorCode.NOT_VALID_AUCTION_PERIOD);
}
}
Expand Down