source: src/main/java/com/example/eatys_app/controller/NarackaController.java@ b3f2adb

Last change on this file since b3f2adb was b3f2adb, checked in by Aleksandar Siljanoski <acewow3@…>, 14 months ago

Adding project to repo

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package com.example.eatys_app.controller;
2
3import com.example.eatys_app.model.Korisnik;
4import com.example.eatys_app.model.Naracka;
5import com.example.eatys_app.service.NarackaService;
6import jakarta.servlet.http.HttpServletRequest;
7import org.springframework.security.core.Authentication;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.*;
11
12@Controller
13@RequestMapping("/shopping-cart")
14public 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}
Note: See TracBrowser for help on using the repository browser.