1
0
forked from PGL/Clyde

added all endPoints

This commit is contained in:
Bartha Maxime 2024-04-12 01:56:18 +02:00
parent 4720669c2c
commit 17cb969160
6 changed files with 169 additions and 12 deletions

View File

@ -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);
}
}

View File

@ -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<String> 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<Iterable<ResearcherDTO>> getCoAuthors(@PathVariable long id){
Research research = researchesServ.getResearchById(id);
if (research == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Iterable<Researcher> researchers= researchesServ.getCoAuthors(research);
ArrayList<ResearcherDTO> 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<Iterable<ResearcherDTO>> postCoAuthor(@RequestHeader("Authorization") String token,
@RequestBody Iterable<Long> 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<ResearcherDTO> 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<String> 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);
}
}

View File

@ -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<ResearcherDTO> patchResearcher(@RequestHeader("Authorization") String token,
@PathVariable long id,
@RequestBody Map<String ,Object> 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<String> 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);
}
}

View File

@ -10,5 +10,9 @@ import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher;
public interface ResearchCoAuthorsRepository extends CrudRepository<ResearchCoAuthors,Long> {
@Query("select distinct rca.coAuthor from ResearchCoAuthors rca where rca.research = ?1")
public Iterable<Researcher> findResearchCoAuthors(Research research);
Iterable<Researcher> findResearchCoAuthors(Research research);
@Query("select rca from ResearchCoAuthors rca where rca.research = ?1 and rca.coAuthor =?2")
ResearchCoAuthors findResearchCoAuthors(Research research, Researcher researcher);
}

View File

@ -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<Long> researchersId, Research research) {
if (researchersId == null) return false;
ArrayList<Researcher> 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<Researcher> 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<String, Object> updates) {
for (Map.Entry<String, Object> 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);
}
}

View File

@ -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;
}
}