1
0
forked from PGL/Clyde

added EndPoints and Mock

This commit is contained in:
Bartha Maxime 2024-04-11 12:29:03 +02:00
parent 5e9eccc4f6
commit 4720669c2c
11 changed files with 223 additions and 16 deletions

View File

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

View File

@ -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<String,Object> user;
private String site;
private String domain;
private String orcidId;
private ResearcherDTO(long id, Map<String ,Object> 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());
}
}

View File

@ -5,7 +5,13 @@ import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Services.*; import ovh.herisson.Clyde.Services.*;
import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService;
import ovh.herisson.Clyde.Tables.*; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -24,10 +30,12 @@ public class MockController {
public final CourseService courseService; public final CourseService courseService;
public final InscriptionService inscriptionService; public final InscriptionService inscriptionService;
public final ResearchesService researchesService;
ArrayList<User> mockUsers; ArrayList<User> 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.tokenRepo = tokenRepo;
this.userRepo = userRepo; this.userRepo = userRepo;
this.tokenService = tokenService; this.tokenService = tokenService;
@ -35,6 +43,7 @@ public class MockController {
this.curriculumService = curriculumService; this.curriculumService = curriculumService;
this.courseService = courseService; this.courseService = courseService;
this.inscriptionService = inscriptionService; this.inscriptionService = inscriptionService;
this.researchesService = researchesService;
} }
/** Saves an example of each user type by : /** Saves an example of each user type by :
@ -96,6 +105,18 @@ public class MockController {
inscriptionService.save(inscriptionRequest); 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);
} }
} }

View File

@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.DTO.ScientificPublications.ResearchDTO;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService; 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.Research;
import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher;
import java.util.ArrayList;
import java.util.Map; import java.util.Map;
@RestController @RestController
@ -29,7 +31,7 @@ public class ResearchController {
* the return Research Download URL will be null * the return Research Download URL will be null
*/ */
@GetMapping("/research/{id}") @GetMapping("/research/{id}")
public ResponseEntity<Research> getResearch(@RequestHeader("Authorization") String token, @PathVariable long id){ public ResponseEntity<ResearchDTO> getResearch(@RequestHeader(value = "Authorization", required = false) String token, @PathVariable long id){
Research research = researchesServ.getResearchById(id); Research research = researchesServ.getResearchById(id);
@ -40,7 +42,22 @@ public class ResearchController {
research.setPdfLocation(null); research.setPdfLocation(null);
}// If the user doesn't have access to the document he can't download the pdf }// 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<Iterable<ResearchDTO>> getResearches(@RequestHeader(value = "Authorization",required = false) String token){
Iterable<Research> researches = researchesServ.getAllResearches();
ArrayList<ResearchDTO> 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") @PostMapping("/research")

View File

@ -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<ResearcherDTO> 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<Iterable<ResearcherDTO>> getAllResearchers(){
Iterable<Researcher> researchers = researchesServ.getAllResearchers();
ArrayList<ResearcherDTO> toReturnResearchersDTO = new ArrayList<>();
for (Researcher researcher: researchers){
toReturnResearchersDTO.add(ResearcherDTO.construct(researcher));
}
return new ResponseEntity<>(toReturnResearchersDTO, HttpStatus.OK);
}
@PostMapping("/researcher")
public ResponseEntity<ResearcherDTO> 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<String> 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);
}
}

View File

@ -5,5 +5,5 @@ import ovh.herisson.Clyde.Tables.ScientificPublications.Research;
public interface ResearchRepository extends CrudRepository<Research,Long> { public interface ResearchRepository extends CrudRepository<Research,Long> {
public Research findById(long id); Research findById(long id);
} }

View File

@ -5,5 +5,7 @@ import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
public interface ResearcherRepository extends CrudRepository<Researcher,Long> { public interface ResearcherRepository extends CrudRepository<Researcher,Long> {
public Researcher findByUser(User user); Researcher findByUser(User user);
Researcher findById(long id);
} }

View File

@ -100,7 +100,27 @@ public class ResearchesService {
articleRepo.save(research); articleRepo.save(research);
} }
public void delete(Research research) { public void deleteResearch(Research research) {
articleRepo.delete(research); articleRepo.delete(research);
} }
public Iterable<Research> getAllResearches() {
return articleRepo.findAll();
}
public Researcher saveResearcher(Researcher researcher) {
return researcherRepo.save(researcher);
}
public Iterable<Researcher> getAllResearchers() {
return researcherRepo.findAll();
}
public Researcher getResearcherById(long id) {
return researcherRepo.findById(id);
}
public void deleteResearcher(Researcher researcher) {
researcherRepo.delete(researcher);
}
} }

View File

@ -1,7 +1,7 @@
package ovh.herisson.Clyde.Tables.ScientificPublications; package ovh.herisson.Clyde.Tables.ScientificPublications;
/****************************************************** /******************************************************
* @file Article.java * @file Research.java
* @author Maxime Bartha * @author Maxime Bartha
* @scope Extension Publications scientifiques * @scope Extension Publications scientifiques
* *

View File

@ -1,17 +1,14 @@
package ovh.herisson.Clyde.Tables.ScientificPublications; package ovh.herisson.Clyde.Tables.ScientificPublications;
/****************************************************** /******************************************************
* @file ArticleCoAuthors * @file ResearchCoAuthors
* @author Maxime Bartha * @author Maxime Bartha
* @scope Extension Publications scientifiques * @scope Extension Publications scientifiques
* *
* Co-Authors List entity (will be accessed by Articles) * Co-Authors List entity (will be accessed by Articles)
* *
******************************************************/ ******************************************************/
import jakarta.persistence.Entity; import jakarta.persistence.*;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -26,6 +23,10 @@ import org.hibernate.annotations.OnDeleteAction;
@Entity @Entity
public class ResearchCoAuthors { public class ResearchCoAuthors {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Researcher") @JoinColumn(name = "Researcher")
private Researcher coAuthor; private Researcher coAuthor;

View File

@ -19,15 +19,21 @@ import ovh.herisson.Clyde.Tables.User;
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor
@Entity @Entity
public class Researcher { public class Researcher {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long id; private long id;
@OneToOne @OneToOne(cascade=CascadeType.REMOVE, optional=true)
private User user; private User user;
private String orcidId; private String orcidId;
private String site; 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;
}
} }