From 3c61fdca93e0a80100826fa8099279a63ebd1cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Wed, 19 Mar 2025 12:05:56 +0100 Subject: [PATCH] feat: finished implementing apiService functions --- .../myinpulse/controller/SharedApi.java | 10 +++-- .../myinpulse/service/AdminApiService.java | 44 +++++++++++++++---- .../myinpulse/service/SharedApiService.java | 29 +++++++++--- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/SharedApi.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/SharedApi.java index 519ee41..a0b63e3 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/SharedApi.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/SharedApi.java @@ -1,6 +1,7 @@ package enseirb.myinpulse.controller; import com.itextpdf.text.DocumentException; + import enseirb.myinpulse.model.*; import enseirb.myinpulse.service.SharedApiService; @@ -10,7 +11,8 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.web.bind.annotation.*; -import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URISyntaxException; @SpringBootApplication @RestController @@ -83,10 +85,12 @@ public class SharedApi { @PathVariable int appointmentId, @AuthenticationPrincipal Jwt principal) { try { sharedApiService.getPDFReport(appointmentId, principal.getClaimAsString("email")); - } catch (FileNotFoundException e) { - System.out.println(e + "File not found"); } catch (DocumentException e) { System.out.println(e + "Document exception"); + } catch (URISyntaxException e) { + System.out.println(e + "Error with URI"); + } catch (IOException e) { + System.out.println(e + "Failed to access file"); } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java index d6efc7d..629aaf1 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java @@ -4,11 +4,7 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.ACTIVE; import static enseirb.myinpulse.model.ProjectDecisionValue.REJECTED; import enseirb.myinpulse.model.*; -import enseirb.myinpulse.service.database.AdministratorService; -import enseirb.myinpulse.service.database.AppointmentService; -import enseirb.myinpulse.service.database.ProjectService; -import enseirb.myinpulse.service.database.ReportService; -import enseirb.myinpulse.service.database.UserService; +import enseirb.myinpulse.service.database.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -17,6 +13,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 AdminApiService { @@ -28,6 +27,7 @@ public class AdminApiService { private final UtilsService utilsService; private final AppointmentService appointmentService; private final ReportService reportService; + private final SectionCellService sectionCellService; @Autowired AdminApiService( @@ -36,13 +36,15 @@ public class AdminApiService { AdministratorService administratorService, UtilsService utilsService, AppointmentService appointmentService, - ReportService reportService) { + ReportService reportService, + SectionCellService sectionCellService) { this.projectService = projectService; this.userService = userService; this.administratorService = administratorService; this.utilsService = utilsService; this.appointmentService = appointmentService; this.reportService = reportService; + this.sectionCellService = sectionCellService; } // TODO: check if tests are sufficient - peer verification required @@ -52,10 +54,36 @@ public class AdminApiService { this.userService.getUserByEmail(mail).getIdUser())); } - // TODO public Iterable getUpcomingAppointments(String mail) { logger.info("User {} check their upcoming appointments", mail); - throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); + User user = this.userService.getUserByEmail(mail); + List appointments = new ArrayList<>(); + if (user instanceof Administrator) { + List projects = new ArrayList<>(((Administrator) user).getListProject()); + projects.forEach( + project -> { + project.getListSectionCell() + .forEach( + sectionCell -> { + appointments.addAll( + this.sectionCellService + .getAppointmentsBySectionCellId( + sectionCell + .getIdSectionCell())); + }); + }); + } + if (user instanceof Entrepreneur) { + Project project = ((Entrepreneur) user).getProjectParticipation(); + project.getListSectionCell() + .forEach( + sectionCell -> { + appointments.addAll( + this.sectionCellService.getAppointmentsBySectionCellId( + sectionCell.getIdSectionCell())); + }); + } + return appointments; } // TODO: check if tests are sufficient - peer verification required 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 fc539b7..23ad616 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java @@ -13,8 +13,14 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; -import java.io.FileNotFoundException; +import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -95,7 +101,6 @@ public class SharedApiService { return project.getProjectAdministrator(); } - // TODO public Iterable getAppointmentsByProjectId(long projectId, String mail) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) { logger.warn( @@ -123,9 +128,8 @@ public class SharedApiService { return appointments; } - // public void getPDFReport(long appointmentId, String mail) - throws FileNotFoundException, DocumentException { + throws DocumentException, URISyntaxException, IOException { long projectId = this.appointmentService .getAppointmentById(appointmentId) @@ -202,9 +206,24 @@ public class SharedApiService { document.add(new Paragraph("\n")); document.close(); + + // Replace uri with website address + Files.copy( + new URI( + "http://localhost:8080/shared/projects/appointments/report/" + + appointmentId) + .toURL() + .openStream(), + Paths.get("Report" + appointmentId + ".pdf"), + StandardCopyOption.REPLACE_EXISTING); + + // delete file, we don't want to stock all reports on the server + File file = new File("Report" + appointmentId + ".pdf"); + if (!file.delete()) { + logger.warn("Failed to delete report {}", file.getAbsolutePath()); + } } - // TODO public void createAppointmentRequest(Appointment appointment, String mail) { long projectId = appointment