Skip to content

Commit

Permalink
Merge pull request #51 from lotteon2/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
PlayEnergy authored Jan 21, 2024
2 parents 9996069 + eff61a3 commit 6fc448a
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 37 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation 'software.amazon.awssdk:dynamodb:2.1.0'

implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation group: 'io.github.dailyon-maven', name: 'daily-on-common', version: '0.0.9'

implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.dailyon.auctionservice.controller;

import com.dailyon.auctionservice.dto.response.AuctionProductInfo;
import com.dailyon.auctionservice.facade.AuctionHistoryFacade;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@CrossOrigin("*")
@RestController
@RequiredArgsConstructor
@RequestMapping("/clients/auction-histories")
public class AuctionHistoryFeignApiController {
private final AuctionHistoryFacade auctionHistoryFacade;

@GetMapping("/{auctionId}")
public Mono<AuctionProductInfo> getAuctionHistory(
@RequestHeader("memberId") Long memberId, @PathVariable("auctionId") String auctionId) {
return auctionHistoryFacade.readAuctionHistoryAndProductDetail(memberId, auctionId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.dailyon.auctionservice.dto.response;

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuctionProductInfo {
private String productName;
private Integer stock;
private Integer price;
private String gender;
private String imgUrl;
private Long sizeId;
private String sizeName;
private boolean isWinner;
private Long orderPrice;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package com.dailyon.auctionservice.facade;

import com.dailyon.auctionservice.common.webclient.client.ProductClient;
import com.dailyon.auctionservice.document.AuctionHistory;
import com.dailyon.auctionservice.dto.response.AuctionProductInfo;
import com.dailyon.auctionservice.dto.response.ReadAuctionDetailResponse;
import com.dailyon.auctionservice.dto.response.ReadAuctionHistoryPageResponse;
import com.dailyon.auctionservice.service.AuctionHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
@RequiredArgsConstructor
public class AuctionHistoryFacade {
private final AuctionHistoryService auctionHistoryService;
private final AuctionHistoryService auctionHistoryService;
private final ProductClient productClient;

public ReadAuctionHistoryPageResponse readAuctionHistories(String memberId, Pageable pageable) {
return ReadAuctionHistoryPageResponse.of(auctionHistoryService.readAuctionHistories(memberId, pageable));
}
public ReadAuctionHistoryPageResponse readAuctionHistories(String memberId, Pageable pageable) {
return ReadAuctionHistoryPageResponse.of(
auctionHistoryService.readAuctionHistories(memberId, pageable));
}

public Mono<AuctionProductInfo> readAuctionHistoryAndProductDetail(
Long memberId, String auctionId) {
AuctionHistory auctionHistory =
auctionHistoryService.getAuctionHistory(String.valueOf(memberId), auctionId);
Mono<ReadAuctionDetailResponse.ReadProductDetailResponse> readProductDetailResponseMono =
productClient.readProductDetail(auctionHistory.getAuctionProductId());
return readProductDetailResponseMono.flatMap(
product -> {
AuctionProductInfo auctionProductInfo =
AuctionProductInfo.builder()
.productName(product.getName())
.stock(product.getProductStocks().get(0).getQuantity().intValue())
.price(product.getPrice())
.gender(product.getGender())
.imgUrl(product.getImgUrl())
.sizeId(product.getProductStocks().get(0).getProductSizeId())
.sizeName(product.getProductStocks().get(0).getProductSizeName())
.isWinner(auctionHistory.isWinner())
.orderPrice(auctionHistory.getAuctionWinnerBid())
.build();
return Mono.just(auctionProductInfo);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Mono<Long> createBid(CreateBidRequest request, String memberId) {
}

public Mono<Void> start(String auctionId) {
ChatPayload<Object> payload = ChatPayload.of(ChatCommand.START, null);
ChatPayload<Object> payload = ChatPayload.of(ChatCommand.START, "start");
return auctionService
.startAuction(auctionId)
.flatMap(
Expand All @@ -58,7 +58,7 @@ public Mono<Void> end(String auctionId) {
.endAuction(auctionId)
.flatMap(
auction -> {
ChatPayload<Object> payload = ChatPayload.of(ChatCommand.AUCTION_CLOSE, null);
ChatPayload<Object> payload = ChatPayload.of(ChatCommand.AUCTION_CLOSE, "end");
return chatHandler.broadCast(payload).then();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import org.springframework.data.repository.CrudRepository;

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

@EnableScan
@EnableScanCount
public interface AuctionHistoryRepository extends CrudRepository<AuctionHistory, String> {
List<AuctionHistory> findByMemberId(String memberId);
List<AuctionHistory> findByMemberId(String memberId);

Optional<AuctionHistory> findByAuctionIdAndMemberId(String AuctionId, String memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,35 @@
@Service
@RequiredArgsConstructor
public class AuctionHistoryService {
private final AuctionHistoryRepository auctionHistoryRepository;
private final AuctionHistoryRepository auctionHistoryRepository;

public Page<AuctionHistory> readAuctionHistories(String memberId, Pageable pageable) {
int currentPage = pageable.getPageNumber();
int pageSize = pageable.getPageSize();
public Page<AuctionHistory> readAuctionHistories(String memberId, Pageable pageable) {
int currentPage = pageable.getPageNumber();
int pageSize = pageable.getPageSize();

int startIdx = currentPage * pageSize;
int endIdx = startIdx + pageSize;
int startIdx = currentPage * pageSize;
int endIdx = startIdx + pageSize;

List<AuctionHistory> auctionHistories = auctionHistoryRepository.findByMemberId(memberId);
if(auctionHistories.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}
int totalSize = auctionHistories.size();

List<AuctionHistory> sorted = auctionHistories.stream()
.sorted(AuctionHistory::compareTo)
.collect(Collectors.toList())
.subList(startIdx, Math.min(endIdx, totalSize));

return new PageImpl<>(sorted, pageable, totalSize);
List<AuctionHistory> auctionHistories = auctionHistoryRepository.findByMemberId(memberId);
if (auctionHistories.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}
int totalSize = auctionHistories.size();

List<AuctionHistory> sorted =
auctionHistories.stream()
.sorted(AuctionHistory::compareTo)
.collect(Collectors.toList())
.subList(startIdx, Math.min(endIdx, totalSize));

return new PageImpl<>(sorted, pageable, totalSize);
}

public AuctionHistory getAuctionHistory(String memberId, String auctionId) {
AuctionHistory auctionHistory =
auctionHistoryRepository
.findByAuctionIdAndMemberId(auctionId, memberId)
.orElseThrow(() -> new RuntimeException("해당 경매 내역 정보가 존재하지 않습니다."));
return auctionHistory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,15 @@ public Mono<Auction> startAuction(String auctionId) {
.flatMap(
auction -> {
// 현재 시각이 경매 시작 시간과 같거나 이후이고, 아직 시작되지 않고, 끝나지 않은 경매라면 시작 가능
auction.setStarted(true);
return Mono.justOrEmpty(auctionRepository.save(auction));
// if((LocalDateTime.now().isEqual(auction.getStartAt()) ||
// LocalDateTime.now().isAfter(auction.getStartAt())) &&
// (!auction.isStarted() && !auction.isEnded())
// ) {
// auction.setStarted(true);
//
// } else {
// return Mono.error(new RuntimeException("시작 가능한 상태가 아닙니다"));
// }
if ((LocalDateTime.now().isEqual(auction.getStartAt())
|| LocalDateTime.now().isAfter(auction.getStartAt()))
&& (!auction.isStarted() && !auction.isEnded())) {
auction.setStarted(true);
return Mono.justOrEmpty(auctionRepository.save(auction));

} else {
return Mono.error(new RuntimeException("시작 가능한 상태가 아닙니다"));
}
});
}

Expand Down

0 comments on commit 6fc448a

Please sign in to comment.