|
|
|
@ -1,21 +1,19 @@
|
|
|
|
|
package fr.iut.sciencequest.sae.controllers;
|
|
|
|
|
|
|
|
|
|
import fr.iut.sciencequest.sae.entities.IidAndLibelleOnly;
|
|
|
|
|
import fr.iut.sciencequest.sae.entities.Indice;
|
|
|
|
|
import fr.iut.sciencequest.sae.entities.Scientifique;
|
|
|
|
|
import fr.iut.sciencequest.sae.exceptions.DuplicatedFieldException;
|
|
|
|
|
import fr.iut.sciencequest.sae.entities.indice.IIndiceidAndLibelleOnlyProjection;
|
|
|
|
|
import fr.iut.sciencequest.sae.entities.indice.Indice;
|
|
|
|
|
import fr.iut.sciencequest.sae.entities.indice.IValidateOnlyLibelle;
|
|
|
|
|
import fr.iut.sciencequest.sae.exceptions.IncorrectPageException;
|
|
|
|
|
import fr.iut.sciencequest.sae.repositories.IndiceRepository;
|
|
|
|
|
import fr.iut.sciencequest.sae.services.IndiceService;
|
|
|
|
|
import fr.iut.sciencequest.sae.services.interfaces.IScientifiqueService;
|
|
|
|
|
import org.springframework.dao.DataIntegrityViolationException;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.hateoas.CollectionModel;
|
|
|
|
|
import org.springframework.hateoas.EntityModel;
|
|
|
|
|
import org.springframework.hateoas.Link;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
@ -24,19 +22,21 @@ import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
|
|
|
|
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/v1/scientifiques")
|
|
|
|
|
public class ScientifiqueController {
|
|
|
|
|
|
|
|
|
|
private final IScientifiqueService scientifiqueService;
|
|
|
|
|
private final IndiceRepository indiceRepository;
|
|
|
|
|
private final IndiceService indiceService;
|
|
|
|
|
|
|
|
|
|
private final int PAGE_SIZE = 1;
|
|
|
|
|
|
|
|
|
|
public ScientifiqueController(IScientifiqueService scientifiqueService, IndiceRepository indiceRepository) {
|
|
|
|
|
|
|
|
|
|
public ScientifiqueController(IScientifiqueService scientifiqueService, IndiceService indiceService) {
|
|
|
|
|
this.scientifiqueService = scientifiqueService;
|
|
|
|
|
this.indiceRepository = indiceRepository;
|
|
|
|
|
this.indiceService = indiceService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Integer page, Method method, Object... args) {
|
|
|
|
@ -63,40 +63,35 @@ public class ScientifiqueController {
|
|
|
|
|
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public CollectionModel<EntityModel<Scientifique>> getAllScientists(@RequestParam(name = "page") Optional<Integer> page) {
|
|
|
|
|
return this.scientifiqueService.findAll(page.orElse(0));
|
|
|
|
|
try {
|
|
|
|
|
Page<Scientifique> pagedResult = this.scientifiqueService.findAll(page.orElse(0));
|
|
|
|
|
return getPageableCollectionModel(pagedResult, page.orElse(0), ScientifiqueController.class.getMethod("getAllScientists", Optional.class));
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
throw new IncorrectPageException("numéro de page incorrect");
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public EntityModel<Optional<Scientifique>> getScientistById(@PathVariable int id) {
|
|
|
|
|
return this.scientifiqueService.findById(id);
|
|
|
|
|
public EntityModel<Scientifique> getScientistById(@PathVariable int id) {
|
|
|
|
|
Link selfLink = linkTo(methodOn(ScientifiqueController.class).getScientistById(id)).withSelfRel();
|
|
|
|
|
return EntityModel.of(this.scientifiqueService.findById(id), selfLink);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping(value="/{id}/indices", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public /*CollectionModel<EntityModel<Indice>>*/ Iterable<IidAndLibelleOnly> getScientistHints(@PathVariable int id, @RequestParam(name = "page") Optional<Integer> page) {
|
|
|
|
|
return this.indiceRepository.findByScientifiqueId(id);
|
|
|
|
|
|
|
|
|
|
/*try {
|
|
|
|
|
Pageable paging = PageRequest.of(page.orElse(0), PAGE_SIZE);
|
|
|
|
|
Page<Indice> pagedResult = indiceRepository.findAll(paging);
|
|
|
|
|
|
|
|
|
|
return this.getPageableCollectionModel(pagedResult, page.orElse(0), ScientifiqueController.class.getMethod("getScientistHints", int.class, Optional.class), id);
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
throw new IncorrectPageException("numéro de page incorrect");
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}*/
|
|
|
|
|
public CollectionModel<IIndiceidAndLibelleOnlyProjection> getScientistHints(@PathVariable int id) {
|
|
|
|
|
Link selfLink = linkTo(methodOn(ScientifiqueController.class).getScientistHints(id)).withSelfRel();
|
|
|
|
|
return CollectionModel.of(this.scientifiqueService.getLinkedIndicesByScientifiqueId(id), selfLink);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping(value="/{id}/indices", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
@ResponseStatus(HttpStatus.CREATED)
|
|
|
|
|
public Indice postIndice(@RequestBody Indice indice){
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
return this.indiceRepository.save(indice);
|
|
|
|
|
} catch (DataIntegrityViolationException e){
|
|
|
|
|
throw new DuplicatedFieldException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
public Indice postIndice(@PathVariable int id, @Validated(IValidateOnlyLibelle.class) @RequestBody Indice indice){
|
|
|
|
|
indice.setScientifique(this.scientifiqueService.findById(id));
|
|
|
|
|
return this.indiceService.create(indice);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|