[b3f2adb] | 1 | package com.example.eatys_app.controller;
|
---|
| 2 |
|
---|
| 3 | import com.example.eatys_app.model.Korisnik;
|
---|
| 4 | import com.example.eatys_app.model.Naracka;
|
---|
| 5 | import com.example.eatys_app.service.NarackaService;
|
---|
| 6 | import jakarta.servlet.http.HttpServletRequest;
|
---|
| 7 | import org.springframework.security.core.Authentication;
|
---|
| 8 | import org.springframework.stereotype.Controller;
|
---|
| 9 | import org.springframework.ui.Model;
|
---|
| 10 | import org.springframework.web.bind.annotation.*;
|
---|
| 11 |
|
---|
| 12 | @Controller
|
---|
| 13 | @RequestMapping("/shopping-cart")
|
---|
| 14 | public class NarackaController {
|
---|
| 15 |
|
---|
| 16 | private final NarackaService narackaService;
|
---|
| 17 |
|
---|
| 18 | public NarackaController(NarackaService narackaService) {
|
---|
| 19 | this.narackaService = narackaService;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | @GetMapping
|
---|
| 23 | public String getShoppingCartPage(@RequestParam(required = false) String error,
|
---|
| 24 | HttpServletRequest req,
|
---|
| 25 | Model model) {
|
---|
| 26 | if (error != null && !error.isEmpty()) {
|
---|
| 27 | model.addAttribute("hasError", true);
|
---|
| 28 | model.addAttribute("error", error);
|
---|
| 29 | }
|
---|
| 30 | String username = req.getRemoteUser();
|
---|
| 31 | Naracka shoppingCart = this.narackaService.getActiveShoppingCart(username);
|
---|
| 32 | // double prices=this.narackaService.getPrice(shoppingCart.getId());
|
---|
| 33 | // model.addAttribute("amount", (int)prices * 100); // in cents
|
---|
| 34 | int prices=this.narackaService.Total(shoppingCart.getId());
|
---|
| 35 | model.addAttribute("obroci", this.narackaService.listAllObrociInShoppingCart(shoppingCart.getId()));
|
---|
| 36 | model.addAttribute("amount", prices);
|
---|
| 37 | return "shopping-cart.html";
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | @PostMapping("/add-obrok/{id}")
|
---|
| 41 | public String addObrokToShoppingCart(@PathVariable Integer id, HttpServletRequest req, Authentication authentication) {
|
---|
| 42 |
|
---|
| 43 | try {
|
---|
| 44 | String username = req.getRemoteUser();
|
---|
| 45 | this.narackaService.addObrokToShoppingCart(username, id);
|
---|
| 46 | return "redirect:/shopping-cart";
|
---|
| 47 | } catch (RuntimeException exception) {
|
---|
| 48 | return "redirect:/shopping-cart?error=" + exception.getMessage();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | }
|
---|