Skip to content

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.

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.

  1. Add a form to the coffee-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 with type="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 the name attribute, e.g.: <input type="hidden" name="id" th:value="${id}">
    • A submit button labeled "Send Order"
  2. In the CoffeeOrderWebController add a new Post-mapped method (called sendOrder) for /send-order that takes in the ID via the @ModelAttribute("id") annotation.

  3. Inside the method:

    • Do a System.out.println of the incoming ID
    • Return a redirect back to the order view page

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.

  1. Create a stub class that implements this interface (StubSendCoffeeOrderService):
    • For the send method, just do a System.out.println of the order's ID and name, and then return -1.
    • For the statusFor method, return "Brewing"
  2. Annotate this implementation with @Service
  3. Inject SendCoffeeOrderService into the web controller via the constructor
    • Make sure to hold on to the reference in a private member variable!
  4. In the CoffeeOrderWebController.sendOrder() method:
    • Retrieve the CoffeeOrder from the repository via the ID that came in
    • Call the sendCoffeeOrderService.send() method, passing in the CoffeeOrder from the repository
    • Continue to redirect to the order view page

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:

  1. Add a new member variable, orderNumber as a Long (not a long)
  2. Add a method, updateOrderNumberTo(long orderNumber) to set the order number
  3. 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:

  1. Take the return value from sendCoffeeOrderService.send() and update the order number on the coffeeOrder (via the updateOrderNumberTo method you wrote above)
  2. 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:

  1. Add to the CoffeeOrderResponse a member variable String orderStatus
  2. Add HTML to display this order status in the coffee-order-view.html template
  3. In the CoffeeOrderWebController.coffeeOrderView() method:
    • If the coffeeOrder.orderNumber() is null, set the response's orderStatus to "Not Sent"
    • Otherwise, call sendCoffeeOrderService.statusFor() with the orderNumber, and store the returned status in the response object.

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.