From 17cb969160f9b4a73d2d82d37bd3118db03ab47d Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Fri, 12 Apr 2024 01:56:18 +0200 Subject: [PATCH] added all endPoints --- .../Clyde/EndPoints/MockController.java | 14 +++- .../ResearchController.java | 72 ++++++++++++++++++- .../ResearcherController.java | 19 ++++- .../ResearchCoAuthorsRepository.java | 6 +- .../ResearchesService.java | 64 +++++++++++++++-- .../ResearchCoAuthors.java | 6 +- 6 files changed, 169 insertions(+), 12 deletions(-) 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 422cded..411aa92 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -113,9 +113,21 @@ public class MockController { Researcher output = researchesService.saveResearcher(jojoResearcherAccount); Research jojoResearch = new Research("Graphs : Advanced Search Algorithms",output,new Date(), - PaperType.Article,null,null,"english", + PaperType.Article,"here",null,"english", Access.OpenSource,"IT","This Article's title speak for itself\n We'll discuss about advanced Graph search Algorithms"); + Research restrictedResearch = new Research("Graphs : Advanced Search Algorithms",output,new Date(), + PaperType.Article,"restricted",null,"english", + Access.Restricted,"Restricted","This Article's title speak for itself\n We'll discuss about advanced Graph search Algorithms"); + + Research privateResearch = new Research("Graphs : Advanced Search Algorithms",output,new Date(), + PaperType.Article,"private",null,"english", + Access.Private,"private","This Article's title speak for itself\n We'll discuss about advanced Graph search Algorithms"); + + + researchesService.saveResearch(restrictedResearch); + researchesService.saveResearch(privateResearch); + 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 cab79d1..692b4cc 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 @@ -5,6 +5,7 @@ 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.DTO.ScientificPublications.ResearcherDTO; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService; @@ -36,7 +37,7 @@ public class ResearchController { Research research = researchesServ.getResearchById(id); if (research == null) - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); if (researchesServ.hasNoAccessTo(research,authServ.getUserFromToken(token))){ research.setPdfLocation(null); @@ -81,7 +82,7 @@ public class ResearchController { Researcher researcher = researchesServ.getResearcherByUser(authServ.getUserFromToken(token)); if (research == null) - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token) && researcher != research.getAuthor()) { @@ -92,6 +93,9 @@ public class ResearchController { return new ResponseEntity<>(HttpStatus.OK); } + /** Only Admin, Secretary and author can delete a research + * + */ @DeleteMapping("/research/{id}") public ResponseEntity deleteResearch(@RequestHeader("Authorization") String token, @PathVariable long id){ @@ -106,7 +110,69 @@ public class ResearchController { return new UnauthorizedResponse<>(null); } - researchesServ.delete(research); + researchesServ.deleteResearch(research); + return new ResponseEntity<>(HttpStatus.OK); + } + + + ///////////// + // Co-authors Part + + @GetMapping("/research/{id}/co-authors") + public ResponseEntity> getCoAuthors(@PathVariable long id){ + Research research = researchesServ.getResearchById(id); + + if (research == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); + + Iterable researchers= researchesServ.getCoAuthors(research); + ArrayList toReturn = new ArrayList<>(); + + for (Researcher researcher: researchers){ + toReturn.add(ResearcherDTO.construct(researcher)); + } + return new ResponseEntity<>(toReturn,HttpStatus.OK); + } + + @PostMapping("/research/{id}/co-authors") + public ResponseEntity> postCoAuthor(@RequestHeader("Authorization") String token, + @RequestBody Iterable researchersId, + @PathVariable long id) + { + Research research = researchesServ.getResearchById(id); + if (research == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); + + if (authServ.isNotIn(new Role[]{Role.Admin}, token) || research.getAuthor() != researchesServ.getResearcherByUser(authServ.getUserFromToken(token))) + return new UnauthorizedResponse<>(null); + + if (!researchesServ.saveCoAuthors(researchersId,research)) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + ArrayList toReturn = new ArrayList<>(); + for (Long reId: researchersId){ + toReturn.add(ResearcherDTO.construct(researchesServ.getResearcherById(reId))); + } + + return new ResponseEntity<>(toReturn,HttpStatus.OK); + } + + /** deletes the co-author with *coAuthorId* from the research with the *researchId* + */ + @DeleteMapping("/research/{researchId}/co-author/{coAuthorId}") + public ResponseEntity deleteCoAuthor(@RequestHeader("Authorization") String token, + @PathVariable long researchId, + @PathVariable long coAuthorId) + { + Research research = researchesServ.getResearchById(researchId); + Researcher coAuthor = researchesServ.getResearcherById(coAuthorId); + + if (authServ.isNotIn(new Role[]{Role.Admin}, token) || research.getAuthor() != researchesServ.getResearcherByUser(authServ.getUserFromToken(token))) + return new UnauthorizedResponse<>(null); + + if (coAuthor == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); + + if (!researchesServ.deleteCoAuthor(research, coAuthor)) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.OK); } } 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 index bb210f6..9108691 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearcherController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearcherController.java @@ -12,6 +12,7 @@ import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; import java.util.ArrayList; +import java.util.Map; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") @@ -53,6 +54,23 @@ public class ResearcherController { return new ResponseEntity<>(ResearcherDTO.construct(posted), HttpStatus.OK); } + @PatchMapping("/researcher/{id}") + public ResponseEntity patchResearcher(@RequestHeader("Authorization") String token, + @PathVariable long id, + @RequestBody Map updates){ + + Researcher researcher = researchesServ.getResearcherById(id); //todo check authorization j'ai pu patch sans le bon token + if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin}, token) + && researcher == researchesServ.getResearcherByUser(authServ.getUserFromToken(token))) + return new UnauthorizedResponse<>(null); + + if (researcher == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + researchesServ.modifyResearcherData(researcher,updates); + + return new ResponseEntity<>(ResearcherDTO.construct(researcher),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)) @@ -61,5 +79,4 @@ public class ResearcherController { researchesServ.deleteResearcher(researchesServ.getResearcherById(id)); return new ResponseEntity<>(HttpStatus.OK); } - } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java index 70eb16e..51f25fb 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java @@ -10,5 +10,9 @@ import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; public interface ResearchCoAuthorsRepository extends CrudRepository { @Query("select distinct rca.coAuthor from ResearchCoAuthors rca where rca.research = ?1") - public Iterable findResearchCoAuthors(Research research); + Iterable findResearchCoAuthors(Research research); + + + @Query("select rca from ResearchCoAuthors rca where rca.research = ?1 and rca.coAuthor =?2") + ResearchCoAuthors findResearchCoAuthors(Research research, Researcher researcher); } 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 60a8c3e..a48fb1c 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 @@ -6,12 +6,10 @@ import ovh.herisson.Clyde.Repositories.ScientificPublications.ResearchCoAuthorsR import ovh.herisson.Clyde.Repositories.ScientificPublications.ResearchRepository; import ovh.herisson.Clyde.Repositories.ScientificPublications.ResearcherRepository; import ovh.herisson.Clyde.Tables.Role; -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 ovh.herisson.Clyde.Tables.ScientificPublications.*; import ovh.herisson.Clyde.Tables.User; +import java.util.ArrayList; import java.util.Date; import java.util.Map; @@ -46,11 +44,14 @@ public class ResearchesService { public boolean hasNoAccessTo(Research research, User user){ + if (research.getAccess() == Access.OpenSource) return false; // if the access is open source even non-users can see it if (user == null) return true; // else you need at least to be a user + if (user.getRole() == Role.Admin) return false; + if (research.getAccess() == Access.Restricted && - user.getRole() == Role.Admin || user.getRole() == Role.Secretary || + user.getRole() == Role.Secretary || user.getRole() == Role.Teacher || user.getRole() == Role.InscriptionService) return false; // if the access is restricted only the staff member (above) can access the research @@ -123,4 +124,57 @@ public class ResearchesService { public void deleteResearcher(Researcher researcher) { researcherRepo.delete(researcher); } + + public boolean saveCoAuthors(Iterable researchersId, Research research) { + + if (researchersId == null) return false; + + ArrayList toAdd = new ArrayList<>(); + for (long researcherId : researchersId){ + Researcher researcher= researcherRepo.findById(researcherId); + if (research== null){ + return false; + } + if (!toAdd.contains(researcher)) + { + toAdd.add(researcher); + } + } + for (Researcher researcher: toAdd){ + researchCoAuthorsRepo.save(new ResearchCoAuthors(researcher,research)); + } + return true; + } + + public Iterable getCoAuthors(Research research) { + return researchCoAuthorsRepo.findResearchCoAuthors(research); + } + + public boolean deleteCoAuthor(Research research,Researcher coAuthor) { + ResearchCoAuthors result = researchCoAuthorsRepo.findResearchCoAuthors(research,coAuthor); + + if (result ==null) + return false; + + researchCoAuthorsRepo.delete(result); + return true; + } + + public void modifyResearcherData(Researcher researcher, Map updates) { + + for (Map.Entry entry : updates.entrySet()){ + switch (entry.getKey()){ + case "orcidId": + researcher.setOrcidId((String) entry.getValue()); + break; + case "domain": + researcher.setDomain((String) entry.getValue()); + break; + case "site": + researcher.setSite((String) entry.getValue()); + break; + } + } + researcherRepo.save(researcher); + } } 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 0b78245..dbc293d 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 @@ -19,7 +19,6 @@ import org.hibernate.annotations.OnDeleteAction; @Getter @Setter @NoArgsConstructor -@AllArgsConstructor @Entity public class ResearchCoAuthors { @@ -35,4 +34,9 @@ public class ResearchCoAuthors { @OnDelete(action = OnDeleteAction.CASCADE) @JoinColumn(name = "Article") private Research research; + + public ResearchCoAuthors(Researcher coAuthor, Research research){ + this.coAuthor = coAuthor; + this.research = research; + } }