diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java index 91cf332..67c0fd9 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java @@ -17,8 +17,8 @@ public class Appointment { new ArrayList<>(); */ // should now be useless - @OneToMany(mappedBy = "appointmentReport", fetch = FetchType.LAZY, orphanRemoval = true) - private final List listReport = new ArrayList<>(); + @OneToOne(mappedBy = "appointmentReport", fetch = FetchType.LAZY, orphanRemoval = true) + private Report report; @ManyToMany( fetch = FetchType.LAZY, @@ -109,4 +109,12 @@ public class Appointment { public void setAppointmentSubject(String appointmentSubject) { this.appointmentSubject = appointmentSubject; } + + public List getAppointmentListSectionCell() { + return listSectionCell; + } + + public Report getAppointmentReport() { + return report; + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Report.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Report.java index 410cd7c..5ff5e82 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Report.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Report.java @@ -17,7 +17,7 @@ public class Report { private String reportContent; - @ManyToOne(fetch = FetchType.LAZY) + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "idAppointment") private Appointment appointmentReport; @@ -28,6 +28,10 @@ public class Report { this.reportContent = reportContent; } + public Long getIdReport() { + return idReport; + } + public String getReportContent() { return reportContent; } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java index 71f5940..d77faf6 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java @@ -83,4 +83,8 @@ public class SectionCell { public Project getProjectSectionCell() { return projectSectionCell; } + + public List getAppointmentSectionCell() { + return appointment; + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java index 1fe9335..e5983e0 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java @@ -10,6 +10,9 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; +import java.util.ArrayList; +import java.util.List; + @Service public class SharedApiService { @@ -20,6 +23,7 @@ public class SharedApiService { private final ProjectService projectService; private final EntrepreneurService entrepreneurService; private final SectionCellService sectionCellService; + private final AppointmentService appointmentService; @Autowired SharedApiService( @@ -27,12 +31,14 @@ public class SharedApiService { UserService userService, ProjectService projectService, EntrepreneurService entrepreneurService, - SectionCellService sectionCellService) { + SectionCellService sectionCellService, + AppointmentService appointmentService) { this.administratorService = administratorService; this.userService = userService; this.projectService = projectService; this.entrepreneurService = entrepreneurService; this.sectionCellService = sectionCellService; + this.appointmentService = appointmentService; } // TODO: test @@ -103,12 +109,58 @@ public class SharedApiService { } // TODO - public Iterable getAppointmentsByProjectId(int projectId, String mail) { - throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); + public Iterable getAppointmentsByProjectId(long projectId, String mail) { + if (!isAllowedToCheckProject(mail, projectId)) { + logger.warn( + "User {} tried to check the appointments related to the project {} but is not allowed to.", + mail, + projectId); + throw new ResponseStatusException( + HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); + } + logger.info( + "User {} tried to check the appointments related to the project {}", + mail, + projectId); + Iterable sectionCells = + this.sectionCellService.getSectionCellsByProject( + projectService.getProjectById(projectId), + 2L); // sectionId useless in this function ? + List appointments = new ArrayList(); + sectionCells.forEach( + sectionCell -> { + appointments.addAll( + this.sectionCellService.getAppointmentsBySectionCellId( + sectionCell.getIdSectionCell())); + }); + return appointments; } // TODO - public void getPDFReport(int appointmentId, String mail) { + public void getPDFReport(long appointmentId, String mail) { + long projectId = + this.appointmentService + .getAppointmentById(appointmentId) + .getAppointmentListSectionCell() + .getFirst() + .getProjectSectionCell() + .getIdProject(); + if (!isAllowedToCheckProject(mail, projectId)) { + logger.warn( + "User {} tried to generate the PDF report {} related to the appointment {} but is not allowed to.", + mail, + this.appointmentService + .getAppointmentById(appointmentId) + .getAppointmentReport() + .getIdReport(), + appointmentId); + throw new ResponseStatusException( + HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); + } + /* return this.appointmentService + .getAppointmentById(appointmentId) + .getAppointmentReport().getReportContent(); */ + // generate pdf from this string, and format it to be decent looking throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java index f51f4d5..5a70bee 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java @@ -1,5 +1,6 @@ package enseirb.myinpulse.service.database; +import enseirb.myinpulse.model.Appointment; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.repository.SectionCellRepository; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; @Service @@ -77,4 +79,9 @@ public class SectionCellService { Project sectionProject = sectionCell.getProjectSectionCell(); return sectionProject.getIdProject(); } + + public List getAppointmentsBySectionCellId(Long sectionCellId) { + SectionCell sectionCell = getSectionCellById(sectionCellId); + return sectionCell.getAppointmentSectionCell(); + } }