1 | package com.example.medweb.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.medweb.model.Doktor;
|
---|
5 | import com.example.medweb.model.Termin;
|
---|
6 | import com.example.medweb.model.exceptions.InvalidUserCredentialsException;
|
---|
7 | import com.example.medweb.service.DoktorService;
|
---|
8 | import com.example.medweb.service.TerminService;
|
---|
9 | import org.springframework.stereotype.Controller;
|
---|
10 | import org.springframework.ui.Model;
|
---|
11 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
12 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
13 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
14 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
15 |
|
---|
16 | import javax.servlet.http.HttpServletRequest;
|
---|
17 | import javax.transaction.Transactional;
|
---|
18 | import java.time.LocalDateTime;
|
---|
19 | import java.time.ZoneId;
|
---|
20 | import java.time.ZonedDateTime;
|
---|
21 | import java.time.format.DateTimeFormatter;
|
---|
22 | import java.util.List;
|
---|
23 |
|
---|
24 | @Controller
|
---|
25 | public class DoktorController {
|
---|
26 |
|
---|
27 | private final DoktorService doktorService;
|
---|
28 | private final TerminService terminService;
|
---|
29 |
|
---|
30 | public DoktorController(DoktorService doktorService, TerminService terminService) {
|
---|
31 | this.doktorService = doktorService;
|
---|
32 | this.terminService = terminService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | // prikazhi forma za login na doktor
|
---|
36 | @GetMapping("/login-doktor")
|
---|
37 | public String showLoginForm(Model model){
|
---|
38 | model.addAttribute("doktor", new Doktor());
|
---|
39 | model.addAttribute("bodyContent", new Doktor());
|
---|
40 | model.addAttribute("bodyContent", "login-doktor");
|
---|
41 | return "master-template";
|
---|
42 | }
|
---|
43 |
|
---|
44 | // najava na doktor i redirekcija do pochetna ako e uspeshen loginot
|
---|
45 | // najaveniot doktor se zachuvuva vo sesija
|
---|
46 | @PostMapping("/login-doktor")
|
---|
47 | public String loginDoktor (HttpServletRequest request, Model model) {
|
---|
48 | Doktor doktor = null;
|
---|
49 |
|
---|
50 | try {
|
---|
51 | doktor = this.doktorService.login(request.getParameter("email"),
|
---|
52 | request.getParameter("pass"));
|
---|
53 | request.getSession().setAttribute("doktor", doktor);
|
---|
54 | return "redirect:/home-doktor";
|
---|
55 | }
|
---|
56 | catch (InvalidUserCredentialsException exception) {
|
---|
57 | model.addAttribute("hasError", true);
|
---|
58 | model.addAttribute("error", exception.getMessage());
|
---|
59 | return "login-doktor";
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | // pochetna strana na doktor posle uspeshen login
|
---|
64 | @GetMapping("/home-doktor")
|
---|
65 | public String getDoktorHomePage (Model model) {
|
---|
66 | model.addAttribute("bodyContent", "home-doktor");
|
---|
67 | return "master-template";
|
---|
68 | }
|
---|
69 |
|
---|
70 | // pregled na site termini objaveni od najaveniot doktor (kopche moi termini)
|
---|
71 | @GetMapping("/moi-termini")
|
---|
72 | public String getTermini (HttpServletRequest request, Doktor doktor, Model model) {
|
---|
73 | doktor = (Doktor) request.getSession().getAttribute("doktor");
|
---|
74 | //List<Termin> terminList = doktor.getTerminList();
|
---|
75 | List<Termin> terminList = this.terminService
|
---|
76 | .findOnlyFutureAndFreeAndByDoktor(ZonedDateTime.now(), doktor.getCovek_id());
|
---|
77 | model.addAttribute("terminList", terminList);
|
---|
78 | model.addAttribute("bodyContent", terminList);
|
---|
79 | model.addAttribute("bodyContent", "doktor-termini");
|
---|
80 | return "master-template";
|
---|
81 | }
|
---|
82 |
|
---|
83 | // prikaz na templejt za vnes na nov termin
|
---|
84 | @GetMapping("/vnesi-termin/{id}")
|
---|
85 | public String getEditTerminPage (@PathVariable Integer id, Model model) {
|
---|
86 | Doktor doktor = this.doktorService.findById(id).get();
|
---|
87 | model.addAttribute("termin", new Termin());
|
---|
88 | model.addAttribute("bodyContent", new Termin());
|
---|
89 | model.addAttribute("bodyContent", "vnesi-nov-termin");
|
---|
90 | return "master-template";
|
---|
91 | }
|
---|
92 |
|
---|
93 | // posle vnesen datum i chas za terminot, istiot se vnesuva vo bazata
|
---|
94 | // prikaz na podatocite za uspeshno objaveniot termin
|
---|
95 | @PostMapping("/save-termin")
|
---|
96 | public String saveTermin (Model model, HttpServletRequest request,
|
---|
97 | @RequestParam(value = "vreme") String vreme){
|
---|
98 | Doktor doktor = (Doktor) request.getSession().getAttribute("doktor");
|
---|
99 | try {
|
---|
100 | Termin termin = new Termin();
|
---|
101 | termin.setDoktor_id(doktor.getCovek_id());
|
---|
102 | ZoneId zoneId = ZoneId.systemDefault();
|
---|
103 | ZonedDateTime vremeZ = LocalDateTime.parse(vreme, DateTimeFormatter.ISO_DATE_TIME).atZone(zoneId);
|
---|
104 | termin.setVreme(vremeZ);
|
---|
105 | model.addAttribute("termin", termin);
|
---|
106 | model.addAttribute("bodyContent", termin);
|
---|
107 | model.addAttribute("bodyContent", "nov-termin");
|
---|
108 | this.terminService.save(termin);
|
---|
109 | return "master-template";
|
---|
110 | } catch (RuntimeException exception) {
|
---|
111 | return "redirect:/moi-termini?error=" + exception.getMessage();
|
---|
112 | }
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | // brishenje na termin
|
---|
117 | @GetMapping("/termin/{id}/delete")
|
---|
118 | @Transactional
|
---|
119 | public String delete(@PathVariable Integer id) {
|
---|
120 | if (this.terminService.findOneTerminByTerminId(id) != null) {
|
---|
121 | Termin termin = this.terminService.findOneTerminByTerminId(id);
|
---|
122 | this.terminService.deleteTermin(termin);
|
---|
123 | return "redirect:/moi-termini";
|
---|
124 | }
|
---|
125 | else {
|
---|
126 | return "redirect:/home-doktor";
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|