From 24ee5b1fb2153c22864971b2f4fdb2bd75cbd5be Mon Sep 17 00:00:00 2001 From: wakkpu Date: Sat, 20 Jan 2024 23:56:04 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EA=B2=BD=EB=A7=A4=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C,=20=EA=B2=BD=EB=A7=A4=20=EC=8B=9C=EC=9E=91=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webclient/client/ProductClient.java | 10 +-- .../controller/AuctionAdminController.java | 14 +++- .../auctionservice/facade/AuctionFacade.java | 31 ++++---- .../repository/AuctionRepository.java | 5 +- .../service/AuctionService.java | 25 +++++-- .../service/AuctionServiceTests.java | 75 ------------------- 6 files changed, 54 insertions(+), 106 deletions(-) delete mode 100644 src/test/java/com/dailyon/auctionservice/service/AuctionServiceTests.java diff --git a/src/main/java/com/dailyon/auctionservice/common/webclient/client/ProductClient.java b/src/main/java/com/dailyon/auctionservice/common/webclient/client/ProductClient.java index 5c4c890..a76dec1 100644 --- a/src/main/java/com/dailyon/auctionservice/common/webclient/client/ProductClient.java +++ b/src/main/java/com/dailyon/auctionservice/common/webclient/client/ProductClient.java @@ -23,13 +23,13 @@ public Mono readProductDeta .bodyToMono(ReadAuctionDetailResponse.ReadProductDetailResponse.class); } - public void deleteProducts(String memberId, String role, List ids) { - String joinedIds = ids.stream().map(String::valueOf).collect(Collectors.joining(",")); - webClient.delete() - .uri(uriBuilder -> uriBuilder.path("/admin/products?ids={joinedIds}").build(joinedIds)) + public Mono deleteProducts(String memberId, String role, Long ids) { + return webClient.delete() + .uri(uriBuilder -> uriBuilder.path("/admin/products").queryParam("ids", ids).build()) .header("memberId", memberId) .header("role", role) - .retrieve(); + .retrieve() + .bodyToMono(Void.class); } public Mono createProduct(String memberId, String role, diff --git a/src/main/java/com/dailyon/auctionservice/controller/AuctionAdminController.java b/src/main/java/com/dailyon/auctionservice/controller/AuctionAdminController.java index 820ee3c..685108f 100644 --- a/src/main/java/com/dailyon/auctionservice/controller/AuctionAdminController.java +++ b/src/main/java/com/dailyon/auctionservice/controller/AuctionAdminController.java @@ -27,8 +27,18 @@ public Mono createAuction( } @GetMapping("/auctions") - public Mono readAuctionsForAdmin(@RequestParam(name = "page") int page, - @RequestParam(name = "size") int size) { + public Mono readAuctionsForAdmin( + @RequestParam(name = "page") int page, + @RequestParam(name = "size") int size) { return Mono.just(auctionFacade.readAuctionsForAdmin(PageRequest.of(page, size))); } + + @DeleteMapping("/auctions") + public Mono deleteAuction( + @RequestHeader(name = "memberId") String memberId, + @RequestHeader(name = "role") String role, + @RequestParam(name = "auctionId") String auctionId + ) { + return auctionFacade.deleteAuction(memberId, role, auctionId); + } } diff --git a/src/main/java/com/dailyon/auctionservice/facade/AuctionFacade.java b/src/main/java/com/dailyon/auctionservice/facade/AuctionFacade.java index 2a11f84..ac49c29 100644 --- a/src/main/java/com/dailyon/auctionservice/facade/AuctionFacade.java +++ b/src/main/java/com/dailyon/auctionservice/facade/AuctionFacade.java @@ -24,19 +24,18 @@ public class AuctionFacade { private final JwtUtil jwtUtil; public Mono createAuction( - String memberId, String role, CreateAuctionRequest createAuctionRequest) { + String memberId, String role, CreateAuctionRequest createAuctionRequest) { return productClient .createProduct(memberId, role, createAuctionRequest.getProductRequest()) - .flatMap( - response -> { - Auction auction = null; - try { + .flatMap(response -> { + Auction auction = null; + try { auction = auctionService.create(createAuctionRequest, response); - } catch (Exception e) { - productClient.deleteProducts(memberId, role, List.of(response.getProductId())); - } - return Mono.just(CreateAuctionResponse.create(auction, response)); - }); + } catch (Exception e) { + productClient.deleteProducts(memberId, role, response.getProductId()); + } + return Mono.just(CreateAuctionResponse.create(auction, response)); + }); } public ReadAuctionPageResponse readAuctionsForAdmin(Pageable pageable) { @@ -54,8 +53,7 @@ public ReadAuctionPageResponse readCurrentAuctions(Pageable pageable) { public ReadAuctionPageResponse readPastAuctions(Pageable pageable) { return ReadAuctionPageResponse.of(auctionService.readPastAuctions(pageable)); } - - // TODO : cache 달기 + public Mono readAuctionDetail(String auctionId) { Mono auctionDetail = Mono.just(auctionService.readAuctionDetail(auctionId)); @@ -69,9 +67,14 @@ public Mono readAuctionDetail(String auctionId) { }); } + public Mono deleteAuction(String memberId, String role, String auctionId) { + return Mono.just(auctionService.readAuction(auctionId)) + .flatMap(auction -> productClient.deleteProducts(memberId, role, auction.getAuctionProductId()) + .then(Mono.fromRunnable(() -> auctionService.delete(auction)))); + } + public String createToken(Long memberId) { Map claims = Map.of("memberId", memberId, "role", "ROLE_USER"); - String token = jwtUtil.generateToken(String.valueOf(memberId), claims); - return token; + return jwtUtil.generateToken(String.valueOf(memberId), claims); } } diff --git a/src/main/java/com/dailyon/auctionservice/repository/AuctionRepository.java b/src/main/java/com/dailyon/auctionservice/repository/AuctionRepository.java index 4cb6117..e35da60 100644 --- a/src/main/java/com/dailyon/auctionservice/repository/AuctionRepository.java +++ b/src/main/java/com/dailyon/auctionservice/repository/AuctionRepository.java @@ -1,18 +1,19 @@ package com.dailyon.auctionservice.repository; import com.dailyon.auctionservice.document.Auction; +import org.socialsignin.spring.data.dynamodb.repository.DynamoDBCrudRepository; import org.socialsignin.spring.data.dynamodb.repository.EnableScan; import org.socialsignin.spring.data.dynamodb.repository.EnableScanCount; -import org.springframework.data.repository.CrudRepository; import java.util.List; +import java.util.Optional; /* Caused by: com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Auction[created_at]; no HASH key for GSI auction_sort_idx https://stackoverflow.com/questions/68067091/sorting-not-supported-for-scan-expressions-and-no-hash-key-for-gsi-for-dynamodbp */ @EnableScan @EnableScanCount -public interface AuctionRepository extends CrudRepository { +public interface AuctionRepository extends DynamoDBCrudRepository { List findAll(); List findAuctionsByStartedAndEnded(boolean started, boolean ended); } diff --git a/src/main/java/com/dailyon/auctionservice/service/AuctionService.java b/src/main/java/com/dailyon/auctionservice/service/AuctionService.java index cdc7a1a..b0ac07d 100644 --- a/src/main/java/com/dailyon/auctionservice/service/AuctionService.java +++ b/src/main/java/com/dailyon/auctionservice/service/AuctionService.java @@ -132,7 +132,7 @@ public ReadAuctionResponse readAuctionDetail(String auctionId) { .findById(auctionId) .orElseThrow(() -> new RuntimeException("존재하지 않는 경매입니다")); if (auction.isEnded()) { - new RuntimeException(("이미 종료된 경매입니다.")); + throw new RuntimeException(("이미 종료된 경매입니다.")); } return ReadAuctionResponse.of(auction); } @@ -148,15 +148,16 @@ public Mono startAuction(String auctionId) { .switchIfEmpty(Mono.error(new RuntimeException("존재하지 않는 경매입니다"))) .flatMap( auction -> { - // 아직 시작 가능 시간 전이라면 시작 불가 - if (auction.getStartAt().isAfter(LocalDateTime.now()) - || auction.isStarted() - || auction.isEnded()) { + // 현재 시각이 경매 시작 시간과 같거나 이후이고, 아직 시작되지 않고, 끝나지 않은 경매라면 시작 가능 + 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("시작 가능한 상태가 아닙니다")); } - - auction.setStarted(true); - return Mono.justOrEmpty(auctionRepository.save(auction)); }); } @@ -173,4 +174,12 @@ public Mono endAuction(String auctionId) { return Mono.justOrEmpty(auctionRepository.save(auction)); }); } + + public void deleteAuction(String auctionId) { + auctionRepository.deleteById(auctionId); + } + + public void delete(Auction auction) { + auctionRepository.delete(auction); + } } diff --git a/src/test/java/com/dailyon/auctionservice/service/AuctionServiceTests.java b/src/test/java/com/dailyon/auctionservice/service/AuctionServiceTests.java deleted file mode 100644 index 77abbbc..0000000 --- a/src/test/java/com/dailyon/auctionservice/service/AuctionServiceTests.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.dailyon.auctionservice.service; - -import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync; -import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; -import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; -import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; -import com.amazonaws.services.dynamodbv2.util.TableUtils; -import com.dailyon.auctionservice.ContainerBaseTestSupport; -import com.dailyon.auctionservice.document.Auction; -import com.dailyon.auctionservice.repository.AuctionRepository; -import org.assertj.core.api.BDDAssertions; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; - -import static org.junit.jupiter.api.Assertions.*; - -import java.time.LocalDateTime; -import java.util.stream.IntStream; - -public class AuctionServiceTests extends ContainerBaseTestSupport { - @Autowired private AmazonDynamoDBAsync dynamoDB; - @Autowired private DynamoDBMapper dynamoDBMapper; - @Autowired private AuctionRepository auctionRepository; - @Autowired private AuctionService auctionService; - - @BeforeEach - void beforeEach() { - CreateTableRequest createTableRequest = dynamoDBMapper - .generateCreateTableRequest(Auction.class) - .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); - - TableUtils.createTableIfNotExists(dynamoDB, createTableRequest); - } - - @AfterEach - void afterEach() { - TableUtils.deleteTableIfExists( - dynamoDB, - dynamoDBMapper.generateDeleteTableRequest(Auction.class) - ); - } - - @Test - @DisplayName("경매 목록 생성 내림차순 기준 정렬 페이지네이션 조회") - void paginationTest() { - for(int i=0; i<10; i++) { - auctionRepository.save(Auction.builder() - .auctionProductId((long) i) - .auctionName("TEST_"+i) - .startBidPrice(1000) - .maximumWinner(5) - .startAt(LocalDateTime.now()) - .build() - ); - } - - Page auctions = auctionService.readAuctionsForAdmin(PageRequest.of(0, 5)); - assertEquals(10, auctions.getTotalElements()); - assertEquals(2, auctions.getTotalPages()); - assertEquals(5, auctions.getContent().size()); - IntStream.range(1, 5).forEach(i -> { - Auction prev = auctions.getContent().get(i-1); - Auction next = auctions.getContent().get(i); - - BDDAssertions - .then(prev.getCreatedAt().isAfter(next.getCreatedAt())) - .isTrue(); - }); - } -}