Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.example.team6backend.incident.dto.IncidentResponse;
import org.example.team6backend.incident.entity.Incident;
import org.example.team6backend.incident.service.IncidentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/incidents")
Expand All @@ -19,6 +21,7 @@ public IncidentController(IncidentService incidentService) {
this.incidentService = incidentService;
}

/**Create new incident*/
@PostMapping
public IncidentResponse createIncident(@Valid @RequestBody IncidentRequest incidentRequest) {
Incident incident = new Incident();
Expand All @@ -30,11 +33,25 @@ public IncidentResponse createIncident(@Valid @RequestBody IncidentRequest incid
return IncidentResponse.fromEntity(saved);
}

@GetMapping
public List<IncidentResponse> getAllIncidents() {
return incidentService.findAll()
.stream()
.map(IncidentResponse::fromEntity)
.toList();
/**Get my incidents(user)*/
@PreAuthorize("hasRole('RESIDENT')")
@GetMapping ("/myincident")
public Page<IncidentResponse> getMyIncidents(Pageable pageable){
return incidentService.findByCreatedBy(pageable)
.map(IncidentResponse::fromEntity);
}

@PreAuthorize("hasRole('HANDLER')")
@GetMapping ("/assigned")
public Page<IncidentResponse> getAssignedIncidents(Pageable pageable){
return incidentService.findByAssignedTo(pageable)
.map(IncidentResponse::fromEntity);
}

@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/admin")
public Page<IncidentResponse> getAllIncidents(Pageable pageable) {
return incidentService.findAll(pageable)
.map(IncidentResponse::fromEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,43 @@ public class Incident {
private String subject;
private String description;

@Column(name = "incident_category")
@Enumerated(EnumType.STRING)
private IncidentCategory incidentCategory;

@Column(name = "incident_status")
@Enumerated(EnumType.STRING)
private IncidentStatus incidentStatus;

@ManyToOne
@JoinColumn(name = "created_by_id")
private AppUser createdBy;

@ManyToOne
@JoinColumn(name = "modified_by_id")
private AppUser modifiedBy;

@ManyToOne
@JoinColumn(name = "assigned_to_id")
private AppUser assignedTo;

@Column(name = "created_at")
private LocalDateTime createdAt;

@Column(name = "updated_at")
private LocalDateTime updatedAt;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
updatedAt = LocalDateTime.now();
}

@PreUpdate
protected void onUpdate() {
updatedAt = LocalDateTime.now();
}

public Long getId() {
return id;
}
Expand Down Expand Up @@ -67,6 +87,10 @@ public LocalDateTime getCreatedAt() {
return createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public void setId(Long id) {
this.id = id;
}
Expand Down Expand Up @@ -102,4 +126,8 @@ public void setAssignedTo(AppUser assignedTo) {
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.example.team6backend.user.entity.AppUser;
import org.example.team6backend.incident.entity.Incident;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface IncidentRepository extends JpaRepository<Incident, Long> {

List<Incident> findByCreatedBy(AppUser user);
List <Incident> findByAssignedTo(AppUser user);
Page<Incident> findByCreatedBy(AppUser user, Pageable pageable);
Page<Incident> findByAssignedTo(AppUser user, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.example.team6backend.incident.entity.Incident;
import org.example.team6backend.incident.entity.IncidentStatus;
import org.example.team6backend.incident.repository.IncidentRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
Expand All @@ -21,28 +25,52 @@ public IncidentService(IncidentRepository incidentRepository) {
this.incidentRepository = incidentRepository;
}

/**Help-method for sorting**/
private Pageable withDefaultSort(Pageable pageable) {
if (pageable.isUnpaged() || pageable.getSort().isSorted()) {
return pageable;
}
return PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by(Sort.Direction.DESC, "id")
);
}

/**Create incident**/
public Incident createIncident(Incident incident){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails userDetails = (CustomUserDetails) auth.getPrincipal();
AppUser appUser = userDetails.getUser();

incident.setCreatedBy(appUser);

incident.setIncidentStatus(IncidentStatus.OPEN);
incident.setCreatedAt(LocalDateTime.now());
incident.setUpdatedAt(LocalDateTime.now());

return incidentRepository.save(incident);
}

public List<Incident> findByCreatedBy(AppUser user){
return incidentRepository.findByCreatedBy(user);
/**Find all incidents (Admin)**/
public Page <Incident> findAll(Pageable pageable){
return incidentRepository.findAll(withDefaultSort(pageable));
}

public List<Incident> findByAssignedTo(AppUser user){
return incidentRepository.findByAssignedTo(user);
/**Find your own incidents (user)**/
public Page<Incident> findByCreatedBy(Pageable pageable){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails userDetails = (CustomUserDetails) auth.getPrincipal();
AppUser user = userDetails.getUser();

return incidentRepository.findByCreatedBy(user, withDefaultSort(pageable));
}

public List <Incident> findAll(){
return incidentRepository.findAll();
/**Find assigned incidents per HANDLER**/
public Page<Incident> findByAssignedTo(Pageable pageable){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails userDetails = (CustomUserDetails) auth.getPrincipal();
AppUser user = userDetails.getUser();

return incidentRepository.findByAssignedTo(user, withDefaultSort (pageable));
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spring:
flyway:
enabled: true
locations: classpath:db/migration
baseline-on-migrate: true

security:
oauth2:
Expand Down