Skip to content

Commit

Permalink
Merge pull request #71 from PSR-Co/feat/#69-edit-order
Browse files Browse the repository at this point in the history
[feat] 요청 수정 API 생성
  • Loading branch information
psyeon1120 authored Aug 6, 2023
2 parents 5539f0b + 70771f7 commit 61c831a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {
INVALID_ORDER_TYPE(HttpStatus.BAD_REQUEST, "올바르지 않은 요청 타입입니다."),

// product
NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "해당 상품을 찾을 수 없습니다.");
NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "해당 상품을 찾을 수 없습니다."),
NULL_PRODUCT_ID(HttpStatus.BAD_REQUEST, "상품ID를 입력해주세요.");

val status: HttpStatus = status
val message: String = message
Expand Down
29 changes: 26 additions & 3 deletions src/main/kotlin/com/psr/psr/order/controller/OrderController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,44 @@ class OrderController(
) {
// 요청하기
@PostMapping
fun makeOrder(@AuthenticationPrincipal userAccount: UserAccount, @RequestBody @Valid orderReq: OrderReq): BaseResponse<Unit> {
fun makeOrder(
@AuthenticationPrincipal userAccount: UserAccount,
@RequestBody @Valid orderReq: OrderReq
): BaseResponse<Unit> {
if (orderReq.productId == null) return BaseResponse(BaseResponseCode.NULL_PRODUCT_ID)
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> {
fun getOrderDetail(
@AuthenticationPrincipal userAccount: UserAccount,
@PathVariable orderId: Long
): BaseResponse<OrderRes> {
return BaseResponse(orderService.getOrderDetail(userAccount.getUser(), orderId))
}

// 요청 목록 조회
@GetMapping
fun getOrderList(@AuthenticationPrincipal userAccount: UserAccount, type: String, status: String): BaseResponse<OrderListRes> {
fun getOrderList(
@AuthenticationPrincipal userAccount: UserAccount,
type: String,
status: String
): BaseResponse<OrderListRes> {
if (type !in listOf(SELL, ORDER)) return BaseResponse(BaseResponseCode.INVALID_ORDER_TYPE)
return BaseResponse(orderService.getOrderList(userAccount.getUser(), type, status))
}

// 요청 수정
@PatchMapping("/{orderId}")
fun editOrder(
@AuthenticationPrincipal userAccount: UserAccount,
@PathVariable orderId: Long,
@RequestBody(required = false) @Valid orderReq: OrderReq?,
@RequestParam(required = false) status: String?
): BaseResponse<Unit> {
if (orderReq != null && orderReq.websiteUrl.isNullOrBlank()) orderReq.websiteUrl = null
return BaseResponse(orderService.editOrder(userAccount.getUser(), orderReq, status, orderId))
}
}
4 changes: 1 addition & 3 deletions src/main/kotlin/com/psr/psr/order/dto/OrderReq.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.psr.psr.order.dto

import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size

data class OrderReq (
@field:NotNull(message = "상품ID를 입력해주세요.")
val productId: Long,
val productId: Long?,

@field:NotBlank(message = "이름을 입력해주세요.")
@field:Size(max = 100, message = "이름은 최대 100자입니다.")
Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/com/psr/psr/order/entity/Order.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.psr.psr.order.entity

import com.psr.psr.global.entity.BaseEntity
import com.psr.psr.order.dto.OrderReq
import com.psr.psr.product.entity.product.Product
import com.psr.psr.user.entity.User
import jakarta.persistence.*
Expand Down Expand Up @@ -39,4 +40,16 @@ data class Order(
@NotNull
var isReviewed: Boolean = false

) : BaseEntity()
) : BaseEntity() {
fun editOrder(orderReq: OrderReq?, orderStatus: OrderStatus?) {
if (orderReq != null) {
this.ordererName = orderReq.ordererName
this.websiteUrl = orderReq.websiteUrl
this.inquiry = orderReq.inquiry
this.description = orderReq.description
}
if (orderStatus != null) {
this.orderStatus = orderStatus
}
}
}
15 changes: 14 additions & 1 deletion src/main/kotlin/com/psr/psr/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OrderService(
) {
// 요청하기
fun makeOrder(user: User, orderReq: OrderReq) {
val product: Product = productRepository.findByIdAndStatus(orderReq.productId, ACTIVE_STATUS)
val product: Product = orderReq.productId?.let { productRepository.findByIdAndStatus(it, ACTIVE_STATUS) }
?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT)
orderRepository.save(orderAssembler.toEntity(user, orderReq, product))
}
Expand All @@ -48,4 +48,17 @@ class OrderService(

return orderAssembler.toListDto(orders)
}

// 요청 수정
fun editOrder(user: User, orderReq: OrderReq?, status: String?, orderId: Long) {
val order: Order = orderRepository.findByIdAndStatus(orderId, ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_ORDER)
if (order.user.id != user.id) throw BaseException(BaseResponseCode.NO_PERMISSION)

var orderStatus: OrderStatus? = null
if (status != null) orderStatus = OrderStatus.findByName(status)

order.editOrder(orderReq, orderStatus)
orderRepository.save(order)
}
}

0 comments on commit 61c831a

Please sign in to comment.