Line | |
---|
1 | package com.example.salonbella.controller.cart;
|
---|
2 |
|
---|
3 | import com.example.salonbella.service.ShoppingCartService;
|
---|
4 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | import org.springframework.ui.Model;
|
---|
7 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
8 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
9 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
10 |
|
---|
11 | @Controller
|
---|
12 | public 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.