Skip to content

Commit

Permalink
feat: 경매 입찰 요청 반환 데이터 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
veronees committed Nov 18, 2024
1 parent d2f5903 commit 0058165
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.api.jaebichuri.bid.dto.AuctionBidHttpResponse.BidVolumeResponse;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -17,8 +18,8 @@ public class AuctionBidSocketResponse {
private Long newBidderId;
private Long auctionId;
private Integer numOfBidders;
private BidVolumeResponse bidVolumeResponse;// 오늘 거래량 volume
private BidDatePriceResponse bidDatePriceResponse; // 입찰 내역 하나 추가
private List<BidDatePriceResponse> top3BidDatePriceList;
private List<BidVolumeResponse> bidVolumeResponseList;

@Getter
@Builder
Expand All @@ -27,8 +28,8 @@ public static class AuctionBidSubscribersResponse {
private Long topBidPrice;
private Long canBidPrice;
private Integer numOfBidders;
private BidVolumeResponse bidVolumeResponse;// 오늘 거래량 volume
private BidDatePriceResponse bidDatePriceResponse; // 입찰 내역 하나 추가
private List<BidDatePriceResponse> top3BidDatePriceList;
private List<BidVolumeResponse> bidVolumeResponseList;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface AuctionBidMapper {
@Mapping(source = "bidDatePriceResponseList", target = "top3BidDatePriceList")
@Mapping(source = "bidVolumeResponseList", target = "bidVolumeResponseList")
@Mapping(source = "memberId", target = "memberId")
AuctionBidHttpResponse toHttpResponse(Auction auction, AuctionProduct product, AuctionBid auctionBid,
AuctionBidHttpResponse toHttpResponse(Auction auction, AuctionProduct product,
AuctionBid auctionBid,
Long canBidPrice, Long numOfBidders, List<BidDatePriceResponse> bidDatePriceResponseList,
List<BidVolumeResponse> bidVolumeResponseList, Long memberId);

Expand Down Expand Up @@ -61,11 +62,12 @@ AuctionBidHttpResponse toHttpResponse(Auction auction, AuctionProduct product, A
@Mapping(source = "newBidderId", target = "newBidderId")
@Mapping(source = "auctionId", target = "auctionId")
@Mapping(source = "numOfBidders", target = "numOfBidders")
@Mapping(source = "bidVolumeResponse", target = "bidVolumeResponse")
@Mapping(source = "bidDatePriceResponse", target = "bidDatePriceResponse")
@Mapping(source = "top3BidDatePriceList", target = "top3BidDatePriceList")
@Mapping(source = "bidVolumeResponseList", target = "bidVolumeResponseList")
AuctionBidSocketResponse toSocketResponse(Long topBidPrice, Long canBidPrice,
Long previousBidderId, Long newBidderId, Long auctionId, Long numOfBidders,
BidVolumeResponse bidVolumeResponse, BidDatePriceResponse bidDatePriceResponse);
List<BidDatePriceResponse> top3BidDatePriceList,
List<BidVolumeResponse> bidVolumeResponseList);

@Mapping(target = "isSuccess", constant = "true")
@Mapping(target = "code", expression = "java(getSuccessCode())")
Expand All @@ -80,8 +82,8 @@ AuctionBidSocketResponse toSocketResponse(Long topBidPrice, Long canBidPrice,
@Mapping(source = "socketResponse.topBidPrice", target = "topBidPrice")
@Mapping(source = "socketResponse.canBidPrice", target = "canBidPrice")
@Mapping(source = "socketResponse.numOfBidders", target = "numOfBidders")
@Mapping(source = "socketResponse.bidVolumeResponse", target = "bidVolumeResponse")
@Mapping(source = "socketResponse.bidDatePriceResponse", target = "bidDatePriceResponse")
@Mapping(source = "socketResponse.top3BidDatePriceList", target = "top3BidDatePriceList")
@Mapping(source = "socketResponse.bidVolumeResponseList", target = "bidVolumeResponseList")
AuctionBidSubscribersResponse toSubscribersResponse(AuctionBidSocketResponse socketResponse);

default String getSuccessCode() {
Expand Down
70 changes: 25 additions & 45 deletions src/main/java/com/api/jaebichuri/bid/service/AuctionBidService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import com.api.jaebichuri.member.entity.Member;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -52,18 +50,19 @@ public AuctionBidHttpResponse getAuctionBid(Long auctionId, Member member) {
return auctionBidRepository.findTopByAuctionOrderByBidPriceDesc(auction)
.map(topAuctionBid -> {
Long canBidPrice = calculateCanBidPrice(topAuctionBid.getBidPrice());
return auctionBidMapper.toHttpResponse(auction, auction.getProduct(), topAuctionBid, canBidPrice,
return auctionBidMapper.toHttpResponse(auction, auction.getProduct(), topAuctionBid,
canBidPrice,
numOfBidders, top3BidDatePriceList, bidVolumeResponseList, member.getId());
}).orElseGet(() -> auctionBidMapper.toHttpResponse(auction, auction.getProduct(), member.getId()));
}).orElseGet(() -> auctionBidMapper.toHttpResponse(auction, auction.getProduct(),
member.getId()));
}

@DistributedLock(key = "auctionId")
@Transactional
public AuctionBidSocketResponse saveAuctionBid(Member member, Long auctionId,
AuctionBidRequestDto requestDto) {
Auction auction = auctionRepository.findById(auctionId)
.orElseThrow(() -> new CustomSocketException(ErrorStatus._AUCTION_NOT_FOUND,
member.getId()));
Auction auction = auctionRepository.findById(auctionId).orElseThrow(
() -> new CustomSocketException(ErrorStatus._AUCTION_NOT_FOUND, member.getId()));

Long requestBidPrice = requestDto.getBidPrice();

Expand Down Expand Up @@ -106,13 +105,17 @@ public List<BidVolumeResponse> getVolumeResponseList(Auction auction) {
Collectors.counting()
));

List<BidVolumeResponse> bidVolumeResponseList = new ArrayList<>();
volumeByDate.forEach((date, volume) -> {
BidVolumeResponse bidVolumeResponse = auctionBidMapper.toBidVolumeResponse(date,
volume);
bidVolumeResponseList.add(bidVolumeResponse);
});
return bidVolumeResponseList;
return volumeByDate.entrySet().stream()
.sorted((entry1, entry2) -> {
LocalDate date1 = LocalDate.parse(entry1.getKey(),
DateTimeFormatter.ofPattern("MM/dd"));
LocalDate date2 = LocalDate.parse(entry2.getKey(),
DateTimeFormatter.ofPattern("MM/dd"));
return date2.compareTo(date1);
})
.limit(10)
.map(entry -> auctionBidMapper.toBidVolumeResponse(entry.getKey(), entry.getValue()))
.toList();
}

private AuctionBidSocketResponse handleFirstAuctionBid(Member member, Auction auction,
Expand All @@ -128,18 +131,12 @@ private AuctionBidSocketResponse handleFirstAuctionBid(Member member, Auction au

Long responseCanBidPrice = calculateCanBidPrice(auctionBid.getBidPrice());

Long todayVolume = getTodayVolume(auction);

BidVolumeResponse bidVolumeResponse = auctionBidMapper.toBidVolumeResponse(getTodayFormat(),
todayVolume);

BidDatePriceResponse bidDatePriceResponse = auctionBidMapper.toBidDatePriceResponse(
getTodayFormat(), requestBidPrice);
List<BidVolumeResponse> volumeResponseList = getVolumeResponseList(auction);
List<BidDatePriceResponse> bidDatePriceResponseList = getBidDatePriceResponseList(auction);

return auctionBidMapper.toSocketResponse(requestBidPrice, responseCanBidPrice,
auctionBid.getBidder().getId(), member.getId(), auction.getId(),
auction.updateNumOfBidders().getNumOfBidders(), bidVolumeResponse,
bidDatePriceResponse);
auction.updateNumOfBidders().getNumOfBidders(), bidDatePriceResponseList, volumeResponseList);
}

private void validateAuctionBid(Member member, AuctionBid topAuctionBid, Long requestBidPrice) {
Expand Down Expand Up @@ -169,17 +166,12 @@ private AuctionBidSocketResponse handleNewAuctionBid(Member member, Auction auct
Long numOfParticipants = isNotNewBidder ? auction.getNumOfBidders()
: auction.updateNumOfBidders().getNumOfBidders();

Long todayVolume = getTodayVolume(auction);

BidVolumeResponse bidVolumeResponse = auctionBidMapper.toBidVolumeResponse(getTodayFormat(),
todayVolume);

BidDatePriceResponse bidDatePriceResponse = auctionBidMapper.toBidDatePriceResponse(
getTodayFormat(), requestBidPrice);
List<BidVolumeResponse> volumeResponseList = getVolumeResponseList(auction);
List<BidDatePriceResponse> bidDatePriceResponseList = getBidDatePriceResponseList(auction);

return auctionBidMapper.toSocketResponse(requestBidPrice, responseCanBidPrice,
previousTopAuctionBid.getBidder().getId(), member.getId(), auction.getId(), numOfParticipants,
bidVolumeResponse, bidDatePriceResponse);
previousTopAuctionBid.getBidder().getId(), member.getId(), auction.getId(),
numOfParticipants, bidDatePriceResponseList, volumeResponseList);
}

private Long calculateCanBidPrice(Long nowBestBidPrice) {
Expand All @@ -199,16 +191,4 @@ private AuctionBid saveAuctionBid(Member member, Auction auction, Long requestBi
auctionBidRepository.save(auctionBid);
return auctionBid;
}

private Long getTodayVolume(Auction auction) {
LocalDateTime startOfDay = LocalDateTime.now().with(LocalTime.MIN); // 오늘 자정
LocalDateTime endOfDay = LocalDateTime.now().with(LocalTime.MAX); // 오늘의 마지막 순간

return auctionBidRepository.countByAuctionAndCreatedAtBetween(auction, startOfDay,
endOfDay);
}

private String getTodayFormat() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM/dd"));
}
}

0 comments on commit 0058165

Please sign in to comment.