[b101b69] | 1 | package mk.ukim.finki.busngo.web;
|
---|
| 2 |
|
---|
[bde8b13] | 3 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
[b101b69] | 4 | import mk.ukim.finki.busngo.model.enums.BILET_STATUS;
|
---|
[bde8b13] | 5 | import mk.ukim.finki.busngo.model.exceptions.InvalidPatnikIdException;
|
---|
[b101b69] | 6 | import mk.ukim.finki.busngo.service.AuthService;
|
---|
| 7 | import mk.ukim.finki.busngo.service.BiletService;
|
---|
[bde8b13] | 8 | import mk.ukim.finki.busngo.service.PatnikService;
|
---|
[b101b69] | 9 | import mk.ukim.finki.busngo.service.TipBiletService;
|
---|
| 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.GetMapping;
|
---|
| 14 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 15 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 16 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 17 |
|
---|
| 18 | import java.time.LocalDateTime;
|
---|
| 19 |
|
---|
| 20 | @Controller
|
---|
| 21 | @RequestMapping("/bilet")
|
---|
| 22 | public class BiletController {
|
---|
| 23 | private final BiletService biletService;
|
---|
| 24 | private final TipBiletService tipBiletService;
|
---|
| 25 | private final AuthService authService;
|
---|
[bde8b13] | 26 | private final PatnikService patnikService;
|
---|
[b101b69] | 27 |
|
---|
[bde8b13] | 28 | public BiletController(BiletService biletService, TipBiletService tipBiletService, AuthService authService, PatnikService patnikService) {
|
---|
[b101b69] | 29 | this.biletService = biletService;
|
---|
| 30 | this.tipBiletService = tipBiletService;
|
---|
| 31 | this.authService = authService;
|
---|
[bde8b13] | 32 | this.patnikService = patnikService;
|
---|
[b101b69] | 33 | }
|
---|
| 34 |
|
---|
| 35 | @GetMapping()
|
---|
| 36 | public String getAllBileti(@RequestParam(required = false) Long id,
|
---|
| 37 | @RequestParam(required = false) BILET_STATUS status,
|
---|
[bde8b13] | 38 | Model model,
|
---|
| 39 | Authentication authentication){
|
---|
[b101b69] | 40 | model.addAttribute("bodyContent", "listBileti");
|
---|
[bde8b13] | 41 | Patnik patnik = null;
|
---|
| 42 | try{
|
---|
[e272096] | 43 | patnik = patnikService.loadUserByEmail(authentication.getName());
|
---|
[bde8b13] | 44 | model.addAttribute("bileti", biletService.findAllByPatnikEmail(authentication.getName()));
|
---|
| 45 | }
|
---|
| 46 | catch (InvalidPatnikIdException e){
|
---|
| 47 | model.addAttribute("bodyContent", "listBileti");
|
---|
| 48 | model.addAttribute("hasError", true);
|
---|
| 49 | model.addAttribute("error", e.getMessage());
|
---|
| 50 | return "master-template";
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[b101b69] | 53 | return "master-template";
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @GetMapping("/kupi")
|
---|
| 57 | public String getKupiPage(Model model){
|
---|
| 58 | model.addAttribute("bodyContent", "kupiBilet");
|
---|
| 59 | model.addAttribute("tipbileti", tipBiletService.listAll());
|
---|
| 60 | return "master-template";
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @PostMapping("/kupi")
|
---|
| 64 | public String kupiBilet(@RequestParam Long tipbilet, Authentication authentication){
|
---|
| 65 | biletService.buy(tipbilet, LocalDateTime.now(), BILET_STATUS.INACTIVE, authentication.getName());
|
---|
| 66 | return "redirect:/bilet";
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[bde8b13] | 69 | // @GetMapping("/encode")
|
---|
| 70 | // public String encode(){
|
---|
| 71 | // biletService.encode();
|
---|
| 72 | // return "redirect:/bilet";
|
---|
| 73 | // }
|
---|
| 74 |
|
---|
[b101b69] | 75 | }
|
---|