-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor incident creation flow and dashboard links #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4d31260
782bc6a
a1348f1
41c7d32
274eb5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,33 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.example.team6backend.page; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.validation.Valid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.team6backend.incident.dto.IncidentRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.team6backend.incident.entity.Incident; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.team6backend.incident.service.IncidentService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.team6backend.security.CustomUserDetails; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.team6backend.user.entity.AppUser; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.team6backend.user.service.UserService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.data.domain.Page; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.data.domain.Pageable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.http.HttpStatus; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.security.access.prepost.PreAuthorize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.security.web.csrf.CsrfToken; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Controller; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.ui.Model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.validation.BindingResult; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.server.ResponseStatusException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Controller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class PageController { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final UserService userService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final IncidentService incidentService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public PageController(UserService userService) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public PageController(UserService userService, IncidentService incidentService) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.userService = userService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.incidentService = incidentService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -22,10 +36,26 @@ public String index() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/dashboard") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String dashboard(@AuthenticationPrincipal CustomUserDetails userDetails, Model model) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppUser user = userDetails.getUser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("user", user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("role", user.getRole().name()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String dashboard(@AuthenticationPrincipal CustomUserDetails userDetails, Model model, Pageable pageable) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userDetails != null && userDetails.getUser() != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppUser user = userDetails.getUser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("user", userDetails.getUser()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("role", user.getRole().name()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Page<Incident> incidents; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (user.getRole()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case RESIDENT -> incidents = incidentService.findByCreatedBy(user, pageable); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case HANDLER -> incidents = incidentService.findByAssignedTo(user, pageable); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case ADMIN -> incidents = incidentService.findAll(pageable); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default -> incidents = Page.empty(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("incidents", incidents); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("role", "PENDING"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "dashboard"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,18 +66,78 @@ public String incidents(@AuthenticationPrincipal CustomUserDetails userDetails, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/create-incident") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String createIncident(@AuthenticationPrincipal CustomUserDetails userDetails, Model model) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String createIncident(@AuthenticationPrincipal CustomUserDetails userDetails, Model model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HttpServletRequest request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppUser user = userDetails.getUser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String role = user.getRole().name(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CsrfToken csrf = (CsrfToken) request.getAttribute("_csrf"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("_csrf", csrf); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (role.equals("RESIDENT") || role.equals("ADMIN")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("role", role); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("user", user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("incidentRequest", new IncidentRequest()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "createincident"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "redirect:/dashboard"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @PreAuthorize("hasAnyRole('RESIDENT', 'ADMIN')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @PostMapping("/create-incident") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String submitIncident(@AuthenticationPrincipal CustomUserDetails userDetails, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Valid @ModelAttribute IncidentRequest incidentRequest, BindingResult bindingResult, Model model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HttpServletRequest request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppUser user = userDetails.getUser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String role = user.getRole().name(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bindingResult.hasErrors()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("role", role); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("user", user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CsrfToken csrf = (CsrfToken) request.getAttribute("_csrf"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("_csrf", csrf); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "createincident"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Incident incident = new Incident(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| incident.setSubject(incidentRequest.getSubject()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| incident.setDescription(incidentRequest.getDescription()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| incident.setIncidentCategory(incidentRequest.getIncidentCategory()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| incident.setCreatedBy(user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Incident saved = incidentService.createIncident(incident); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("success", "Incident created successfully!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("incidentRequest", incidentRequest); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "redirect:/incident/" + saved.getId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+110
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Model attributes won't survive the redirect. Lines 107-108 set 🔧 Proposed fix `@PostMapping`("/create-incident")
public String submitIncident(`@AuthenticationPrincipal` CustomUserDetails userDetails,
- `@Valid` `@ModelAttribute` IncidentRequest incidentRequest, BindingResult bindingResult, Model model) {
+ `@Valid` `@ModelAttribute` IncidentRequest incidentRequest, BindingResult bindingResult,
+ RedirectAttributes redirectAttributes, Model model) {
AppUser user = userDetails.getUser();
String role = user.getRole().name();
if (bindingResult.hasErrors()) {
model.addAttribute("role", role);
model.addAttribute("user", user);
return "createincident";
}
Incident incident = new Incident();
incident.setSubject(incidentRequest.getSubject());
incident.setDescription(incidentRequest.getDescription());
incident.setIncidentCategory(incidentRequest.getIncidentCategory());
incident.setCreatedBy(user);
Incident saved = incidentService.createIncident(incident);
- model.addAttribute("success", "Incident created successfully!");
- model.addAttribute("incidentRequest", incidentRequest);
+ redirectAttributes.addFlashAttribute("success", "Incident created successfully!");
return "redirect:/incident/" + saved.getId();
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/incident/{id}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String viewIncident(@PathVariable Long id, @AuthenticationPrincipal CustomUserDetails userDetails, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Model model) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Incident incident = incidentService.findById(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (incident == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "redirect:/dashboard"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppUser user = userDetails.getUser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean isAdmin = user.getRole().name().equals("ADMIN"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean isHandler = incident.getAssignedTo() != null && incident.getAssignedTo().getId().equals(user.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean isResident = incident.getCreatedBy().getId().equals(user.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null check for While 🛡️ Proposed fix- boolean isResident = incident.getCreatedBy().getId().equals(user.getId());
+ boolean isResident = incident.getCreatedBy() != null && incident.getCreatedBy().getId().equals(user.getId());📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isAdmin && !isHandler && !isResident) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ResponseStatusException(HttpStatus.FORBIDDEN); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.addAttribute("incident", incident); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "view-incident"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/profile") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String profile(@AuthenticationPrincipal CustomUserDetails userDetails, Model model) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppUser user = userDetails.getUser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.