From f718a72e244dd218e38dd8aa626ce170ae8fefa1 Mon Sep 17 00:00:00 2001 From: pine_lee Date: Sat, 19 Oct 2024 23:15:55 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EC=A4=91=EB=B3=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=95=EB=A6=AC=20#12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auction/controller/AuctionController.kt | 9 ++++-- .../auction/service/AuctionService.kt | 30 ++++++++++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/get_offer/auction/controller/AuctionController.kt b/src/main/kotlin/com/get_offer/auction/controller/AuctionController.kt index 9526d07..9ab30a7 100644 --- a/src/main/kotlin/com/get_offer/auction/controller/AuctionController.kt +++ b/src/main/kotlin/com/get_offer/auction/controller/AuctionController.kt @@ -34,9 +34,12 @@ class AuctionController( return ApiResponse.success(auctionService.getBuyHistory(userId.toLong())) } - @GetMapping("{id}/sold") - fun getSoldAuctionDetail(@RequestParam userId: String, @PathVariable id: Long): ApiResponse { - return ApiResponse.success(auctionService.getSoldAuctionDetail(userId.toLong(), id)) + @GetMapping("{auctionId}/sold") + fun getSoldAuctionDetail( + @RequestParam userId: String, + @PathVariable auctionId: Long + ): ApiResponse { + return ApiResponse.success(auctionService.getSoldAuctionDetail(userId.toLong(), auctionId)) } @GetMapping("{id}/bought") diff --git a/src/main/kotlin/com/get_offer/auction/service/AuctionService.kt b/src/main/kotlin/com/get_offer/auction/service/AuctionService.kt index 65ed11f..313919c 100644 --- a/src/main/kotlin/com/get_offer/auction/service/AuctionService.kt +++ b/src/main/kotlin/com/get_offer/auction/service/AuctionService.kt @@ -1,9 +1,12 @@ package com.get_offer.auction.service import com.get_offer.auction.controller.repository.AuctionResultRepository +import com.get_offer.auction.domain.AuctionResult import com.get_offer.common.exception.NotFoundException import com.get_offer.common.exception.UnAuthorizationException +import com.get_offer.product.domain.Product import com.get_offer.product.repository.ProductRepository +import com.get_offer.user.domain.User import com.get_offer.user.repository.UserRepository import org.springframework.stereotype.Service @@ -31,30 +34,35 @@ class AuctionService( } fun getSoldAuctionDetail(userId: Long, auctionId: Long): SellAuctionDetailDto { - val auction = auctionRepository.findById(auctionId) - .orElseThrow { NotFoundException("$auctionId 의 경매 내역은 존재하지 않습니다.") } - - val product = productRepository.findById(auction.productId) - .orElseThrow { NotFoundException("${auction.productId} 의 상품은 존재하지 않습니다.") } + val (auction, product) = getAuctionAndProduct(auctionId) if (userId != product.writerId) throw UnAuthorizationException() - val buyer = userRepository.findById(auction.buyerId) - .orElseThrow { NotFoundException("${auction.buyerId} 의 사용자는 존재하지 않습니다.") } + val buyer = getUser(auction.buyerId) return SellAuctionDetailDto.of(product, buyer, auction) } fun getBoughtAuctionDetail(userId: Long, auctionId: Long): BuyAuctionDetailDto { + val (auction, product) = getAuctionAndProduct(auctionId) + if (userId != auction.buyerId) throw UnAuthorizationException() + + val seller = getUser(product.writerId) + + return BuyAuctionDetailDto.of(product, seller, auction) + } + + private fun getAuctionAndProduct(auctionId: Long): Pair { val auction = auctionRepository.findById(auctionId) .orElseThrow { NotFoundException("$auctionId 의 경매 내역은 존재하지 않습니다.") } - if (userId != auction.buyerId) throw UnAuthorizationException() val product = productRepository.findById(auction.productId) .orElseThrow { NotFoundException("${auction.productId} 의 상품은 존재하지 않습니다.") } - val seller = userRepository.findById(product.writerId) - .orElseThrow { NotFoundException("${product.writerId} 의 사용자는 존재하지 않습니다.") } + return Pair(auction, product) + } - return BuyAuctionDetailDto.of(product, seller, auction) + private fun getUser(userId: Long): User { + return userRepository.findById(userId) + .orElseThrow { NotFoundException("$userId 의 사용자는 존재하지 않습니다.") } } } \ No newline at end of file