source: src/main/java/com/example/salonbella/controller/cart/CartController.java@ 4d7e387

Last change on this file since 4d7e387 was 4d7e387, checked in by makyjovanovsky <mjovanovski04@…>, 18 months ago

commit 1

  • Property mode set to 100644
File size: 1.4 KB
Line 
1package com.example.salonbella.controller.cart;
2
3import com.example.salonbella.service.ShoppingCartService;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.stereotype.Controller;
6import org.springframework.ui.Model;
7import org.springframework.web.bind.annotation.GetMapping;
8import org.springframework.web.bind.annotation.PostMapping;
9import org.springframework.web.bind.annotation.RequestParam;
10
11@Controller
12public class CartController {
13
14 private final ShoppingCartService shoppingCartService;
15
16 @Autowired
17 public CartController(ShoppingCartService shoppingCartService) {
18 this.shoppingCartService = shoppingCartService;
19 }
20
21 @PostMapping("/add-to-cart")
22 public String addProductToCart(@RequestParam(name = "id") String id) {
23 if(!shoppingCartService.existInCart(Long.parseLong(id))) {
24 shoppingCartService.addProduct(Long.parseLong(id));
25 }
26 return "redirect:/order";
27 }
28
29 @GetMapping("/my-cart")
30 public String getCartDetails(Model model) {
31 model.addAttribute("products", shoppingCartService.getShoppingCartDetails());
32 return "user-cart";
33 }
34
35 @GetMapping("/delete-product-from-cart")
36 public String deleteProductFromCart(@RequestParam(name = "id") String id) {
37 shoppingCartService.deleteProductFromCart(Long.parseLong(id));
38 return "redirect:/my-cart";
39 }
40
41}
Note: See TracBrowser for help on using the repository browser.