forked from tom.biard/ScienceQuest
parent
d600565838
commit
790b121a57
@ -0,0 +1,83 @@
|
|||||||
|
package fr.iut.sciencequest.sae.controllers;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.hateoas.CollectionModel;
|
||||||
|
import org.springframework.hateoas.EntityModel;
|
||||||
|
import org.springframework.hateoas.Link;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
||||||
|
|
||||||
|
public class Controller {
|
||||||
|
protected final int PAGE_SIZE = 1;
|
||||||
|
|
||||||
|
protected <T> EntityModel<T> getSelfLinkEntityModel(T entity, String method, Object... args) {
|
||||||
|
Class<?>[] argTypes = new Class[args.length];
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
argTypes[i] = args[i].getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Link selfLink = linkTo(this.getClass(), this.getClass().getMethod(method, argTypes), args).withSelfRel();
|
||||||
|
return EntityModel.of(entity, selfLink);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
System.err.println("Method doesn't exist");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T> CollectionModel<T> getSelfLinkCollectionModel(Iterable<T> entities, String method, Object... args) {
|
||||||
|
Class<?>[] argTypes = new Class[args.length];
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
argTypes[i] = args[i].getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Link selfLink = linkTo(this.getClass(), this.getClass().getMethod(method, argTypes), args).withSelfRel();
|
||||||
|
return CollectionModel.of(entities, selfLink);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
System.err.println("Method doesn't exist");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Integer page, String method, Object... args) {
|
||||||
|
try {
|
||||||
|
List<EntityModel<T>> entities = pagedResult.map(EntityModel::of).toList();
|
||||||
|
|
||||||
|
Class<?>[] argTypes = new Class[args.length+1];
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
argTypes[i] = args[i].getClass();
|
||||||
|
}
|
||||||
|
argTypes[args.length] = Optional.class;
|
||||||
|
|
||||||
|
Method finalMethod = this.getClass().getMethod(method, argTypes);
|
||||||
|
|
||||||
|
List<Object> selfObj = new ArrayList<>(List.of(args)); selfObj.add(page);
|
||||||
|
Link selfLink = linkTo(this.getClass(), finalMethod, selfObj.toArray()).withSelfRel().expand(page);
|
||||||
|
|
||||||
|
CollectionModel<EntityModel<T>> result = CollectionModel.of(entities, selfLink);
|
||||||
|
|
||||||
|
if (pagedResult.hasPrevious()) {
|
||||||
|
List<Object> previousObj = new ArrayList<>(List.of(args)); previousObj.add(pagedResult.previousPageable().getPageNumber());
|
||||||
|
result.add(linkTo(this.getClass(), finalMethod, previousObj.toArray()).withRel("previous"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pagedResult.hasNext()) {
|
||||||
|
List<Object> nextObj = new ArrayList<>(List.of(args)); nextObj.add(pagedResult.nextPageable().getPageNumber());
|
||||||
|
result.add(linkTo(this.getClass(), finalMethod, nextObj.toArray()).withRel("next"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch(NoSuchMethodException e) {
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,97 +1,61 @@
|
|||||||
package fr.iut.sciencequest.sae.controllers;
|
package fr.iut.sciencequest.sae.controllers;
|
||||||
|
|
||||||
import fr.iut.sciencequest.sae.entities.Scientifique;
|
import fr.iut.sciencequest.sae.entities.scientifique.Scientifique;
|
||||||
import fr.iut.sciencequest.sae.entities.indice.IIndiceidAndLibelleOnlyProjection;
|
import fr.iut.sciencequest.sae.entities.indice.IIndiceidAndLibelleAndScientifiqueIdOnlyProjection;
|
||||||
import fr.iut.sciencequest.sae.entities.indice.Indice;
|
import fr.iut.sciencequest.sae.entities.indice.Indice;
|
||||||
import fr.iut.sciencequest.sae.entities.indice.IValidateOnlyLibelle;
|
import fr.iut.sciencequest.sae.entities.indice.IValidateOnlyLibelle;
|
||||||
import fr.iut.sciencequest.sae.exceptions.IncorrectPageException;
|
import fr.iut.sciencequest.sae.exceptions.IncorrectPageException;
|
||||||
import fr.iut.sciencequest.sae.services.IndiceService;
|
import fr.iut.sciencequest.sae.services.IndiceService;
|
||||||
import fr.iut.sciencequest.sae.services.interfaces.IScientifiqueService;
|
import fr.iut.sciencequest.sae.services.interfaces.IScientifiqueService;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.projection.ProjectionFactory;
|
||||||
|
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||||
import org.springframework.hateoas.CollectionModel;
|
import org.springframework.hateoas.CollectionModel;
|
||||||
import org.springframework.hateoas.EntityModel;
|
import org.springframework.hateoas.EntityModel;
|
||||||
import org.springframework.hateoas.Link;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
||||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/scientifiques")
|
@RequestMapping("/api/v1/scientifiques")
|
||||||
public class ScientifiqueController {
|
public class ScientifiqueController extends Controller {
|
||||||
|
|
||||||
private final IScientifiqueService scientifiqueService;
|
private final IScientifiqueService scientifiqueService;
|
||||||
private final IndiceService indiceService;
|
private final IndiceService indiceService;
|
||||||
|
|
||||||
private final int PAGE_SIZE = 1;
|
|
||||||
|
|
||||||
|
|
||||||
public ScientifiqueController(IScientifiqueService scientifiqueService, IndiceService indiceService) {
|
public ScientifiqueController(IScientifiqueService scientifiqueService, IndiceService indiceService) {
|
||||||
this.scientifiqueService = scientifiqueService;
|
this.scientifiqueService = scientifiqueService;
|
||||||
this.indiceService = indiceService;
|
this.indiceService = indiceService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Integer page, Method method, Object... args) {
|
|
||||||
List<EntityModel<T>> entities = pagedResult.map(EntityModel::of).toList();
|
|
||||||
|
|
||||||
List<Object> selfObj = new ArrayList<>(List.of(args)); selfObj.add(page);
|
|
||||||
Link selfLink = linkTo(ScientifiqueController.class, method, selfObj.toArray()).withSelfRel().expand(page);
|
|
||||||
|
|
||||||
CollectionModel<EntityModel<T>> result = CollectionModel.of(entities, selfLink);
|
|
||||||
|
|
||||||
if (pagedResult.hasPrevious()) {
|
|
||||||
List<Object> previousObj = new ArrayList<>(List.of(args)); previousObj.add(pagedResult.previousPageable().getPageNumber());
|
|
||||||
result.add(linkTo(ScientifiqueController.class, method, previousObj.toArray()).withRel("previous"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pagedResult.hasNext()) {
|
|
||||||
List<Object> nextObj = new ArrayList<>(List.of(args)); nextObj.add(pagedResult.nextPageable().getPageNumber());
|
|
||||||
result.add(linkTo(ScientifiqueController.class, method, nextObj.toArray()).withRel("next"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
|
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ResponseBody
|
|
||||||
public CollectionModel<EntityModel<Scientifique>> getAllScientists(@RequestParam(name = "page") Optional<Integer> page) {
|
public CollectionModel<EntityModel<Scientifique>> getAllScientists(@RequestParam(name = "page") Optional<Integer> page) {
|
||||||
try {
|
try {
|
||||||
Page<Scientifique> pagedResult = this.scientifiqueService.findAll(page.orElse(0));
|
return getPageableCollectionModel(this.scientifiqueService.findAll(page.orElse(0)), page.orElse(0),"getAllScientists");
|
||||||
return getPageableCollectionModel(pagedResult, page.orElse(0), ScientifiqueController.class.getMethod("getAllScientists", Optional.class));
|
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
throw new IncorrectPageException("numéro de page incorrect");
|
throw new IncorrectPageException("numéro de page incorrect");
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ResponseBody
|
public EntityModel<Scientifique> getScientistById(@PathVariable Integer id) {
|
||||||
public EntityModel<Scientifique> getScientistById(@PathVariable int id) {
|
return getSelfLinkEntityModel(this.scientifiqueService.findById(id), "getScientistById", 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)
|
@GetMapping(value="/{id}/indices", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ResponseBody
|
public CollectionModel<IIndiceidAndLibelleAndScientifiqueIdOnlyProjection> getScientistHints(@PathVariable Integer id) {
|
||||||
public CollectionModel<IIndiceidAndLibelleOnlyProjection> getScientistHints(@PathVariable int id) {
|
return getSelfLinkCollectionModel(this.scientifiqueService.getLinkedIndicesByScientifiqueId(id), "getScientistHints", 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)
|
@PostMapping(value="/{id}/indices", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public Indice postIndice(@PathVariable int id, @Validated(IValidateOnlyLibelle.class) @RequestBody Indice indice){
|
public IIndiceidAndLibelleAndScientifiqueIdOnlyProjection postIndice(@PathVariable Integer id, @Validated(IValidateOnlyLibelle.class) @RequestBody Indice indice){
|
||||||
indice.setScientifique(this.scientifiqueService.findById(id));
|
indice.setScientifique(this.scientifiqueService.findById(id));
|
||||||
return this.indiceService.create(indice);
|
indice = this.indiceService.create(indice);
|
||||||
|
return indice.toProjection(IIndiceidAndLibelleAndScientifiqueIdOnlyProjection.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut.sciencequest.sae.entities;
|
||||||
|
|
||||||
|
import org.springframework.data.projection.ProjectionFactory;
|
||||||
|
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||||
|
|
||||||
|
public abstract class BaseEntity implements IToProjection {
|
||||||
|
public <T> T toProjection(Class<T> projectionType){
|
||||||
|
ProjectionFactory pf = new SpelAwareProxyProjectionFactory();
|
||||||
|
return pf.createProjection(projectionType, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package fr.iut.sciencequest.sae.entities;
|
||||||
|
|
||||||
|
public interface IToProjection {
|
||||||
|
public <T> T toProjection(Class<T> projectionType);
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package fr.iut.sciencequest.sae.entities.indice;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.scientifique.IScientifiqueIdOnlyProjection;
|
||||||
|
|
||||||
|
public interface IIndiceidAndLibelleAndScientifiqueIdOnlyProjection {
|
||||||
|
int getId();
|
||||||
|
String getLibelle();
|
||||||
|
|
||||||
|
IScientifiqueIdOnlyProjection getScientifique();
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
package fr.iut.sciencequest.sae.entities.indice;
|
|
||||||
|
|
||||||
public interface IIndiceidAndLibelleOnlyProjection {
|
|
||||||
int getId();
|
|
||||||
String getLibelle();
|
|
||||||
}
|
|
@ -0,0 +1,5 @@
|
|||||||
|
package fr.iut.sciencequest.sae.entities.scientifique;
|
||||||
|
|
||||||
|
public interface IScientifiqueIdOnlyProjection {
|
||||||
|
int getId();
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services;
|
||||||
|
|
||||||
|
public class PartieService {
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Question;
|
||||||
|
import fr.iut.sciencequest.sae.repositories.QuestionRepository;
|
||||||
|
import fr.iut.sciencequest.sae.services.interfaces.IQuestionService;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class QuestionService implements IQuestionService {
|
||||||
|
private static final int PAGE_SIZE = 1;
|
||||||
|
private final QuestionRepository questionRepository;
|
||||||
|
|
||||||
|
public QuestionService(QuestionRepository questionRepository) {
|
||||||
|
this.questionRepository = questionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<Question> findAll(Integer page) {
|
||||||
|
Pageable paging = PageRequest.of(page, PAGE_SIZE);
|
||||||
|
return questionRepository.findAll(paging);
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
package fr.iut.sciencequest.sae.services.interfaces;
|
package fr.iut.sciencequest.sae.services.interfaces;
|
||||||
|
|
||||||
import fr.iut.sciencequest.sae.entities.indice.IIndiceidAndLibelleOnlyProjection;
|
import fr.iut.sciencequest.sae.entities.indice.IIndiceidAndLibelleAndScientifiqueIdOnlyProjection;
|
||||||
import fr.iut.sciencequest.sae.entities.indice.Indice;
|
import fr.iut.sciencequest.sae.entities.indice.Indice;
|
||||||
|
|
||||||
public interface IIndiceService {
|
public interface IIndiceService {
|
||||||
Iterable<IIndiceidAndLibelleOnlyProjection> findByScientifiqueId(int id);
|
Iterable<IIndiceidAndLibelleAndScientifiqueIdOnlyProjection> findByScientifiqueId(int id);
|
||||||
Indice update(Indice indice);
|
Indice update(Indice indice);
|
||||||
Indice create(Indice indice);
|
Indice create(Indice indice);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services.interfaces;
|
||||||
|
|
||||||
|
public class IPartieService {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services.interfaces;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Question;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
|
public interface IQuestionService {
|
||||||
|
Page<Question> findAll(Integer page);
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Main-Class: fr.iut.sciencequest.sae.SaeApplication
|
||||||
|
|
Loading…
Reference in new issue