NetanelBasal
6/10/2018 - 2:55 PM

cart.service.ts

@Injectable({
  providedIn: 'root'
})
export class CartService {
  constructor(private cartStore: CartStore, 
              private cartQuery: CartQuery) {}

  addProductToCart(productId: Product['id']) {
    const findItem = this.cartQuery.getEntity(productId);
    
    if (findItem) {
      return this.cartStore.updateQuantity(productId);
    }

    const item = createCartItem({
      productId
    });

    return this.cartStore.add(item);
  }

}