Lab16 send order fake
A. Show Full Coffee Order¶
Update the coffee order view page to show the full order, e.g., it should display the size, creamer, and sweetener options in addition to the name and price.
Thymeleaf Docs
Thymeleaf forms documentation: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#creating-a-form
Try out the page and make sure you see the full order.
Send Order to Display¶
The following process is typical when adding new external services.
B. Client Side Send Order¶
So far, the "Place Order" button only saved the order in the local repository. Now you're going to add a button to the order view page to actually send the order to the barista who will see the order on a kitchen display.
-
Add a
form
to thecoffee-order-view.html
page that will do a POST to/send-order
and has two fields:- A hidden field containing the order ID (this is just an
input
withtype="hidden"
), which you can do either by:- Create a one-property DTO and then use
th:object
as you've done before, or - Use
th:value
and manually setting thename
attribute, e.g.:<input type="hidden" name="id" th:value="${id}">
- Create a one-property DTO and then use
- A submit button labeled "Send Order"
- A hidden field containing the order ID (this is just an
-
In the
CoffeeOrderWebController
add a new Post-mapped method (calledsendOrder
) for/send-order
that takes in the ID via the@ModelAttribute("id")
annotation. -
Inside the method:
- Do a
System.out.println
of the incoming ID - Return a
redirect
back to the order view page
- Do a
Try it out: make sure it works by clicking on the Send Order
button, which should print the order number in the console.
C. Service to Send Order via API¶
In order to send the order, you'll need to send JSON to the remote service via a POST
.
As you did for the currency conversion service, you want to start with an interface defining the domain port that you'll use to send the order:
public interface SendCoffeeOrderService {
long send(CoffeeOrder coffeeOrder);
String statusFor(Long orderNumber);
}
Note: The long
return value represents the remote system's ID for this order.
- Create a stub class that implements this interface (
StubSendCoffeeOrderService
):- For the
send
method, just do aSystem.out.println
of the order'sID
andname
, and then return-1
. - For the
statusFor
method, return"Brewing"
- For the
- Annotate this implementation with
@Service
- Inject
SendCoffeeOrderService
into the web controller via the constructor- Make sure to hold on to the reference in a private member variable!
- In the
CoffeeOrderWebController.sendOrder()
method:- Retrieve the
CoffeeOrder
from the repository via the ID that came in - Call the
sendCoffeeOrderService.send()
method, passing in theCoffeeOrder
from the repository - Continue to redirect to the order view page
- Retrieve the
Try it out to make sure it works by clicking on the Send Order
button, which should print both the name and the order number in the console.
D. Update Order Status via Service¶
Now you want to show the status of the coffee order on the order view page, so do the following:
First, you'll need to keep track of the remote-system's order number, so:
In the CoffeeOrder
object:
- Add a new member variable,
orderNumber
as aLong
(not along
) - Add a method,
updateOrderNumberTo(long orderNumber)
to set the order number - Add a method,
orderNumber()
that returns the order number
Next, you'll store the order number returned by the send order service, so in the CoffeeOrderWebController.sendOrder()
method:
- Take the return value from
sendCoffeeOrderService.send()
and update the order number on thecoffeeOrder
(via theupdateOrderNumberTo
method you wrote above) - Use the repository to save this modified
coffeeOrder
in order to remember the order number for later
Finally, on the view order page, show the current status of the order, but only if the order was sent:
- Add to the
CoffeeOrderResponse
a member variableString orderStatus
- Add HTML to display this order status in the
coffee-order-view.html
template - In the
CoffeeOrderWebController.coffeeOrderView()
method:- If the
coffeeOrder.orderNumber()
isnull
, set the response'sorderStatus
to"Not Sent"
- Otherwise, call
sendCoffeeOrderService.statusFor()
with theorderNumber
, and store the returned status in the response object.
- If the
Try it out. You should see a status of "Not Sent" if you haven't sent the order yet.
Once you send the order, you should see a status of "Brewing".
Once you've completed the above steps,
check in with the instructor to review your code.