코딩 나도 할 수 있다!!
2024.06.13 (목) 주문 CRUD
혹시나도?
2024. 6. 13. 21:29
어제에 이어서 오늘은 주문에 대한 CRUD 작성을 하였다
너무 CRUD만 하는 거 아니냐?라고 의문이 들 수 있지만
기본을 탄탄하게 다져놔야 다음 단계를 넘어갈 때 쉽게 넘어갈 수 있다고 생각한다
먼저 Controller이다
@RestController
@RequestMapping("/products/{productId}/orders")
class OrderController(
private val orderService: OrderService,
) {
@GetMapping
fun getOrderList(): ResponseEntity<List<OrderResponse>> {
return ResponseEntity.ok(orderService.getOrderList())
}
@GetMapping("/{orderId}")
fun getOrder(
@PathVariable productId: Long,
@PathVariable orderId: Long
): ResponseEntity<OrderResponse> {
return ResponseEntity.ok(orderService.getOrder(productId, orderId))
}
@PostMapping
fun createOrder(
@PathVariable productId: Long,
): ResponseEntity<OrderResponse> {
val order: OrderResponse = orderService.createOrder(productId)
return ResponseEntity.ok(order)
}
@DeleteMapping("{orderId}")
fun deleteOrder(
@PathVariable productId: Long,
@PathVariable orderId: Long
): ResponseEntity<Unit> {
return ResponseEntity.ok(orderService.deleteOrder(productId,orderId))
}
}
업데이트가 왜 없을까?라고 생각했다면 답은 주문 수정하는 게 필요 없어서라고 답할 수 있다
우리는 온라인으로 게임을 판매하는 걸 만들기 때문에 게임을 계정당 1개 이상 구입할 필요가 없고
온라인 구매이기 때문에 배송지도 수정할 필요가 없다
그 외 것 들은 다른 CRUD와 유사하다 아니 똑같다고 볼 수 있다
다음은 Entitiy 부분이다
@Entity
@Table(name = "orders")
class Order(
@Column(name = "created_at")
var createdAt: LocalDateTime = LocalDateTime.now(),
@Column(name = "product_price")
var productPrice: Float,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id")
var product: Product
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null
}
fun Order.toOrderResponse(): OrderResponse = OrderResponse(
id = id!!,
createdAt = createdAt,
productPrice = productPrice
)
다음은 Servic 부분이다
@Service
class OrderService(
private val orderRepository: OrderRepository,
private val productRepository: ProductRepository
) {
fun getOrderList(): List<OrderResponse> {
return orderRepository.findAll().map { it.toOrderResponse() }
}
fun getOrder(productId: Long, orderId: Long): OrderResponse {
val result = orderRepository.findByProductIdAndId(productId, orderId)
?: throw RuntimeException("Product with ID $orderId not found")
return result.toOrderResponse()
}
@Transactional
fun createOrder(productId: Long): OrderResponse {
val product =
productRepository.findByIdOrNull(productId)
?: throw RuntimeException("Product with ID $productId not found")
val order = Order(
productPrice = product.price,
product = product
)
product.createOrder(order)
orderRepository.save(order)
return order.toOrderResponse()
}
@Transactional
fun deleteOrder(productId: Long, orderId: Long) {
val product = productRepository.findByIdOrNull(productId)
?: throw RuntimeException("Product with ID $productId not found")
val order = orderRepository.findByIdOrNull(orderId)
?: throw RuntimeException("Order with ID $orderId not found")
product.deleteOrder(order)
productRepository.save(product)
}
}
주문 조회, 생성, 삭제를 담당하는 메서드이다
1차적으로 내가 담당한 부분은 다 끝났다 내일은 다른 팀원이 했던 코드랑 병합하는 과정과 버그 수정, 기능 추가를 할 예정이다