From dd5ca2cbd74fea623afe95409797994190615173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Wed, 26 Feb 2025 14:51:02 +0100 Subject: [PATCH] feat: entrepreneur api and api service --- .../myinpulse/controller/EntrepreneurApi.java | 27 ++++---- .../enseirb/myinpulse/model/Annotation.java | 2 +- .../service/EntrepreneurApiService.java | 55 +++++++++++++--- .../service/database/AnnotationService.java | 54 ++++++++++++++++ .../service/database/SectionCellService.java | 63 +++++++++++++++++++ 5 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java create mode 100644 MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java index 2ace1c7..b0de5b7 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/EntrepreneurApi.java @@ -1,6 +1,6 @@ package enseirb.myinpulse.controller; -import enseirb.myinpulse.model.LCSection; +import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.service.EntrepreneurApiService; @@ -29,12 +29,12 @@ public class EntrepreneurApi { * @return status code */ @PutMapping("/entrepreneur/lcsection/modify/{sectionId}") - public void editLCSection( - @PathVariable String sectionId, - @RequestBody LCSection section, + public void editSectionCell( + @PathVariable Long sectionId, + @RequestBody SectionCell sectionCell, @AuthenticationPrincipal Jwt principal) { - entrepreneurApiService.editLCSection( - sectionId, section, principal.getClaimAsString("email")); + entrepreneurApiService.editSectionCell( + sectionId, sectionCell, principal.getClaimAsString("email")); } /** @@ -45,9 +45,9 @@ public class EntrepreneurApi { * @return status code */ @DeleteMapping("/entrepreneur/lcsection/remove/{sectionId}") - public void removeLCSection( - @PathVariable String sectionId, @AuthenticationPrincipal Jwt principal) { - entrepreneurApiService.removeLCSection(sectionId, principal.getClaimAsString("email")); + public void removeSectionCell( + @PathVariable Long sectionId, @AuthenticationPrincipal Jwt principal) { + entrepreneurApiService.removeSectionCell(sectionId, principal.getClaimAsString("email")); } /** @@ -57,13 +57,10 @@ public class EntrepreneurApi { * * @return status code */ - @PostMapping("/entrepreneur/lcsection/add/{sectionId}") + @PostMapping("/entrepreneur/lcsection/add") // remove id from doc aswell public void addLCSection( - @PathVariable String sectionId, - @RequestBody LCSection section, - @AuthenticationPrincipal Jwt principal) { - entrepreneurApiService.addLCSection( - sectionId, section, principal.getClaimAsString("email")); + @RequestBody SectionCell sectionCell, @AuthenticationPrincipal Jwt principal) { + entrepreneurApiService.addSectionCell(sectionCell, principal.getClaimAsString("email")); } /** diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java index e7e9b3e..fa73e0d 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java @@ -33,7 +33,7 @@ public class Annotation { return comment; } - public void setCommentary(String comment) { + public void setComment(String comment) { this.comment = comment; } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java index 80d06ba..d22a6a9 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -1,29 +1,66 @@ package enseirb.myinpulse.service; -import enseirb.myinpulse.model.LCSection; import enseirb.myinpulse.model.Project; +import enseirb.myinpulse.model.SectionCell; +import enseirb.myinpulse.service.database.ProjectService; +import enseirb.myinpulse.service.database.SectionCellService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; @Service public class EntrepreneurApiService { - public void editLCSection(String sectionId, LCSection section, String mail) { - throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); + private final SectionCellService sectionCellService; + private final ProjectService projectService; + + @Autowired + EntrepreneurApiService(SectionCellService sectionCellService, ProjectService projectService) { + this.sectionCellService = sectionCellService; + this.projectService = projectService; } - public void removeLCSection(String sectionId, String mail) { - throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); + public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) { + SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); + if (editSectionCell == null) { + System.err.println("Trying to edit unknown section cell"); + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); + } + sectionCellService.updateSectionCell( + sectionCellId, + sectionCell.getTitle(), + sectionCell.getContentSectionCell(), + sectionCell.getModificationDate()); } - public void addLCSection(String sectionId, LCSection section, String mail) { - throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); + public void removeSectionCell(Long sectionCellId, String mail) { + SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); + if (editSectionCell == null) { + System.err.println("Trying to remove unknown section cell"); + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); + } + sectionCellService.removeSectionCellById(sectionCellId); + } + + public void addSectionCell(SectionCell sectionCell, String mail) { + if (sectionCell == null) { + System.err.println("Trying to create an empty section cell"); + throw new ResponseStatusException( + HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide"); + } + sectionCellService.addNewSectionCell(sectionCell); } public void requestNewProject(Project project, String mail) { - throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); + if (project == null) { + System.err.println("Trying to request the creation of a null project"); + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le projet fourni est vide"); + } + project.setProjectStatus("PENDING"); + projectService.addNewProject(project); } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java new file mode 100644 index 0000000..225ef06 --- /dev/null +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AnnotationService.java @@ -0,0 +1,54 @@ +package enseirb.myinpulse.service.database; + +import enseirb.myinpulse.model.Annotation; +import enseirb.myinpulse.repository.AnnotationRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Optional; + +@Service +public class AnnotationService { + + private final AnnotationRepository annotationRepository; + + @Autowired + AnnotationService(AnnotationRepository annotationRepository) { + this.annotationRepository = annotationRepository; + } + + public Iterable getAllAnnotations() { + return annotationRepository.findAll(); + } + + public Annotation getAnnotationById(Long id) { + Optional annotation = annotationRepository.findById(id); + if (annotation.isEmpty()) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cette annotation n'existe pas"); + } + return annotation.get(); + } + + public Annotation addNewAnnotation(Annotation annotation) { + return this.annotationRepository.save(annotation); + } + + public void deleteAnnotationById(Long id) { + this.annotationRepository.deleteById(id); + } + + public Annotation updateAnnotation(Long id, String comment) { + Optional annotation = annotationRepository.findById(id); + if (annotation.isEmpty()) { + return null; + } + if (comment != null) { + annotation.get().setComment(comment); + } + return this.annotationRepository.save(annotation.get()); + } +} 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 new file mode 100644 index 0000000..c65f770 --- /dev/null +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/SectionCellService.java @@ -0,0 +1,63 @@ +package enseirb.myinpulse.service.database; + +import enseirb.myinpulse.model.SectionCell; +import enseirb.myinpulse.repository.SectionCellRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; + +import java.time.LocalDateTime; +import java.util.Optional; + +@Service +public class SectionCellService { + + private final SectionCellRepository sectionCellRepository; + + @Autowired + SectionCellService(SectionCellRepository sectionCellRepository) { + this.sectionCellRepository = sectionCellRepository; + } + + public Iterable getAllSectionCells() { + return this.sectionCellRepository.findAll(); + } + + public SectionCell getSectionCellById(Long id) { + Optional sectionCell = this.sectionCellRepository.findById(id); + if (sectionCell.isEmpty()) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); + } + return sectionCell.get(); + } + + public SectionCell addNewSectionCell(SectionCell sectionCell) { + return this.sectionCellRepository.save(sectionCell); + } + + public void removeSectionCellById(Long id) { + this.sectionCellRepository.deleteById(id); + } + + public SectionCell updateSectionCell( + Long id, String title, String contentSectionCell, LocalDateTime modificationDate) { + Optional sectionCell = this.sectionCellRepository.findById(id); + if (sectionCell.isEmpty()) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); + } + if (title != null) { + sectionCell.get().setTitle(title); + } + if (contentSectionCell != null) { + sectionCell.get().setContentSectionCell(contentSectionCell); + } + if (modificationDate != null) { + sectionCell.get().setModificationDate(modificationDate); + } + return this.sectionCellRepository.save(sectionCell.get()); + } +}