| 1 | package mk.ukim.finki.wp.db.controller;
|
|---|
| 2 |
|
|---|
| 3 | import lombok.RequiredArgsConstructor;
|
|---|
| 4 | import mk.ukim.finki.wp.db.service.SupportTicketService;
|
|---|
| 5 | import org.springframework.security.core.Authentication;
|
|---|
| 6 | import org.springframework.security.core.GrantedAuthority;
|
|---|
| 7 | import org.springframework.stereotype.Controller;
|
|---|
| 8 | import org.springframework.ui.Model;
|
|---|
| 9 | import org.springframework.web.bind.annotation.GetMapping;
|
|---|
| 10 | import org.springframework.web.bind.annotation.PathVariable;
|
|---|
| 11 | import org.springframework.web.bind.annotation.PostMapping;
|
|---|
| 12 | import org.springframework.web.bind.annotation.RequestParam;
|
|---|
| 13 |
|
|---|
| 14 | @Controller
|
|---|
| 15 | @RequiredArgsConstructor
|
|---|
| 16 | public class SupportTicketController {
|
|---|
| 17 |
|
|---|
| 18 | private final SupportTicketService supportTicketService;
|
|---|
| 19 |
|
|---|
| 20 | @GetMapping("/support-ticket")
|
|---|
| 21 | public String getSupportTicketPage(Model model, Authentication authentication) {
|
|---|
| 22 | Object principal = authentication.getPrincipal();
|
|---|
| 23 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 24 | model.addAttribute("tickets", supportTicketService.findAll(email));
|
|---|
| 25 |
|
|---|
| 26 | boolean loggedIn = authentication != null && authentication.isAuthenticated();
|
|---|
| 27 | model.addAttribute("loggedIn", loggedIn);
|
|---|
| 28 |
|
|---|
| 29 | String userRole = null;
|
|---|
| 30 |
|
|---|
| 31 | if (loggedIn) {
|
|---|
| 32 | userRole = authentication.getAuthorities().stream()
|
|---|
| 33 | .map(GrantedAuthority::getAuthority)
|
|---|
| 34 | .findFirst()
|
|---|
| 35 | .orElse(null);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | model.addAttribute("userRole", userRole);
|
|---|
| 39 | return "support_ticket/support_ticket";
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @GetMapping("/add/support-ticket")
|
|---|
| 43 | public String getAddSupportTicketPage() {
|
|---|
| 44 | return "support_ticket/support_ticket_form";
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | @PostMapping("/add/support-ticket")
|
|---|
| 48 | public String addSupportTicket(@RequestParam String subject,
|
|---|
| 49 | @RequestParam String description,
|
|---|
| 50 | Authentication authentication) {
|
|---|
| 51 | Object principal = authentication.getPrincipal();
|
|---|
| 52 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 53 | supportTicketService.addSupportTicket(email, subject, description);
|
|---|
| 54 | return "redirect:/support-ticket";
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | @PostMapping("/resolve/support-ticket/{id}")
|
|---|
| 58 | public String resolveSupportTicket(@PathVariable Integer id) {
|
|---|
| 59 | supportTicketService.resolveSupportTicket(id);
|
|---|
| 60 | return "redirect:/support-ticket";
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|