1 | package mk.ukim.finki.busngo.web;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
4 | import mk.ukim.finki.busngo.model.enums.BILET_STATUS;
|
---|
5 | import mk.ukim.finki.busngo.model.exceptions.InvalidPatnikIdException;
|
---|
6 | import mk.ukim.finki.busngo.service.AuthService;
|
---|
7 | import mk.ukim.finki.busngo.service.BiletService;
|
---|
8 | import mk.ukim.finki.busngo.service.PatnikService;
|
---|
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;
|
---|
26 | private final PatnikService patnikService;
|
---|
27 |
|
---|
28 | public BiletController(BiletService biletService, TipBiletService tipBiletService, AuthService authService, PatnikService patnikService) {
|
---|
29 | this.biletService = biletService;
|
---|
30 | this.tipBiletService = tipBiletService;
|
---|
31 | this.authService = authService;
|
---|
32 | this.patnikService = patnikService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @GetMapping()
|
---|
36 | public String getAllBileti(@RequestParam(required = false) Long id,
|
---|
37 | @RequestParam(required = false) BILET_STATUS status,
|
---|
38 | Model model,
|
---|
39 | Authentication authentication){
|
---|
40 | model.addAttribute("bodyContent", "listBileti");
|
---|
41 | Patnik patnik = null;
|
---|
42 | try{
|
---|
43 | patnik = patnikService.loadUserByEmail(authentication.getName());
|
---|
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 |
|
---|
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 |
|
---|
69 | // @GetMapping("/encode")
|
---|
70 | // public String encode(){
|
---|
71 | // biletService.encode();
|
---|
72 | // return "redirect:/bilet";
|
---|
73 | // }
|
---|
74 |
|
---|
75 | }
|
---|