Skip to content

Commit

Permalink
feat : 중복 코드 정리 #12
Browse files Browse the repository at this point in the history
  • Loading branch information
soleu committed Oct 19, 2024
1 parent da70a11 commit f718a72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SellAuctionDetailDto> {
return ApiResponse.success(auctionService.getSoldAuctionDetail(userId.toLong(), id))
@GetMapping("{auctionId}/sold")
fun getSoldAuctionDetail(
@RequestParam userId: String,
@PathVariable auctionId: Long
): ApiResponse<SellAuctionDetailDto> {
return ApiResponse.success(auctionService.getSoldAuctionDetail(userId.toLong(), auctionId))
}

@GetMapping("{id}/bought")
Expand Down
30 changes: 19 additions & 11 deletions src/main/kotlin/com/get_offer/auction/service/AuctionService.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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<AuctionResult, Product> {
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 의 사용자는 존재하지 않습니다.") }
}
}

0 comments on commit f718a72

Please sign in to comment.