Spring HATEOAS - separating order service
This commit is contained in:
parent
c6575d0bc5
commit
17a72d00a3
|
@ -4,6 +4,10 @@ import java.util.Map;
|
|||
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
public class Customer extends ResourceSupport {
|
||||
private String customerId;
|
||||
private String customerName;
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.List;
|
|||
import org.baeldung.persistence.model.Customer;
|
||||
import org.baeldung.persistence.model.Order;
|
||||
import org.baeldung.web.service.CustomerService;
|
||||
import org.baeldung.web.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
|
||||
|
@ -18,19 +19,22 @@ public class CustomerController {
|
|||
@Autowired
|
||||
private CustomerService customerService;
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
|
||||
public Customer getCustomerById(@PathVariable final String customerId) {
|
||||
return customerService.getCustomerDetail(customerId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET)
|
||||
public Order getOrderByIdForCustomer(@PathVariable final String customerId, @PathVariable final String orderId) {
|
||||
return customerService.getOrderByIdForCustomer(customerId, orderId);
|
||||
public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) {
|
||||
return orderService.getOrderByIdForCustomer(customerId, orderId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET)
|
||||
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
|
||||
return customerService.getAllOrdersForCustomer(customerId);
|
||||
return orderService.getAllOrdersForCustomer(customerId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/customers", method = RequestMethod.GET)
|
||||
|
@ -39,14 +43,11 @@ public class CustomerController {
|
|||
for (final Customer customer : allCustomers) {
|
||||
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
|
||||
customer.add(selfLink);
|
||||
if (customer.getOrders().values().size() > 0) {
|
||||
if (orderService.getAllOrdersForCustomer(customer.getCustomerId()).size() > 0) {
|
||||
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
|
||||
customer.add(ordersLink);
|
||||
}
|
||||
for (final Order order : customer.getOrders().values()) {
|
||||
final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order");
|
||||
order.add(orderLink);
|
||||
}
|
||||
|
||||
}
|
||||
return allCustomers;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package org.baeldung.web.service;
|
|||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import org.baeldung.persistence.model.Order;
|
||||
|
||||
public interface CustomerService {
|
||||
|
||||
|
@ -11,8 +10,4 @@ public interface CustomerService {
|
|||
|
||||
Customer getCustomerDetail(final String id);
|
||||
|
||||
List<Order> getAllOrdersForCustomer(String customerId);
|
||||
|
||||
Order getOrderByIdForCustomer(String customerId, String orderId);
|
||||
|
||||
}
|
||||
|
|
|
@ -5,37 +5,21 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import org.baeldung.persistence.model.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
||||
private HashMap<String, Customer> customerMap;
|
||||
private HashMap<String, Order> customerOneOrderMap;
|
||||
private HashMap<String, Order> customerTwoOrderMap;
|
||||
private HashMap<String, Order> customerThreeOrderMap;
|
||||
|
||||
public CustomerServiceImpl() {
|
||||
|
||||
customerMap = new HashMap<>();
|
||||
customerOneOrderMap = new HashMap<>();
|
||||
customerTwoOrderMap = new HashMap<>();
|
||||
customerThreeOrderMap = new HashMap<>();
|
||||
|
||||
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
|
||||
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
|
||||
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
|
||||
|
||||
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
|
||||
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
|
||||
|
||||
customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325));
|
||||
customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525));
|
||||
|
||||
customerOne.setOrders(customerOneOrderMap);
|
||||
customerTwo.setOrders(customerTwoOrderMap);
|
||||
customerThree.setOrders(customerThreeOrderMap);
|
||||
customerMap.put("10A", customerOne);
|
||||
customerMap.put("20B", customerTwo);
|
||||
customerMap.put("30C", customerThree);
|
||||
|
@ -52,23 +36,4 @@ public class CustomerServiceImpl implements CustomerService {
|
|||
return customerMap.get(customerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getAllOrdersForCustomer(final String customerId) {
|
||||
return new ArrayList<>(customerMap.get(customerId).getOrders().values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
|
||||
|
||||
final List<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values();
|
||||
Order selectedOrder = null;
|
||||
for (final Order order : orders) {
|
||||
if (order.getId().equals(orderId)) {
|
||||
selectedOrder = order;
|
||||
}
|
||||
}
|
||||
return selectedOrder;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Order;
|
||||
|
||||
public interface OrderService {
|
||||
|
||||
List<Order> getAllOrdersForCustomer(String customerId);
|
||||
|
||||
Order getOrderByIdForCustomer(String customerId, String orderId);
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import org.baeldung.persistence.model.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
private HashMap<String, Customer> customerMap;
|
||||
private HashMap<String, Order> customerOneOrderMap;
|
||||
private HashMap<String, Order> customerTwoOrderMap;
|
||||
private HashMap<String, Order> customerThreeOrderMap;
|
||||
|
||||
public OrderServiceImpl() {
|
||||
|
||||
customerMap = new HashMap<>();
|
||||
customerOneOrderMap = new HashMap<>();
|
||||
customerTwoOrderMap = new HashMap<>();
|
||||
customerThreeOrderMap = new HashMap<>();
|
||||
|
||||
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
|
||||
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
|
||||
|
||||
customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325));
|
||||
customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525));
|
||||
|
||||
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
|
||||
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
|
||||
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
|
||||
|
||||
customerOne.setOrders(customerOneOrderMap);
|
||||
customerTwo.setOrders(customerTwoOrderMap);
|
||||
customerThree.setOrders(customerThreeOrderMap);
|
||||
customerMap.put("10A", customerOne);
|
||||
customerMap.put("20B", customerTwo);
|
||||
customerMap.put("30C", customerThree);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getAllOrdersForCustomer(final String customerId) {
|
||||
return new ArrayList<>(customerMap.get(customerId).getOrders().values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
|
||||
|
||||
final List<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values();
|
||||
Order selectedOrder = null;
|
||||
for (final Order order : orders) {
|
||||
if (order.getId().equals(orderId)) {
|
||||
selectedOrder = order;
|
||||
}
|
||||
}
|
||||
return selectedOrder;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue