package com.evapps.Controller;

import com.evapps.Entity.Product;
import com.evapps.Service.CRUD.ReadDataService;
import com.evapps.Service.CRUD.UpdateDataService;
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.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashSet;

/**
 * Created by Eduardo veras on 27-Nov-16.
 */
@Controller
public class StoreFrontController {
    // Services
    @Autowired
    private ReadDataService RDS;
    @Autowired
    private UpdateDataService UDS;

    // Gets
    @GetMapping("/")
    public ModelAndView storeFront(Model model) {

        model.addAttribute("user", RDS.getCurrentLoggedUser());

        if(RDS.getCurrentLoggedUser() != null)
            model.addAttribute("shoppingCart", RDS.findRegisteredUserHistory(RDS.getCurrentLoggedUser().getEmail()).getShoppingCart());
        else
            model.addAttribute("shoppingCart", new HashSet<Product>()); // empty cart

        model.addAttribute("selection", RDS.findAllRegisteredProducts());

        return new ModelAndView("StoreFront/index");
    }

    @GetMapping("/account")
    public ModelAndView account(Model model){

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        return new ModelAndView("StoreFront/account");
    }

    @GetMapping("/cart")
    public ModelAndView cart(Model model){

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        return new ModelAndView("StoreFront/cart");
    }

    @GetMapping("/checkout")
    public ModelAndView checkout(Model model){

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        return new ModelAndView("StoreFront/checkout");
    }

    @GetMapping("/product")
    public ModelAndView productList(Model model){

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        return new ModelAndView("StoreFront/product");
    }

    @GetMapping("/product-detail/{id}")
    public ModelAndView product(Model model, @PathVariable("id") Integer productId) {

        if(!RDS.isUserLoggedIn())
            return new ModelAndView("redirect:/login");

        model.addAttribute("item", RDS.findRegisteredProduct(productId));

        return new ModelAndView("StoreFront/product-detail");
    }

    // Posts
}

@@ -25,16 +25,50 @@
 oller;%0A%0A
+import com.evapps.Entity.History;%0A
 import c
@@ -93,16 +93,16 @@
 roduct;%0A
-
 import c
@@ -461,16 +461,76 @@
 riable;%0A
+import org.springframework.web.bind.annotation.PostMapping;%0A
 import o
@@ -575,16 +575,57 @@
 dView;%0A%0A
+import javax.websocket.server.PathParam;%0A
 import j
@@ -641,16 +641,38 @@
 HashSet;
+%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -2148,16 +2148,17 @@
 /product
+s
 %22)%0A    p
@@ -2714,24 +2714,24 @@
 l%22);%0A    %7D%0A%0A
-
     // Posts
@@ -2731,10 +2731,1076 @@
 / Posts%0A
+    @PostMapping(%22/add_to_cart/%7BproductId%7D%22)%0A    public String addToCart(@PathParam(%22productId%22) Integer productId)%7B%0A%0A        if(!RDS.isUserLoggedIn())%0A            return %22redirect:/login%22;%0A%0A        try %7B%0A            History history = RDS.findRegisteredUserHistory(RDS.getCurrentLoggedUser().getEmail());%0A            Set%3CProduct%3E shoppingCart = history.getShoppingCart();%0A            Set%3CProduct%3E browsingHistory = history.getBrowsingHistory();%0A            Product product = RDS.findRegisteredProduct(productId);%0A%0A            // Adding to cart%0A            shoppingCart.add(product);%0A            history.setShoppingCart(shoppingCart);%0A%0A            // Updating the browsing history%0A            browsingHistory.add(product);%0A            history.setBrowsingHistory(browsingHistory);%0A%0A            UDS.updateRegisteredUserHistory(history);%0A%0A            return %22redirect:/%22; // TODO: this should go back to the origin - store page, or product detail%0A        %7D catch (Exception exp)%7B%0A            //%0A        %7D%0A%0A        return %22redirect:/%22; // TODO: Add error handling%0A    %7D%0A%0A
 %7D%0A

Add addToCart Post