1
0
forked from PGL/Clyde

added comments and fixed logic issue

This commit is contained in:
Bartha Maxime 2024-04-14 12:34:04 +02:00
parent 17cb969160
commit 1498cfa11e
5 changed files with 59 additions and 3 deletions

View File

@ -1,5 +1,12 @@
package ovh.herisson.Clyde.DTO.ScientificPublications;
/******************************************************
* @file ResearchDTO.java
* @author Bartha Maxime
* @scope Publications Scientifiques
*
* Research format to return to front (without author's password)
******************************************************/
import lombok.Data;
import ovh.herisson.Clyde.Tables.ScientificPublications.Access;

View File

@ -1,5 +1,12 @@
package ovh.herisson.Clyde.DTO.ScientificPublications;
/******************************************************
* @file ResearcherDTO.java
* @author Bartha Maxime
* @scope Publications Scientifiques
*
* Researcher Format to return to front (without user password)
******************************************************/
import lombok.Data;
import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher;

View File

@ -1,5 +1,13 @@
package ovh.herisson.Clyde.EndPoints.ScientificPublications;
/******************************************************
* @file ResearchController.java
* @author Bartha Maxime
* @scope Publications Scientifiques
*
* API class for the researches
******************************************************/
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -46,6 +54,10 @@ public class ResearchController {
return new ResponseEntity<>(ResearchDTO.construct(research), HttpStatus.OK);
}
/**
* @param token used to know if the user can download or not the research pdf
* @return every research
*/
@GetMapping("/researches")
public ResponseEntity<Iterable<ResearchDTO>> getResearches(@RequestHeader(value = "Authorization",required = false) String token){
Iterable<Research> researches = researchesServ.getAllResearches();
@ -61,6 +73,10 @@ public class ResearchController {
return new ResponseEntity<>(toReturnResearches,HttpStatus.OK);
}
/** post a new research
*
* @return the research saved
*/
@PostMapping("/research")
public ResponseEntity<Research> postResearch(@RequestHeader("Authorization") String token, @RequestBody Research research){
@ -72,6 +88,9 @@ public class ResearchController {
return new ResponseEntity<>(researchesServ.saveResearch(research), HttpStatus.OK);
}
/** post updates to the research
*
*/
@PatchMapping("/research/{id}")
public ResponseEntity<String> patchResearch(@RequestHeader("Authorization") String token,
@RequestBody Map<String,Object> updates,
@ -118,6 +137,11 @@ public class ResearchController {
/////////////
// Co-authors Part
/** get all the co-authors in a research
*
* @param id the research id
* @return all the co-authors associated with the research
*/
@GetMapping("/research/{id}/co-authors")
public ResponseEntity<Iterable<ResearcherDTO>> getCoAuthors(@PathVariable long id){
Research research = researchesServ.getResearchById(id);
@ -133,6 +157,13 @@ public class ResearchController {
return new ResponseEntity<>(toReturn,HttpStatus.OK);
}
/** post co-authors to the research
*
* @param token the Authorization header
* @param researchersId the co-authors ids
* @param id the research id
* @return the added researchers
*/
@PostMapping("/research/{id}/co-authors")
public ResponseEntity<Iterable<ResearcherDTO>> postCoAuthor(@RequestHeader("Authorization") String token,
@RequestBody Iterable<Long> researchersId,

View File

@ -1,5 +1,12 @@
package ovh.herisson.Clyde.EndPoints.ScientificPublications;
/******************************************************
* @file ResearcherController.java
* @author Bartha Maxime
* @scope Publications Scientifiques
*
* API class for the researchers
******************************************************/
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -51,7 +58,9 @@ public class ResearcherController {
Researcher posted = researchesServ.saveResearcher(researcher);
return new ResponseEntity<>(ResearcherDTO.construct(posted), HttpStatus.OK);
if (posted == null) return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ResearcherDTO.construct(posted), HttpStatus.CREATED);
}
@PatchMapping("/researcher/{id}")
@ -59,9 +68,9 @@ public class ResearcherController {
@PathVariable long id,
@RequestBody Map<String ,Object> updates){
Researcher researcher = researchesServ.getResearcherById(id); //todo check authorization j'ai pu patch sans le bon token
Researcher researcher = researchesServ.getResearcherById(id);
if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin}, token)
&& researcher == researchesServ.getResearcherByUser(authServ.getUserFromToken(token)))
|| researcher == researchesServ.getResearcherByUser(authServ.getUserFromToken(token)))
return new UnauthorizedResponse<>(null);
if (researcher == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

View File

@ -110,6 +110,8 @@ public class ResearchesService {
}
public Researcher saveResearcher(Researcher researcher) {
if (researcherRepo.findByUser(researcher.getUser()) != null) return null;
return researcherRepo.save(researcher);
}