From 00581657c13a30c54e3140dfde8846522a127bab Mon Sep 17 00:00:00 2001 From: veronees Date: Tue, 19 Nov 2024 00:52:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B2=BD=EB=A7=A4=20=EC=9E=85=EC=B0=B0?= =?UTF-8?q?=20=EC=9A=94=EC=B2=AD=20=EB=B0=98=ED=99=98=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bid/dto/AuctionBidSocketResponse.java | 9 +-- .../bid/mapper/AuctionBidMapper.java | 14 ++-- .../bid/service/AuctionBidService.java | 70 +++++++------------ 3 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/api/jaebichuri/bid/dto/AuctionBidSocketResponse.java b/src/main/java/com/api/jaebichuri/bid/dto/AuctionBidSocketResponse.java index c6781af..ed0605e 100644 --- a/src/main/java/com/api/jaebichuri/bid/dto/AuctionBidSocketResponse.java +++ b/src/main/java/com/api/jaebichuri/bid/dto/AuctionBidSocketResponse.java @@ -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; @@ -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 top3BidDatePriceList; + private List bidVolumeResponseList; @Getter @Builder @@ -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 top3BidDatePriceList; + private List bidVolumeResponseList; } @Getter diff --git a/src/main/java/com/api/jaebichuri/bid/mapper/AuctionBidMapper.java b/src/main/java/com/api/jaebichuri/bid/mapper/AuctionBidMapper.java index a29a991..57ca905 100644 --- a/src/main/java/com/api/jaebichuri/bid/mapper/AuctionBidMapper.java +++ b/src/main/java/com/api/jaebichuri/bid/mapper/AuctionBidMapper.java @@ -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 bidDatePriceResponseList, List bidVolumeResponseList, Long memberId); @@ -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 top3BidDatePriceList, + List bidVolumeResponseList); @Mapping(target = "isSuccess", constant = "true") @Mapping(target = "code", expression = "java(getSuccessCode())") @@ -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() { diff --git a/src/main/java/com/api/jaebichuri/bid/service/AuctionBidService.java b/src/main/java/com/api/jaebichuri/bid/service/AuctionBidService.java index d186714..ed091d7 100644 --- a/src/main/java/com/api/jaebichuri/bid/service/AuctionBidService.java +++ b/src/main/java/com/api/jaebichuri/bid/service/AuctionBidService.java @@ -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; @@ -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(); @@ -106,13 +105,17 @@ public List getVolumeResponseList(Auction auction) { Collectors.counting() )); - List 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, @@ -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 volumeResponseList = getVolumeResponseList(auction); + List 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) { @@ -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 volumeResponseList = getVolumeResponseList(auction); + List 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) { @@ -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")); - } } \ No newline at end of file