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