Lab07 order status fake
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¶
- Open the
MealOrderApiController
- Add another parameter to the auto-wired constructor that takes a
KitchenService
dependency and call itkitchenService
(just like you did for repository) - 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:
-
In the
mealOrder
method (the one with the@PostMapping
annotation), add this code after the order was saved in the repository:1. In thekitchenService.sendOrder(savedMealOrder);
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¶
- Open the
MealOrderResponse
JavaBean class and add the following properties:private String status; private Long orderNumber;
- Generate getters & setters for those two properties
Copy Info to Response¶
- Open the
MealOrderApiController
and replace theSystem.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¶
- Open the
MealOrder
class and add a new member variable:private Long orderNumber;
- Add a new method:
void updateOrderNumber(Long newOrderNumber)
that sets theorderNumber
to the incomingnewOrderNumber
- Add a new method:
Long orderNumber()
that returns theorderNumber
. - Open up the
KitchenService
and change thesendOrder
method to return aLong
instead ofvoid
- In the
FakeKitchenService
, change the return value forsendOrder
to also beLong
and, since it's a fake, justreturn 1L
Now let's put the order number into the meal order and then save it:
- Open up the
MealOrderApiController
class and in themealOrder
method change the line that calls the sendOrder to:Long orderNumber = kitchenService.sendOrder(savedMealOrder); savedMealOrder.updateOrderNumber(orderNumber); mealOrderRepository.save(mealOrder);
- In the
findMealOrder
method, now copy over theorderNumber
from theMealOrder
to theMealOrderResponse
Try it out¶
Using Postman or curl, do a few POST
s and then some GET
s to see what the order number and status are.