1 | package mk.ukim.finki.busngo.web;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.Linija;
|
---|
4 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
5 | import mk.ukim.finki.busngo.model.entities.Vozenje;
|
---|
6 | import mk.ukim.finki.busngo.model.enums.VOZENJE_STATUS;
|
---|
7 | import mk.ukim.finki.busngo.model.exceptions.InvalidPatnikIdException;
|
---|
8 | import mk.ukim.finki.busngo.model.exceptions.InvalidPostojkaNaLinijaIdException;
|
---|
9 | import mk.ukim.finki.busngo.service.*;
|
---|
10 | import org.springframework.security.core.Authentication;
|
---|
11 | import org.springframework.stereotype.Controller;
|
---|
12 | import org.springframework.ui.Model;
|
---|
13 | import org.springframework.web.bind.annotation.*;
|
---|
14 |
|
---|
15 | @Controller
|
---|
16 | @RequestMapping("/vozenje")
|
---|
17 | public class VozenjeController {
|
---|
18 | private final VozenjeService vozenjeService;
|
---|
19 | private final PostojkaNaLinijaService postojkaNaLinijaService;
|
---|
20 | private final InstancaNaLinijaService instancaNaLinijaService;
|
---|
21 | private final BiletService biletService;
|
---|
22 |
|
---|
23 | public VozenjeController(VozenjeService vozenjeService, PostojkaNaLinijaService postojkaNaLinijaService, InstancaNaLinijaService instancaNaLinijaService, BiletService biletService) {
|
---|
24 | this.vozenjeService = vozenjeService;
|
---|
25 | this.postojkaNaLinijaService = postojkaNaLinijaService;
|
---|
26 | this.instancaNaLinijaService = instancaNaLinijaService;
|
---|
27 | this.biletService = biletService;
|
---|
28 | }
|
---|
29 |
|
---|
30 | @GetMapping()
|
---|
31 | public String getVozenjePage(Model model,
|
---|
32 | Authentication authentication,
|
---|
33 | @RequestParam(required = false) VOZENJE_STATUS status){
|
---|
34 | model.addAttribute("bodyContent", "listVozenja");
|
---|
35 | Patnik patnik = null;
|
---|
36 | try{
|
---|
37 | if(status != null)
|
---|
38 | model.addAttribute("vozenja", vozenjeService.findVozenjaByPatnikAndStatus(authentication.getName(), status));
|
---|
39 | else
|
---|
40 | model.addAttribute("vozenja", vozenjeService.findVozenjaByPatnikAndStatus(authentication.getName(), VOZENJE_STATUS.ACTIVE));
|
---|
41 | model.addAttribute("vozStatus", VOZENJE_STATUS.values());
|
---|
42 | }
|
---|
43 | catch (InvalidPatnikIdException e){
|
---|
44 | model.addAttribute("bodyContent", "listBileti");
|
---|
45 | model.addAttribute("hasError", true);
|
---|
46 | model.addAttribute("error", e.getMessage());
|
---|
47 | return "master-template";
|
---|
48 | }
|
---|
49 |
|
---|
50 | return "master-template";
|
---|
51 | }
|
---|
52 |
|
---|
53 | // @GetMapping("/start")
|
---|
54 | // public String getStartVozenjePagePostojkaPage(Model model){
|
---|
55 | // model.addAttribute("postojki", postojkaNaLinijaService.findAll());
|
---|
56 | // model.addAttribute("bodyContent", "choosePostojka");
|
---|
57 | //
|
---|
58 | // return "master-template";
|
---|
59 | // }
|
---|
60 | @GetMapping("/start")
|
---|
61 | public String getStartVozenjePageAll(@RequestParam(required = false) Long pnlId,
|
---|
62 | Model model,
|
---|
63 | Authentication authentication){
|
---|
64 | try{
|
---|
65 | if(pnlId != null){
|
---|
66 | model.addAttribute("postojka", postojkaNaLinijaService.findById(pnlId));
|
---|
67 | model.addAttribute("instanci", instancaNaLinijaService.findByPnlId(pnlId));
|
---|
68 | }
|
---|
69 | model.addAttribute("postojki", postojkaNaLinijaService.findAll());
|
---|
70 |
|
---|
71 | model.addAttribute("bileti", biletService.findAllByPatnikEmail(authentication.getName()));
|
---|
72 | model.addAttribute("bodyContent", "startVozenje");
|
---|
73 | }
|
---|
74 | catch (InvalidPostojkaNaLinijaIdException | InvalidPatnikIdException exception){
|
---|
75 | model.addAttribute("bodyContent", "listBileti");
|
---|
76 | model.addAttribute("hasError", true);
|
---|
77 | model.addAttribute("error", exception.getMessage());
|
---|
78 | return "master-template";
|
---|
79 | }
|
---|
80 |
|
---|
81 | return "master-template";
|
---|
82 | }
|
---|
83 |
|
---|
84 | @PostMapping("/start")
|
---|
85 | public String startVozenje(@RequestParam Long pnlId,
|
---|
86 | @RequestParam Long inlId,
|
---|
87 | @RequestParam Long bId,
|
---|
88 | Authentication authentication,
|
---|
89 | Model model){
|
---|
90 | try{
|
---|
91 | vozenjeService
|
---|
92 | .start(authentication.getName(), bId, pnlId, inlId);
|
---|
93 | }
|
---|
94 | catch (RuntimeException e){
|
---|
95 | System.out.println(e.getMessage());
|
---|
96 | model.addAttribute("bodyContent", "listBileti");
|
---|
97 | model.addAttribute("hasError", true);
|
---|
98 | model.addAttribute("error", e.getMessage());
|
---|
99 | return "master-template";
|
---|
100 | }
|
---|
101 |
|
---|
102 | return "redirect:/vozenje";
|
---|
103 | }
|
---|
104 |
|
---|
105 | @GetMapping("/{vozenjeId}/end")
|
---|
106 | public String getEndVozenje(@PathVariable Long vozenjeId,
|
---|
107 | Model model){
|
---|
108 | Vozenje vozenje = vozenjeService.findById(vozenjeId);
|
---|
109 | model.addAttribute("vozenje", vozenjeService.findById(vozenjeId));
|
---|
110 | model.addAttribute("pnls",
|
---|
111 | postojkaNaLinijaService
|
---|
112 | .findByLinijaAfterRedenBroj(Long.valueOf(vozenje
|
---|
113 | .getInstancanalinijaByInlId()
|
---|
114 | .getLinijaByLiId()
|
---|
115 | .getLiId()),
|
---|
116 | vozenje.getPostojkanalinijaByKacuvaPnlId().getPnlRedenBroj()));
|
---|
117 |
|
---|
118 | model.addAttribute("bodyContent", "endVozenje");
|
---|
119 |
|
---|
120 | return "master-template";
|
---|
121 | }
|
---|
122 |
|
---|
123 | @PostMapping("/{vozenjeId}/end")
|
---|
124 | public String endVozenje(@PathVariable Long vozenjeId,
|
---|
125 | @RequestParam Long pnlEndId){
|
---|
126 | Vozenje end = vozenjeService.end(vozenjeId, pnlEndId);
|
---|
127 | return "redirect:/vozenje";
|
---|
128 | }
|
---|
129 | }
|
---|