Skip to content

Commit

Permalink
refactor: #139 경매 엔티티에 경매 상태를 계산하는 기능 추가 (#454)
Browse files Browse the repository at this point in the history
* feat: 경매 상태를 나타내는 enum 추가

* feat: Auction 도메인 엔티티에서 경매 상태를 반환하는 기능 추가

* refactor: 문자열로 경매 상태를 표현하던 방식을 enum으로 변경

* refactor: 문자열로 경매 상태를 표현하던 방식을 enum으로 변경
  • Loading branch information
apptie authored and swonny committed Oct 6, 2023
1 parent 743399a commit 3b1222b
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -83,7 +84,7 @@ public ReadAuctionDto readByAuctionId(final Long auctionId) {
"지정한 아이디에 대한 경매를 찾을 수 없습니다."
));

return ReadAuctionDto.from(findAuction);
return ReadAuctionDto.of(findAuction, LocalDateTime.now());
}

public ReadAuctionsDto readAllByCondition(
Expand All @@ -94,19 +95,19 @@ public ReadAuctionsDto readAllByCondition(
readAuctionSearchCondition
);

return ReadAuctionsDto.from(auctions);
return ReadAuctionsDto.of(auctions, LocalDateTime.now());
}

public ReadAuctionsDto readAllByUserId(final Long userId, final Pageable pageable) {
final Slice<Auction> auctions = auctionRepository.findAuctionsAllByUserId(userId, pageable);

return ReadAuctionsDto.from(auctions);
return ReadAuctionsDto.of(auctions, LocalDateTime.now());
}

public ReadAuctionsDto readAllByBidderId(final Long userId, final Pageable pageable) {
final Slice<Auction> auctions = auctionRepository.findAuctionsAllByBidderId(userId, pageable);

return ReadAuctionsDto.from(auctions);
return ReadAuctionsDto.of(auctions, LocalDateTime.now());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ddang.ddang.auction.application.dto;

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.auction.domain.AuctionStatus;
import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.image.application.util.ImageIdProcessor;
import com.ddang.ddang.image.domain.AuctionImage;
Expand All @@ -27,10 +28,11 @@ public record ReadAuctionDto(
Long sellerProfileId,
String sellerName,
double sellerReliability,
boolean isSellerDeleted
boolean isSellerDeleted,
AuctionStatus auctionStatus
) {

public static ReadAuctionDto from(final Auction auction) {
public static ReadAuctionDto of(final Auction auction, final LocalDateTime targetTime) {
return new ReadAuctionDto(
auction.getId(),
auction.getTitle(),
Expand All @@ -50,7 +52,8 @@ public static ReadAuctionDto from(final Auction auction) {
ImageIdProcessor.process(auction.getSeller().getProfileImage()),
auction.getSeller().getName(),
auction.getSeller().getReliability(),
auction.getSeller().isDeleted()
auction.getSeller().isDeleted(),
auction.findAuctionStatus(targetTime)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.ddang.ddang.auction.application.dto;

import com.ddang.ddang.auction.domain.Auction;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.domain.Slice;

public record ReadAuctionsDto(List<ReadAuctionDto> readAuctionDtos, boolean isLast) {

public static ReadAuctionsDto from(final Slice<Auction> auctions) {
public static ReadAuctionsDto of(final Slice<Auction> auctions, final LocalDateTime targetTime) {
final List<ReadAuctionDto> readAuctionDtos = auctions.getContent()
.stream()
.map(ReadAuctionDto::from)
.map(auction -> ReadAuctionDto.of(auction, targetTime))
.toList();

return new ReadAuctionsDto(readAuctionDtos, !auctions.hasNext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public void addAuctionImages(final List<AuctionImage> auctionImages) {
}
}

public AuctionStatus findAuctionStatus(final LocalDateTime targetTime) {
if (targetTime.isBefore(closingTime) && lastBid == null) {
return AuctionStatus.UNBIDDEN;
}
if (targetTime.isBefore(closingTime) && lastBid != null) {
return AuctionStatus.ONGOING;
}
if (targetTime.isAfter(closingTime) && lastBid == null) {
return AuctionStatus.FAILURE ;
}
return AuctionStatus.SUCCESS;
}

public boolean isOwner(final User user) {
return this.seller.equals(user);
}
Expand All @@ -132,6 +145,7 @@ public boolean isClosed(final LocalDateTime targetTime) {

public boolean isInvalidFirstBidPrice(final BidPrice bidPrice) {
final BidPrice startBidPrice = new BidPrice(startPrice.getValue());

return startBidPrice.isGreaterThan(bidPrice);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ddang.ddang.auction.domain;

public enum AuctionStatus {
UNBIDDEN,
ONGOING,
FAILURE,
SUCCESS
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static AuctionDetailResponse from(final ReadAuctionDto dto) {
dto.description(),
dto.startPrice(),
dto.lastBidPrice(),
processAuctionStatus(dto.closingTime(), dto.lastBidPrice()),
dto.auctionStatus().name(),
dto.bidUnit(),
dto.registerTime(),
dto.closingTime(),
Expand All @@ -69,18 +69,4 @@ private static List<DirectRegionResponse> convertDirectRegionsResponse(final Rea
.map(DirectRegionResponse::from)
.toList();
}

// TODO 2차 데모데이 이후 enum으로 처리
private static String processAuctionStatus(final LocalDateTime closingTime, final Integer lastBidPrice) {
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice == null) {
return "UNBIDDEN";
}
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice != null) {
return "ONGOING";
}
if (LocalDateTime.now().isAfter(closingTime) && lastBidPrice == null) {
return "FAILURE";
}
return "SUCCESS";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ddang.ddang.auction.presentation.dto.response;

import com.ddang.ddang.auction.application.dto.CreateInfoAuctionDto;
import com.ddang.ddang.auction.domain.AuctionStatus;
import com.ddang.ddang.image.presentation.util.ImageBaseUrl;
import com.ddang.ddang.image.presentation.util.ImageUrlCalculator;

Expand All @@ -19,8 +20,7 @@ public static CreateAuctionResponse from(final CreateInfoAuctionDto dto) {
dto.title(),
convertAuctionImageUrl(dto.auctionImageId()),
dto.startPrice(),
// TODO 2차 데모데이 이후 enum으로 처리
"UNBIDDEN",
AuctionStatus.UNBIDDEN.name(),
0
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.ddang.ddang.image.presentation.util.ImageBaseUrl;
import com.ddang.ddang.image.presentation.util.ImageUrlCalculator;

import java.time.LocalDateTime;

public record ReadAuctionResponse(
Long id,
String title,
Expand All @@ -21,7 +19,7 @@ public static ReadAuctionResponse from(final ReadAuctionDto dto) {
dto.title(),
convertImageUrl(dto.auctionImageIds().get(0)),
processAuctionPrice(dto.startPrice(), dto.lastBidPrice()),
processAuctionStatus(dto.closingTime(), dto.lastBidPrice()),
dto.auctionStatus().name(),
dto.auctioneerCount()
);
}
Expand All @@ -38,18 +36,4 @@ private static int processAuctionPrice(final Integer startPrice, final Integer l

return lastBidPrice;
}

// TODO 2차 데모데이 이후 enum으로 처리
private static String processAuctionStatus(final LocalDateTime closingTime, final Integer lastBidPrice) {
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice == null) {
return "UNBIDDEN";
}
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice != null) {
return "ONGOING";
}
if (LocalDateTime.now().isAfter(closingTime) && lastBidPrice == null) {
return "FAILURE";
}
return "SUCCESS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static AuctionDetailResponse from(final ReadAuctionDto dto) {
dto.description(),
dto.startPrice(),
dto.lastBidPrice(),
processAuctionStatus(dto.closingTime(), dto.lastBidPrice()),
dto.auctionStatus().name(),
dto.bidUnit(),
dto.registerTime(),
dto.closingTime(),
Expand All @@ -68,18 +68,4 @@ private static List<DirectRegionResponse> convertDirectRegionsResponse(final Rea
.map(DirectRegionResponse::from)
.toList();
}

// TODO 2차 데모데이 이후 enum으로 처리
private static String processAuctionStatus(final LocalDateTime closingTime, final Integer lastBidPrice) {
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice == null) {
return "UNBIDDEN";
}
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice != null) {
return "ONGOING";
}
if (LocalDateTime.now().isAfter(closingTime) && lastBidPrice == null) {
return "FAILURE";
}
return "SUCCESS";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ddang.ddang.user.presentation.dto.response;

import com.ddang.ddang.auction.application.dto.ReadAuctionDto;
import java.time.LocalDateTime;

public record ReadAuctionResponse(
Long id,
Expand All @@ -18,7 +17,7 @@ public static ReadAuctionResponse of(final ReadAuctionDto dto, final String base
dto.title(),
convertImageUrl(dto, baseUrl),
processAuctionPrice(dto.startPrice(), dto.lastBidPrice()),
processAuctionStatus(dto.closingTime(), dto.lastBidPrice()),
dto.auctionStatus().name(),
dto.auctioneerCount()
);
}
Expand All @@ -34,18 +33,4 @@ private static int processAuctionPrice(final Integer startPrice, final Integer l

return lastBidPrice;
}

// TODO 2차 데모데이 이후 enum으로 처리
private static String processAuctionStatus(final LocalDateTime closingTime, final Integer lastBidPrice) {
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice == null) {
return "UNBIDDEN";
}
if (LocalDateTime.now().isBefore(closingTime) && lastBidPrice != null) {
return "ONGOING";
}
if (LocalDateTime.now().isAfter(closingTime) && lastBidPrice == null) {
return "FAILURE";
}
return "SUCCESS";
}
}
Loading

0 comments on commit 3b1222b

Please sign in to comment.