1 | package project.fmo.app.projcetfmo.Web;
|
---|
2 |
|
---|
3 | import org.springframework.stereotype.Controller;
|
---|
4 | import org.springframework.ui.Model;
|
---|
5 | import org.springframework.web.bind.annotation.*;
|
---|
6 | import project.fmo.app.projcetfmo.Model.*;
|
---|
7 | import project.fmo.app.projcetfmo.Service.*;
|
---|
8 |
|
---|
9 | import javax.servlet.http.HttpServletRequest;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | @Controller
|
---|
13 | @RequestMapping("/order")
|
---|
14 | public class NarackaController {
|
---|
15 |
|
---|
16 | private final NarackaService narackaService;
|
---|
17 | private final NarackaSodrziProduktService narackaSodrziProduktService;
|
---|
18 | private final KorisnikService korisnikService;
|
---|
19 | private final KupuvacService kupuvacService;
|
---|
20 | private final CenaService cenaService;
|
---|
21 |
|
---|
22 | private final ProduktService produktService;
|
---|
23 |
|
---|
24 | public NarackaController(NarackaService narackaService, NarackaSodrziProduktService narackaSodrziProduktService, KorisnikService korisnikService, KupuvacService kupuvacService, CenaService cenaService, ProduktService produktService) {
|
---|
25 | this.narackaService = narackaService;
|
---|
26 | this.narackaSodrziProduktService = narackaSodrziProduktService;
|
---|
27 | this.korisnikService = korisnikService;
|
---|
28 | this.kupuvacService = kupuvacService;
|
---|
29 | this.cenaService = cenaService;
|
---|
30 | this.produktService = produktService;
|
---|
31 | }
|
---|
32 |
|
---|
33 | @GetMapping
|
---|
34 | public String getShoppingCartPage(@RequestParam(required = false) String error,
|
---|
35 | HttpServletRequest req,
|
---|
36 | Model model) {
|
---|
37 | if (error != null && !error.isEmpty()) {
|
---|
38 | model.addAttribute("hasError", true);
|
---|
39 | model.addAttribute("error", error);
|
---|
40 | }
|
---|
41 | String username = req.getRemoteUser();
|
---|
42 | Korisnik korisnik = korisnikService.findbyImePrezime(username);
|
---|
43 | Kupuvac kupuvac = kupuvacService.findById(korisnik.getIdKorisnik());
|
---|
44 | Naracka naracka = this.narackaService.findByIdKorisniktAndStatus(kupuvac.getIdKorisnik(),"aktivna");
|
---|
45 | if(naracka == null){
|
---|
46 | model.addAttribute("bodyContent", "home");
|
---|
47 | return "master_template";
|
---|
48 | }
|
---|
49 | List<NarackaSodrziProdukt> narackaSodrziProdukt = narackaSodrziProduktService.findByIdNaracka(naracka.getIdNaracka());
|
---|
50 | List<Produkt> produktList = produktService.findAll();
|
---|
51 | model.addAttribute("order", naracka);
|
---|
52 | model.addAttribute("products", produktList);
|
---|
53 | model.addAttribute("po", narackaSodrziProdukt);
|
---|
54 | model.addAttribute("prices", cenaService.findAll());
|
---|
55 | model.addAttribute("bodyContent", "cart");
|
---|
56 | return "master_template";
|
---|
57 | }
|
---|
58 |
|
---|
59 | @PostMapping("/{id}/success")
|
---|
60 | public String processOrder(@PathVariable Integer id,HttpServletRequest req,@RequestParam(required = false) String address){
|
---|
61 | Naracka n = narackaService.findByIdNaracka(id);
|
---|
62 | String username = req.getRemoteUser();
|
---|
63 | Korisnik korisnik = korisnikService.findbyImePrezime(username);
|
---|
64 | Kupuvac kupuvac = kupuvacService.findById(korisnik.getIdKorisnik());
|
---|
65 | if(address!=null) {
|
---|
66 | narackaService.edit(0, "se sprema", address, kupuvac.getIdKorisnik(), n.getIdNaracka());
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | narackaService.edit(0, "se sprema", "/", kupuvac.getIdKorisnik(), n.getIdNaracka());
|
---|
70 | }
|
---|
71 | return "redirect:/products";
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | }
|
---|