Skip to content

Commit

Permalink
[FEAT] 경매 삭제, 경매 시작 로직 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
wakkpu committed Jan 20, 2024
1 parent 21d815c commit 24ee5b1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public Mono<ReadAuctionDetailResponse.ReadProductDetailResponse> readProductDeta
.bodyToMono(ReadAuctionDetailResponse.ReadProductDetailResponse.class);
}

public void deleteProducts(String memberId, String role, List<Long> 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<Void> 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<CreateProductResponse> createProduct(String memberId, String role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ public Mono<CreateAuctionResponse> createAuction(
}

@GetMapping("/auctions")
public Mono<ReadAuctionPageResponse> readAuctionsForAdmin(@RequestParam(name = "page") int page,
@RequestParam(name = "size") int size) {
public Mono<ReadAuctionPageResponse> readAuctionsForAdmin(
@RequestParam(name = "page") int page,
@RequestParam(name = "size") int size) {
return Mono.just(auctionFacade.readAuctionsForAdmin(PageRequest.of(page, size)));
}

@DeleteMapping("/auctions")
public Mono<Void> deleteAuction(
@RequestHeader(name = "memberId") String memberId,
@RequestHeader(name = "role") String role,
@RequestParam(name = "auctionId") String auctionId
) {
return auctionFacade.deleteAuction(memberId, role, auctionId);
}
}
31 changes: 17 additions & 14 deletions src/main/java/com/dailyon/auctionservice/facade/AuctionFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ public class AuctionFacade {
private final JwtUtil jwtUtil;

public Mono<CreateAuctionResponse> 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) {
Expand All @@ -54,8 +53,7 @@ public ReadAuctionPageResponse readCurrentAuctions(Pageable pageable) {
public ReadAuctionPageResponse readPastAuctions(Pageable pageable) {
return ReadAuctionPageResponse.of(auctionService.readPastAuctions(pageable));
}

// TODO : cache 달기

public Mono<ReadAuctionDetailResponse> readAuctionDetail(String auctionId) {
Mono<ReadAuctionDetailResponse.ReadAuctionResponse> auctionDetail =
Mono.just(auctionService.readAuctionDetail(auctionId));
Expand All @@ -69,9 +67,14 @@ public Mono<ReadAuctionDetailResponse> readAuctionDetail(String auctionId) {
});
}

public Mono<Void> 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<String, Object> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Auction, String> {
public interface AuctionRepository extends DynamoDBCrudRepository<Auction, String> {
List<Auction> findAll();
List<Auction> findAuctionsByStartedAndEnded(boolean started, boolean ended);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -148,15 +148,16 @@ public Mono<Auction> 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));
});
}

Expand All @@ -173,4 +174,12 @@ public Mono<Auction> 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);
}
}

This file was deleted.

0 comments on commit 24ee5b1

Please sign in to comment.