// Path: monolith/src/main/java/demo/model/Product.java
// public class Product implements Serializable {
// 
//   private static final long serialVersionUID = 5186013952828648626L;
// 
//   private String id;
//   private List<String> categories;
//   private String description;
//   private String name;
//   private String priceUsd;
//   private String picture;
// 
//   public Product() {
//     super();
//   }
// 
//   public String getId() {
//     return id;
//   }
// 
//   public List<String> getCategories() {
//     return categories;
//   }
// 
//   public String getDescription() {
//     return description;
//   }
// 
//   public String getName() {
//     return name;
//   }
// 
//   public String getPriceUsd() {
//     return priceUsd;
//   }
// 
//   public String getPicture() {
//     return picture;
//   }
// 
//   public void setId(String productId) {
//     this.id = productId;
//   }
// 
//   public void setCategories(List<String> productCategory) {
//     this.categories = productCategory;
//   }
// 
//   public void setDescription(String productDescription) {
//     this.description = productDescription;
//   }
// 
//   public void setName(String productName) {
//     this.name = productName;
//   }
// 
//   public void setPriceUsd(String productPrice) {
//     this.priceUsd = productPrice;
//   }
// 
//   public void setPicture(String productImage) {
//     this.picture = productImage;
//   }
// }
// 
// Path: microservices/src/main/java/demo/service/CartService.java
// @Service
// public class CartService {
// 
//   @Autowired private HttpSession session;
//   @Autowired private ProductService productService;
// 
//   public Cart getCart() {
//     return (Cart) Optional.ofNullable(session.getAttribute("cart")).orElseGet(Cart::new);
//   }
// 
//   // Empty cart and reset values in session
//   public Cart emptyCart() {
//     Cart cart = new Cart();
//     List<CartItem> items = new ArrayList<CartItem>();
//     cart.setCartItems(items);
//     return cart;
//   }
// 
//   /**
//    * Check if a product is already added to the cart
//    *
//    * @param id
//    * @param cart
//    */
//   public Optional<CartItem> exists(String id, List<CartItem> cart) {
//     return cart.stream().filter(c -> c.getProductId().equals(id)).findAny();
//   }
// 
//   /**
//    * Create list of products from the cart items. This is more of a workaround to cater to what's
//    * expected to show up in the ftl templates
//    *
//    * @param session
//    * @param cart
//    */
//   public List<Product> getSelectedProducts(HttpSession session, Cart cart) {
//     List<Product> selectedProducts = new ArrayList<>();
//     for (CartItem item : cart.getCartItems()) {
//       selectedProducts.add(productService.findProductById(item.getProductId()).get());
//     }
//     return selectedProducts;
//   }
// 
//   /**
//    * Create new cart item based on the user selection
//    *
//    * @param selectedProduct
//    * @param selectedQty
//    * @param cart
//    */
//   public CartItem newCartItem(String selectedProduct, int selectedQty, Cart cart) {
//     CartItem cartItem = new CartItem();
//     cartItem.setProductId(selectedProduct);
//     cartItem.setQuantity(selectedQty);
//     cartItem.setCart(cart);
//     return cartItem;
//   }
// }


import demo.model.Product;
import demo.service.CartService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
/* Copyright 2020 Google LLC. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package demo.controller;


// Controls the landing page
@Controller
@SessionAttributes(
    "cart_size") // Used by header.ftl that's common across all pages, hence cart_size used as a
// session attribute
public class HomeController {

  @Autowired private List<Product> productList;
  @Autowired private CartService cartService;

  @GetMapping(value = "/")
  public String home(Model model) {
    model.addAttribute("products", productList);
// Path: monolith/src/main/java/demo/model/Product.java
// public class Product implements Serializable {
// 
//   private static final long serialVersionUID = 5186013952828648626L;
// 
//   private String id;
//   private List<String> categories;
//   private String description;
//   private String name;
//   private String priceUsd;
//   private String picture;
// 
//   public Product() {
//     super();
//   }
// 
//   public String getId() {
//     return id;
//   }
// 
//   public List<String> getCategories() {
//     return categories;
//   }
// 
//   public String getDescription() {
//     return description;
//   }
// 
//   public String getName() {
//     return name;
//   }
// 
//   public String getPriceUsd() {
//     return priceUsd;
//   }
// 
//   public String getPicture() {
//     return picture;
//   }
// 
//   public void setId(String productId) {
//     this.id = productId;
//   }
// 
//   public void setCategories(List<String> productCategory) {
//     this.categories = productCategory;
//   }
// 
//   public void setDescription(String productDescription) {
//     this.description = productDescription;
//   }
// 
//   public void setName(String productName) {
//     this.name = productName;
//   }
// 
//   public void setPriceUsd(String productPrice) {
//     this.priceUsd = productPrice;
//   }
// 
//   public void setPicture(String productImage) {
//     this.picture = productImage;
//   }
// }
// 
// Path: microservices/src/main/java/demo/service/CartService.java
// @Service
// public class CartService {
// 
//   @Autowired private HttpSession session;
//   @Autowired private ProductService productService;
// 
//   public Cart getCart() {
//     return (Cart) Optional.ofNullable(session.getAttribute("cart")).orElseGet(Cart::new);
//   }
// 
//   // Empty cart and reset values in session
//   public Cart emptyCart() {
//     Cart cart = new Cart();
//     List<CartItem> items = new ArrayList<CartItem>();
//     cart.setCartItems(items);
//     return cart;
//   }
// 
//   /**
//    * Check if a product is already added to the cart
//    *
//    * @param id
//    * @param cart
//    */
//   public Optional<CartItem> exists(String id, List<CartItem> cart) {
//     return cart.stream().filter(c -> c.getProductId().equals(id)).findAny();
//   }
// 
//   /**
//    * Create list of products from the cart items. This is more of a workaround to cater to what's
//    * expected to show up in the ftl templates
//    *
//    * @param session
//    * @param cart
//    */
//   public List<Product> getSelectedProducts(HttpSession session, Cart cart) {
//     List<Product> selectedProducts = new ArrayList<>();
//     for (CartItem item : cart.getCartItems()) {
//       selectedProducts.add(productService.findProductById(item.getProductId()).get());
//     }
//     return selectedProducts;
//   }
// 
//   /**
//    * Create new cart item based on the user selection
//    *
//    * @param selectedProduct
//    * @param selectedQty
//    * @param cart
//    */
//   public CartItem newCartItem(String selectedProduct, int selectedQty, Cart cart) {
//     CartItem cartItem = new CartItem();
//     cartItem.setProductId(selectedProduct);
//     cartItem.setQuantity(selectedQty);
//     cartItem.setCart(cart);
//     return cartItem;
//   }
// }


// Path: microservices/src/main/java/demo/controller/HomeController.java
import demo.model.Product;
import demo.service.CartService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

/* Copyright 2020 Google LLC. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package demo.controller;


// Controls the landing page
@Controller
@SessionAttributes(
    "cart_size") // Used by header.ftl that's common across all pages, hence cart_size used as a
// session attribute
public class HomeController {

  @Autowired private List<Product> productList;
  @Autowired private CartService cartService;

  @GetMapping(value = "/")
  public String home(Model model) {
    model.addAttribute("products", productList);
    model.addAttribute("cart_size", cartService.getCart().getCartItems().size());