Advertisement
ImHungryHi

ticket controller

Sep 17th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. package cc.serviceops.ticket;
  2. import cc.serviceops.Tools;
  3. import cc.serviceops.account.User;
  4. import cc.serviceops.account.dao.SignupService;
  5. import cc.serviceops.account.dao.UserService;
  6. import cc.serviceops.organisation.Organisation;
  7. import cc.serviceops.organisation.dao.OrganisationService;
  8. import cc.serviceops.organisation.Team;
  9. import cc.serviceops.ticket.dao.TicketService;
  10. import cc.serviceops.ticket.helpers.*;
  11. import lombok.AllArgsConstructor;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.validation.BindingResult;
  17. import org.springframework.web.bind.annotation.*;
  18. import org.springframework.web.server.ResponseStatusException;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.validation.Valid;
  22. import java.security.Principal;
  23. import java.time.LocalDate;
  24. import java.time.LocalDateTime;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27.  
  28. @Controller @AllArgsConstructor
  29. public class TicketController {
  30. private final UserService userService;
  31. private final OrganisationService organisationService;
  32. private final TicketService ticketService;
  33. private final SignupService signupService;
  34.  
  35. @GetMapping("/{organisation:^(?!css$|scripts$|img$|fonts$).*}/{ticket}")
  36. public String showTicket(@PathVariable("organisation") String organisationName,
  37. @PathVariable("ticket") String ticketId,
  38. Model model, Principal principal) {
  39. ...
  40. if (principal == null) {
  41. model.addAttribute("anonymousTicketDto", getAnonymousDtoFromTicket(ticket));
  42. return "ticket/anonymous";
  43. }
  44.  
  45. model.addAttribute("ticketDto", getDtoFromTicket(ticket));
  46. return "ticket/ticket";
  47. }
  48. ...
  49. @PostMapping("/{organisation:^(?!css$|scripts$|img$|fonts$).*}/anonymous")
  50. public String doTicket(@Valid @ModelAttribute AnonymousTicketDto anonymousTicketDto, BindingResult bindingResult,
  51. @PathVariable("organisation") String organisationName, Model model,
  52. HttpServletRequest request) {
  53. Organisation activeOrganisation = organisationService.getOrganisationByName(organisationName);
  54. User activeUser = userService.getByEmail(anonymousTicketDto.getCreatorEmail());
  55.  
  56. if (activeOrganisation == null) {
  57. throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Organisation cannot be found");
  58. }
  59.  
  60. if (anonymousTicketDto.getCategory() == null || anonymousTicketDto.getCategory().getId() < 1) {
  61. bindingResult.rejectValue("category.id", "ticket.category");
  62. }
  63.  
  64. if (activeUser != null) {
  65. bindingResult.rejectValue("creatorEmail", "user.email.duplicate");
  66. }
  67.  
  68. if (!anonymousTicketDto.getPassword().equals(anonymousTicketDto.getPasswordConfirmation()) &&
  69. !anonymousTicketDto.getPasswordConfirmation().isBlank()) {
  70. bindingResult.rejectValue("passwordConfirmation", "user.password.confirmation");
  71. }
  72.  
  73. if (bindingResult.hasErrors()) {
  74. fillTicketAttributes(activeOrganisation, model);
  75. model.addAttribute("anonymousTicketDto", anonymousTicketDto);
  76. return "ticket/anonymous";
  77. }
  78.  
  79. activeUser = new User() {{
  80. setEmail(anonymousTicketDto.getCreatorEmail());
  81. setPassword(anonymousTicketDto.getPassword());
  82. }};
  83.  
  84. activeUser = signupService.saveGuest(activeUser);
  85. // Password in activeUser is encrypted
  86. authWithHttpServletRequest(request, activeUser.getEmail(), anonymousTicketDto.getPassword());
  87. Ticket ticket = getTicketFromDto(anonymousTicketDto);
  88. ticket.setCreator(activeUser);
  89. ticket = ticketService.saveTicket(ticket);
  90. return String.format("redirect:/%s/%s?success", organisationName, ticket.getId());
  91. }
  92. ...
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement