Skip to content

Lab 7 - Fake Kitchen Service

In this lab you'll create the KitchenService interface (the port) to interact with the Kitchen Kiosk Display. You'll start out requesting the status of an order, and in the next lab you'll actually send the order.

Create the Service Interface

Create a new interface called KitchenService in the domain package:

package com.welltestedlearning.mealkiosk.domain;

public interface KitchenService {
  void sendOrder(MealOrder mealOrder);
  String statusFor(Long orderNumber);
}

Fake an Implementation

Create a fake implementation of the KitchenService in the api package called FakeKitchenService:

package com.welltestedlearning.mealkiosk.api;

import com.welltestedlearning.mealkiosk.domain.KitchenService;
import com.welltestedlearning.mealkiosk.domain.MealOrder;
import org.springframework.stereotype.Service;

@Service
public class FakeKitchenService implements KitchenService {
  @Override
  public void sendOrder(MealOrder mealOrder) {
    System.out.println("Order sent:");
    mealOrder.display();
  }

  @Override
  public String statusFor(Long orderNumber) {
    return "COOKING";
  }
}

Inject Service into API Controller

  1. Open the MealOrderApiController
  2. Add another parameter to the auto-wired constructor that takes a KitchenService dependency and call it kitchenService (just like you did for repository)
  3. Run all the tests: you might find compile errors because you added a second parameter to the controller's constructor. Fix the tests to conform to the modified constructor.

Now you'll make use of the kitchen service:

  1. In the mealOrder method (the one with the @PostMapping annotation), add this code after the order was saved in the repository:

    kitchenService.sendOrder(savedMealOrder);
    
    1. In the findMealOrder method (the one with the @GetMapping), add this code right before returning the response:

    String status = kitchenService.statusFor(1L);
    System.out.println("Status: " + status);
    

Try it out

Try out accessing the POST and GET apis via Postman or curl.


Enrich MealOrderResponse With Status

  1. Open the MealOrderResponse JavaBean class and add the following properties:
    private String status;
    private Long orderNumber;
    
  2. Generate getters & setters for those two properties

Copy Info to Response

  1. Open the MealOrderApiController and replace the System.out.println("Status: " + status); code with putting the status into the meal order response.

Try it out

Now if you access the GET API for an existing order, you should see the status in the response.


Store the Order Number in the MealOrder

  1. Open the MealOrder class and add a new member variable:
    private Long orderNumber;
    
  2. Add a new method: void updateOrderNumber(Long newOrderNumber) that sets the orderNumber to the incoming newOrderNumber
  3. Add a new method: Long orderNumber() that returns the orderNumber.
  4. Open up the KitchenService and change the sendOrder method to return a Long instead of void
  5. In the FakeKitchenService, change the return value for sendOrder to also be Long and, since it's a fake, just return 1L

Now let's put the order number into the meal order and then save it:

  1. Open up the MealOrderApiController class and in the mealOrder method change the line that calls the sendOrder to:
    Long orderNumber = kitchenService.sendOrder(savedMealOrder);
    savedMealOrder.updateOrderNumber(orderNumber);
    mealOrderRepository.save(mealOrder);
    
  2. In the findMealOrder method, now copy over the orderNumber from the MealOrder to the MealOrderResponse

Try it out

Using Postman or curl, do a few POSTs and then some GETs to see what the order number and status are.