1App của khách gửi JSON vào CreateOrderRequest: customerId và danh sách Item(sku, quantity). DTO không có price hay status để khách tự định giá hoặc tự xác nhận thanh toán.The customer's app sends JSON into CreateOrderRequest: a customerId and a list of Item(sku, quantity). The DTO has no price or status fields for clients to set their own price or confirm payment.
2Ở biên API, @Valid kiểm tra @NotNull customerId, @NotEmpty items và từng Item (@NotBlank sku, @Positive quantity); Go gọi req.Validate(). Đơn thiếu món bị trả 400 trước khi đụng service.At the API boundary, @Valid checks @NotNull customerId, @NotEmpty items and each Item (@NotBlank sku, @Positive quantity); Go calls req.Validate(). An order with no items gets a 400 before reaching the service.
3Nếu app gửi thêm "status":"PAID", handler Go dùng DisallowUnknownFields() nên Decode báo lỗi và trả 400; phía Jackson phải cấu hình rõ bỏ qua hay từ chối. Dù sao CreateOrderRequest cũng không có chỗ chứa status.If the app adds "status":"PAID", the Go handler uses DisallowUnknownFields(), so Decode fails and returns 400; Jackson must be configured explicitly to ignore or reject it. Either way CreateOrderRequest has no place to hold a status.
4Controller gọi checkout.placeOrder(req) (Go: h.svc.Place). Service kiểm tra quyền, lấy giá từ DB và tạo Order PENDING cùng các OrderLine. Entity giữ cả dữ liệu nội bộ như giá vốn và ghi chú vì nghiệp vụ cần chúng.The controller calls checkout.placeOrder(req) (Go: h.svc.Place). The service authorizes, looks up prices in the DB and creates a PENDING Order with its OrderLines. The entity keeps internal data such as cost and notes because business logic needs them.
5Controller gọi mapper.toResponse(saved); OrderMapperImpl (đúng code MapStruct sẽ sinh) chép totalAmount sang total; với mỗi OrderLine, toLine làm phẳng product.sku, product.name và unitPrice thành sku, productName, price. Nếu đọc quan hệ lazy, bước này phải chạy khi dữ liệu đã được tải.The controller calls mapper.toResponse(saved); OrderMapperImpl (exactly what MapStruct would generate) copies totalAmount to total; for each OrderLine, toLine flattens product.sku, product.name and unitPrice into sku, productName, price. If lazy relations are read, this must run while the data is loaded.
6Mapper tạo OrderResponse và các OrderResponse.Line (Go: ToResponse tạo []LineResponse bằng make(..., 0, n) để đơn rỗng ra [] chứ không null). Giá vốn và ghi chú nội bộ không có chỗ trong kiểu trả về.The mapper creates OrderResponse and its OrderResponse.Line items (Go: ToResponse builds []LineResponse with make(..., 0, n) so an empty order encodes [] rather than null). Cost and internal notes have no place in the response type.
7API serialize OrderResponse thành JSON và trả 201 (Go: Encode(ToResponse(o)), không bao giờ Encode(o)). Response không trỏ về entity nên serializer không lần theo quan hệ hay lấy thêm dữ liệu ngoài ý muốn.The API serializes OrderResponse to JSON and returns 201 (Go: Encode(ToResponse(o)), never Encode(o)). The response has no entity references, so serialization cannot wander through relations or fetch unintended data.
8Sau này nhóm đổi tên thuộc tính tổng tiền trong entity thì chỉ sửa một dòng trong OrderMapperImpl (với MapStruct: một @Mapping) và giữ trường total của DTO. Với MapStruct, ReportingPolicy.ERROR bắt trường đích chưa map ngay lúc compile, kiểm thử hợp đồng xác nhận JSON không đổi.Later, renaming the entity's total property only means updating one line in OrderMapperImpl (with MapStruct: one @Mapping) while the DTO keeps total. With MapStruct, ReportingPolicy.ERROR catches unmapped targets at compile time, and a contract test confirms the JSON is unchanged.