package pl.koszela.jan.layer.facade.impl;

import org.springframework.beans.factory.annotation.Autowired;
import pl.koszela.jan.layer.facade.CartFacade;
import pl.koszela.jan.layer.facade.dto.CartDTO;
import pl.koszela.jan.layer.facade.mapper.Mapper;
import pl.koszela.jan.layer.facade.mapper.impl.Maps;
import pl.koszela.jan.layer.model.domain.impl.Order;
import pl.koszela.jan.layer.service.CartService;
import pl.koszela.jan.layer.service.OrderService;
import pl.koszela.jan.main.annotation.Facade;

/**
 * Created on 15.08.2017.
 *
 * @author Jan Koszela
 */
@Facade("cartFacade")
public class DefaultCartFacade implements CartFacade {

  private CartDTO cart;

  @Autowired
  private CartService cartService;

  @Autowired
  private OrderService orderService;

  @Override
  public CartDTO getCart() {

    if ((cart == null) ||
        (cart.hashCode() != cartService.getCart().hashCode())) {

      Mapper cartMapper = Maps.getMapper(CartDTO.class);
      cart = (CartDTO) cartMapper.map(cartService.getCart());
    }

    return cart;
  }

  @Override
  public boolean removeOrderFromCart(String product) {
    Order foundOrder = orderService.getOrderByName(product);
    return cartService.removeOrderFromCart(foundOrder);
  }

  @Override
  public boolean addOrderToCart(String product, String quantity) {

    Order foundOrder = orderService.getOrderByName(product);
    Order newOrder = null;

    if (isOrderFound(foundOrder)) {
      newOrder = orderService.createOrder(product, Integer.parseInt(quantity));
    } else {
      if (isNewQuantityDifferent(quantity, foundOrder)) {
        orderService.updateOrder(product, Integer.parseInt(quantity));
      } else {
        newOrder = foundOrder;
      }
    }

    return cartService.addToCart(newOrder);
  }

  private boolean isNewQuantityDifferent(String quantity, Order foundOrder) {
    return foundOrder.getQuantity() != Integer.parseInt(quantity);
  }

  private boolean isOrderFound(Order foundOrder) {
    return foundOrder == null;
  }
}

@@ -1374,35 +1374,8 @@
 ct);
-%0A    Order newOrder = null;
 %0A%0A  
@@ -1412,101 +1412,8 @@
 ) %7B%0A
-      newOrder = orderService.createOrder(product, Integer.parseInt(quantity));%0A    %7D else %7B%0A
     
@@ -1544,16 +1544,23 @@
 %0A      %7D
+%0A%0A    %7D
  else %7B%0A
@@ -1569,90 +1569,234 @@
     
-  newOrder = foundOrder;%0A      %7D%0A    %7D%0A%0A    return cartService.addToCart(newOrder)
+orderService.createOrder(product, Integer.valueOf(quantity));%0A    %7D%0A%0A    return cartService.addToCart(orderService.getOrderByName(product));%0A  %7D%0A%0A  private boolean isOrderFound(Order foundOrder) %7B%0A    return foundOrder != null
 ;%0A  
@@ -1952,92 +1952,6 @@
  %7D%0A%0A
-  private boolean isOrderFound(Order foundOrder) %7B%0A    return foundOrder == null;%0A  %7D%0A
 %7D%0A

Update cart Facade