1POST /api/orders: Spring bind JSON vào PlaceOrderCommand và @Valid kiểm định dạng. OrderController chỉ một dòng checkout.placeOrder(cmd), không chứa quy tắc nghiệp vụ.POST /api/orders: Spring binds JSON into PlaceOrderCommand and @Valid checks its format. OrderController is one line, checkout.placeOrder(cmd), with no business rules.
2Proxy @Transactional mở transaction cho placeOrder. Service kiểm giỏ không rỗng (nếu rỗng ném EmptyCartException), rồi products.findById(1) tải Product trong cùng transaction. Trong CheckoutDemo, ProductRepository là InMemoryProductRepository nên không cần database.The @Transactional proxy starts a transaction for placeOrder. The service checks the cart is not empty (else EmptyCartException), then products.findById(1) loads the Product in the same transaction. In CheckoutDemo, ProductRepository is an InMemoryProductRepository, so no database is needed.
3Service gọi p.reserve(2): quy tắc không bán quá tồn kho nằm trong Product, còn 10 suất nên giảm còn 8. Sau đó new Order(customerId) và order.addLine(p, 2).The service calls p.reserve(2): the don't-oversell rule lives in Product; 10 meals remain, so stock drops to 8. Then new Order(customerId) and order.addLine(p, 2).
4orders.save(order) ghi đơn trong cùng transaction, rồi events.publishEvent(new OrderPlaced(id, total)) tách việc phụ ra listener. Nếu thông báo chỉ được gửi sau khi chốt đơn, listener phải chạy sau commit.orders.save(order) writes the order in the same transaction, then events.publishEvent(new OrderPlaced(id, total)) hands side work to listeners. If notifications must wait for a committed order, the listener must run after commit.
5Method kết thúc bình thường nên proxy COMMIT: INSERT đơn và UPDATE kho cùng được lưu. Service trả OrderView.from(order), controller trả 201.The method returns normally, so the proxy COMMITs: the order INSERT and stock UPDATE persist together. The service returns OrderView.from(order) and the controller responds 201.
6Nhánh lỗi: khách khác đặt 9 suất khi chỉ còn 8, p.reserve(9) ném OutOfStockException. Proxy ROLLBACK toàn bộ transaction, và @ExceptionHandler của controller dịch nó thành ProblemDetail 409; service không biết gì về HTTP.Error branch: another customer orders 9 meals with only 8 left, so p.reserve(9) throws OutOfStockException. The proxy ROLLs BACK the whole transaction, and the controller's @ExceptionHandler maps it to a 409 ProblemDetail; the service knows nothing about HTTP.
7GET /api/orders/1024 gọi use case khác: getOrder(id) với @Transactional(readOnly = true), orders.findById rồi map sang OrderView; không tìm thấy thì ném OrderNotFoundException.GET /api/orders/1024 calls a different use case: getOrder(id) with @Transactional(readOnly = true), orders.findById, then maps to OrderView; a missing order throws OrderNotFoundException.
8Bản Go: Handler.PlaceOrder giải mã placeOrderReq thành PlaceOrderCmd, gọi Service.PlaceOrder. Service tự BeginTx, truyền tx (thoả DBTX) vào inventory.Reserve và orders.Create, rồi Commit; defer tx.Rollback là lưới an toàn. ErrEmptyCart thành 422, ErrOutOfStock thành 409. main.go chạy thật bằng driver memdb (chỉ BEGIN/COMMIT/ROLLBACK) và memory.Inventory, memory.Orders, gọi handler qua httptest.Go: Handler.PlaceOrder decodes placeOrderReq into PlaceOrderCmd and calls Service.PlaceOrder. The service calls BeginTx itself, passes tx (a DBTX) into inventory.Reserve and orders.Create, then Commits; defer tx.Rollback is the safety net. ErrEmptyCart becomes 422, ErrOutOfStock 409. main.go runs it for real with the memdb driver (BEGIN/COMMIT/ROLLBACK only) plus memory.Inventory and memory.Orders, calling the handler through httptest.