Skip to content

Commit

Permalink
Merge pull request #57 from PSR-Co/feat/#54-get-order-detail
Browse files Browse the repository at this point in the history
[feat] 요청 상세 조회 API 생성
  • Loading branch information
psyeon1120 committed Aug 5, 2023
2 parents d3a76d0 + 9e09b55 commit 6dcae29
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {
INVALID_FAQ_TYPE_NAME(HttpStatus.NOT_FOUND, "올바르지 않은 FAQ 카테고리입니다. "),
NOT_FOUND_FAQ(HttpStatus.NOT_FOUND, "해당 FAQ를 찾을 수 없습니다."),


// inquiry
NOT_FOUND_INQUIRY(HttpStatus.NOT_FOUND, "해당 문의를 찾을 수 없습니다."),
INVALID_INQUIRY_STATUS(HttpStatus.BAD_REQUEST, "올바르지 않은 문의 상태입니다."),
INQUIRY_ANSWER_ALREADY_COMPLETE(HttpStatus.CONFLICT, "이미 답변 완료된 문의입니다."),

// order
NOT_FOUND_ORDER(HttpStatus.NOT_FOUND, "해당 요청을 찾을 수 없습니다."),

// product
NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "해당 상품을 찾을 수 없습니다.");

Expand Down
12 changes: 8 additions & 4 deletions src/main/kotlin/com/psr/psr/order/controller/OrderController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package com.psr.psr.order.controller
import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.jwt.UserAccount
import com.psr.psr.order.dto.OrderReq
import com.psr.psr.order.dto.OrderRes
import com.psr.psr.order.service.OrderService
import jakarta.validation.Valid
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/orders")
Expand All @@ -22,4 +20,10 @@ class OrderController(
if (orderReq.websiteUrl.isNullOrBlank()) orderReq.websiteUrl = null
return BaseResponse(orderService.makeOrder(userAccount.getUser(), orderReq))
}

// 요청 상세 조회
@GetMapping("/{orderId}")
fun getOrderDetail (@AuthenticationPrincipal userAccount: UserAccount, @PathVariable orderId: Long) : BaseResponse<OrderRes> {
return BaseResponse(orderService.getOrderDetail(userAccount.getUser(), orderId))
}
}
22 changes: 19 additions & 3 deletions src/main/kotlin/com/psr/psr/order/dto/OrderAssembler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import com.psr.psr.order.entity.Order
import com.psr.psr.product.entity.product.Product
import com.psr.psr.user.entity.User
import org.springframework.stereotype.Component
import java.time.format.DateTimeFormatter

@Component
class OrderAssembler {
fun toEntity(user: User, orderReq: OrderReq, product: Product): Order {
return Order(
user = user,
product = product,
ordererName = orderReq.ordererName!!,
ordererName = orderReq.ordererName,
websiteUrl = orderReq.websiteUrl,
inquiry = orderReq.inquiry!!,
description = orderReq.description!!
inquiry = orderReq.inquiry,
description = orderReq.description
)
}

fun toOrderResDTO(order: Order, isSeller: Boolean): OrderRes {
return OrderRes(
isSeller = isSeller,
status = order.orderStatus.value,
orderUserId = order.user.id!!,
orderDate = order.createdAt.format(DateTimeFormatter.ISO_DATE),
productId = order.product.id,
productName = order.product.name,
ordererName = order.ordererName,
websiteUrl = order.websiteUrl,
inquiry = order.inquiry,
description = order.description
)
}
}
14 changes: 14 additions & 0 deletions src/main/kotlin/com/psr/psr/order/dto/OrderRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.psr.psr.order.dto

data class OrderRes (
val isSeller: Boolean,
val status: String,
val orderUserId: Long,
val orderDate: String,
val productId: Long,
val productName: String,
val ordererName: String,
var websiteUrl: String? = null,
val inquiry: String,
val description: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository

@Repository
interface OrderRepository: JpaRepository<Order, Long> {
fun findByIdAndStatus(orderId: Long, status: String): Order?
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/psr/psr/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.order.dto.OrderAssembler
import com.psr.psr.order.dto.OrderReq
import com.psr.psr.order.dto.OrderRes
import com.psr.psr.order.entity.Order
import com.psr.psr.order.repository.OrderRepository
import com.psr.psr.product.entity.product.Product
import com.psr.psr.product.repository.product.ProductRepository
Expand All @@ -17,9 +19,19 @@ class OrderService(
private val productRepository: ProductRepository,
private val orderAssembler: OrderAssembler
) {
// 요청하기
fun makeOrder(user: User, orderReq: OrderReq) {
val product: Product = productRepository.findByIdAndStatus(orderReq.productId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT)
orderRepository.save(orderAssembler.toEntity(user, orderReq, product))
}

// 요청 상세 조회
fun getOrderDetail(user: User, orderId: Long): OrderRes {
val order: Order = orderRepository.findByIdAndStatus(orderId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_ORDER)
val isSeller = order.product.user.id == user.id
if (order.user.id != user.id && !isSeller) throw BaseException(BaseResponseCode.NO_PERMISSION)
return orderAssembler.toOrderResDTO(order, isSeller)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ import org.springframework.stereotype.Repository
interface ProductRepository: JpaRepository<Product, Long>, ProductCustom {
fun findAllByUserAndStatusOrderByCreatedAtDesc(user: User, activeStatus: String): List<Product>?
fun findByIdAndStatus(productId: Long, activeStatus: String): Product?


}

0 comments on commit 6dcae29

Please sign in to comment.