Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 요청 상세 조회 API 생성 #57

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

크 예외처리 최💛

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?


}
Loading