From 4720669c2c73316034ada8367cad33d61c7b8366 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Thu, 11 Apr 2024 12:29:03 +0200 Subject: [PATCH] added EndPoints and Mock --- .../ScientificPublications/ResearchDTO.java | 44 +++++++++++++ .../ScientificPublications/ResearcherDTO.java | 31 +++++++++ .../Clyde/EndPoints/MockController.java | 25 ++++++- .../ResearchController.java | 21 +++++- .../ResearcherController.java | 65 +++++++++++++++++++ .../ResearchRepository.java | 2 +- .../ResearcherRepository.java | 4 +- .../ResearchesService.java | 22 ++++++- .../ScientificPublications/Research.java | 2 +- .../ResearchCoAuthors.java | 11 ++-- .../ScientificPublications/Researcher.java | 12 +++- 11 files changed, 223 insertions(+), 16 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearcherDTO.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearcherController.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java b/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java new file mode 100644 index 0000000..2c35a82 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java @@ -0,0 +1,44 @@ +package ovh.herisson.Clyde.DTO.ScientificPublications; + + +import lombok.Data; +import ovh.herisson.Clyde.Tables.ScientificPublications.Access; +import ovh.herisson.Clyde.Tables.ScientificPublications.PaperType; +import ovh.herisson.Clyde.Tables.ScientificPublications.Research; + +import java.util.Date; + +@Data +public class ResearchDTO { + + private long id; + private String title; + private ResearcherDTO researcher; + private Date releaseDate; + private PaperType paperType; + private String pdfLocation; + private String language; + private Access access; + private String domain; + private String summary; + + private ResearchDTO(String title, ResearcherDTO researcherDTO, Date releaseDate, PaperType paperType, String pdfLocation, String language, Access access, String domain, String summary, long id) { + this.title = title; + this.researcher = researcherDTO; + this.releaseDate = releaseDate; + this.paperType = paperType; + this.pdfLocation = pdfLocation; + this.language = language; + this.access = access; + this.domain = domain; + this.summary = summary; + this.id = id; + } + + + public static ResearchDTO construct(Research research){ + return new ResearchDTO(research.getTitle(), ResearcherDTO.construct(research.getAuthor()), research.getReleaseDate(), + research.getPaperType(),research.getPdfLocation(),research.getLanguage(),research.getAccess(), + research.getDomain(), research.getSummary(), research.getId()); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearcherDTO.java b/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearcherDTO.java new file mode 100644 index 0000000..c9eebd4 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearcherDTO.java @@ -0,0 +1,31 @@ +package ovh.herisson.Clyde.DTO.ScientificPublications; + +import lombok.Data; +import ovh.herisson.Clyde.Services.ProtectionService; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; + +import java.util.Map; + +@Data +public class ResearcherDTO { + + private long id; + private Map user; + private String site; + private String domain; + private String orcidId; + + + private ResearcherDTO(long id, Map user, String site,String domain,String orcidId){ + this.domain = domain; + this.orcidId = orcidId; + this.site = site; + this.user = user; + this.id = id; + } + + public static ResearcherDTO construct(Researcher researcher){ + return new ResearcherDTO(researcher.getId(), ProtectionService.userWithoutPassword(researcher.getUser()),researcher.getSite(), + researcher.getDomain(),researcher.getOrcidId()); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 0b7a1b4..422cded 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -5,7 +5,13 @@ import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Services.*; +import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService; import ovh.herisson.Clyde.Tables.*; +import ovh.herisson.Clyde.Tables.ScientificPublications.Access; +import ovh.herisson.Clyde.Tables.ScientificPublications.PaperType; +import ovh.herisson.Clyde.Tables.ScientificPublications.Research; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; + import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -24,10 +30,12 @@ public class MockController { public final CourseService courseService; public final InscriptionService inscriptionService; + + public final ResearchesService researchesService; ArrayList mockUsers; - public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){ + public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService, ResearchesService researchesService){ this.tokenRepo = tokenRepo; this.userRepo = userRepo; this.tokenService = tokenService; @@ -35,6 +43,7 @@ public class MockController { this.curriculumService = curriculumService; this.courseService = courseService; this.inscriptionService = inscriptionService; + this.researchesService = researchesService; } /** Saves an example of each user type by : @@ -95,7 +104,19 @@ public class MockController { InscriptionRequest inscriptionRequest = new InscriptionRequest("helen","prenom","non","helen@gmail.com","america",new Date(),(long) 1,RequestState.Pending,"yes.png","password"); inscriptionService.save(inscriptionRequest); - + + + /////////////////////////// + // extension Publications Scientifiques + Researcher jojoResearcherAccount = new Researcher(jojo,"3363-22555-AB33-T",null,"IT"); + + Researcher output = researchesService.saveResearcher(jojoResearcherAccount); + + Research jojoResearch = new Research("Graphs : Advanced Search Algorithms",output,new Date(), + PaperType.Article,null,null,"english", + Access.OpenSource,"IT","This Article's title speak for itself\n We'll discuss about advanced Graph search Algorithms"); + + researchesService.saveResearch(jojoResearch); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java index c3bdf9b..cab79d1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java @@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import ovh.herisson.Clyde.DTO.ScientificPublications.ResearchDTO; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService; @@ -11,6 +12,7 @@ import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.ScientificPublications.Research; import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; +import java.util.ArrayList; import java.util.Map; @RestController @@ -29,7 +31,7 @@ public class ResearchController { * the return Research Download URL will be null */ @GetMapping("/research/{id}") - public ResponseEntity getResearch(@RequestHeader("Authorization") String token, @PathVariable long id){ + public ResponseEntity getResearch(@RequestHeader(value = "Authorization", required = false) String token, @PathVariable long id){ Research research = researchesServ.getResearchById(id); @@ -40,7 +42,22 @@ public class ResearchController { research.setPdfLocation(null); }// If the user doesn't have access to the document he can't download the pdf - return new ResponseEntity<>(research, HttpStatus.OK); + return new ResponseEntity<>(ResearchDTO.construct(research), HttpStatus.OK); + } + + @GetMapping("/researches") + public ResponseEntity> getResearches(@RequestHeader(value = "Authorization",required = false) String token){ + Iterable researches = researchesServ.getAllResearches(); + + ArrayList toReturnResearches = new ArrayList<>(); + + for (Research research: researches){ + if (researchesServ.hasNoAccessTo(research,authServ.getUserFromToken(token))){ + research.setPdfLocation(null); + } + toReturnResearches.add(ResearchDTO.construct(research)); + } + return new ResponseEntity<>(toReturnResearches,HttpStatus.OK); } @PostMapping("/research") diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearcherController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearcherController.java new file mode 100644 index 0000000..bb210f6 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearcherController.java @@ -0,0 +1,65 @@ +package ovh.herisson.Clyde.EndPoints.ScientificPublications; + +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import ovh.herisson.Clyde.DTO.ScientificPublications.ResearcherDTO; +import ovh.herisson.Clyde.Responses.UnauthorizedResponse; +import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; + +import java.util.ArrayList; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +@AllArgsConstructor +public class ResearcherController { + + ResearchesService researchesServ; + AuthenticatorService authServ; + + + @GetMapping("/researcher/{id}") + public ResponseEntity getResearcher(@PathVariable long id){ + Researcher researcher = researchesServ.getResearcherById(id); + return new ResponseEntity<>(ResearcherDTO.construct(researcher),HttpStatus.OK); + } + + /** + * Everyone can access every researcher Account + * @return all the researchers accounts + */ + @GetMapping("/researchers") + public ResponseEntity> getAllResearchers(){ + Iterable researchers = researchesServ.getAllResearchers(); + ArrayList toReturnResearchersDTO = new ArrayList<>(); + for (Researcher researcher: researchers){ + toReturnResearchersDTO.add(ResearcherDTO.construct(researcher)); + } + return new ResponseEntity<>(toReturnResearchersDTO, HttpStatus.OK); + } + + @PostMapping("/researcher") + public ResponseEntity postResearcher(@RequestHeader("Authorization") String token, @RequestBody Researcher researcher){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token)){ + return new UnauthorizedResponse<>(null); + } + + Researcher posted = researchesServ.saveResearcher(researcher); + + return new ResponseEntity<>(ResearcherDTO.construct(posted), HttpStatus.OK); + } + + @DeleteMapping("/researcher/{id}") + public ResponseEntity deleteResearcher(@RequestHeader ("Authorization") String token, @PathVariable long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + + researchesServ.deleteResearcher(researchesServ.getResearcherById(id)); + return new ResponseEntity<>(HttpStatus.OK); + } + +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java index 09d74ad..8c100c6 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java @@ -5,5 +5,5 @@ import ovh.herisson.Clyde.Tables.ScientificPublications.Research; public interface ResearchRepository extends CrudRepository { - public Research findById(long id); + Research findById(long id); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java index 3cb2f9d..a143972 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java @@ -5,5 +5,7 @@ import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; import ovh.herisson.Clyde.Tables.User; public interface ResearcherRepository extends CrudRepository { - public Researcher findByUser(User user); + Researcher findByUser(User user); + + Researcher findById(long id); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java index e7ca875..60a8c3e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java @@ -100,7 +100,27 @@ public class ResearchesService { articleRepo.save(research); } - public void delete(Research research) { + public void deleteResearch(Research research) { articleRepo.delete(research); } + + public Iterable getAllResearches() { + return articleRepo.findAll(); + } + + public Researcher saveResearcher(Researcher researcher) { + return researcherRepo.save(researcher); + } + + public Iterable getAllResearchers() { + return researcherRepo.findAll(); + } + + public Researcher getResearcherById(long id) { + return researcherRepo.findById(id); + } + + public void deleteResearcher(Researcher researcher) { + researcherRepo.delete(researcher); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java index 3d31056..3bf42a3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java @@ -1,7 +1,7 @@ package ovh.herisson.Clyde.Tables.ScientificPublications; /****************************************************** - * @file Article.java + * @file Research.java * @author Maxime Bartha * @scope Extension Publications scientifiques * diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java index 1b657ce..0b78245 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java @@ -1,17 +1,14 @@ package ovh.herisson.Clyde.Tables.ScientificPublications; /****************************************************** - * @file ArticleCoAuthors + * @file ResearchCoAuthors * @author Maxime Bartha * @scope Extension Publications scientifiques * * Co-Authors List entity (will be accessed by Articles) * ******************************************************/ -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -26,6 +23,10 @@ import org.hibernate.annotations.OnDeleteAction; @Entity public class ResearchCoAuthors { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + Long id; + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Researcher") private Researcher coAuthor; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java index 23fea98..1da105c 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java @@ -19,15 +19,21 @@ import ovh.herisson.Clyde.Tables.User; @Getter @Setter @NoArgsConstructor -@AllArgsConstructor @Entity public class Researcher { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; - @OneToOne + @OneToOne(cascade=CascadeType.REMOVE, optional=true) private User user; private String orcidId; private String site; - private String Domain; + private String domain; + + public Researcher(User user, String orcidId, String site, String domain){ + this.user = user; + this.orcidId = orcidId; + this.site = site; + this.domain = domain; + } }