forked from tom.biard/ScienceQuest
parent
37ac092c55
commit
d284ee23fa
@ -0,0 +1,29 @@
|
|||||||
|
package fr.iut.sciencequest.sae.controllers;
|
||||||
|
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Difficulte;
|
||||||
|
import fr.iut.sciencequest.sae.services.DifficulteService;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/difficultes")
|
||||||
|
public class DifficulteController {
|
||||||
|
|
||||||
|
public final DifficulteService difficulteService;
|
||||||
|
|
||||||
|
public DifficulteController(DifficulteService difficulteService){
|
||||||
|
this.difficulteService = difficulteService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
public Iterable<Difficulte> getAllDifficultes(){
|
||||||
|
return this.difficulteService.findAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package fr.iut.sciencequest.sae.entities;
|
||||||
|
|
||||||
|
public interface IidAndLibelleOnly{
|
||||||
|
int getId();
|
||||||
|
String getLibelle();
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Difficulte;
|
||||||
|
import fr.iut.sciencequest.sae.repositories.DifficulteRepository;
|
||||||
|
import fr.iut.sciencequest.sae.services.interfaces.IDifficulteService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DifficulteService implements IDifficulteService {
|
||||||
|
private final DifficulteRepository difficulteRepository;
|
||||||
|
|
||||||
|
public DifficulteService(DifficulteRepository difficulteRepository){
|
||||||
|
this.difficulteRepository = difficulteRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Difficulte> findAll(){
|
||||||
|
return this.difficulteRepository.findAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.controllers.ScientifiqueController;
|
||||||
|
import fr.iut.sciencequest.sae.entities.Scientifique;
|
||||||
|
import fr.iut.sciencequest.sae.exceptions.IncorrectPageException;
|
||||||
|
import fr.iut.sciencequest.sae.exceptions.ScientifiqueNotFoundException;
|
||||||
|
import fr.iut.sciencequest.sae.repositories.IndiceRepository;
|
||||||
|
import fr.iut.sciencequest.sae.repositories.ScientifiqueRepository;
|
||||||
|
import fr.iut.sciencequest.sae.services.interfaces.IScientifiqueService;
|
||||||
|
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.stereotype.Service;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ScientifiqueService implements IScientifiqueService {
|
||||||
|
|
||||||
|
private static final int PAGE_SIZE = 1;
|
||||||
|
|
||||||
|
private final ScientifiqueRepository scientifiqueRepository;
|
||||||
|
|
||||||
|
public ScientifiqueService(ScientifiqueRepository scientifiqueRepository) {
|
||||||
|
this.scientifiqueRepository = scientifiqueRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Scientifique update(Scientifique scientifique) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Scientifique create(Scientifique scientifique) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CollectionModel<EntityModel<Scientifique>> findAll(Integer page) {
|
||||||
|
try {
|
||||||
|
Pageable paging = PageRequest.of(page, PAGE_SIZE);
|
||||||
|
Page<Scientifique> pagedResult = scientifiqueRepository.findAll(paging);
|
||||||
|
|
||||||
|
return getPageableCollectionModel(pagedResult, page, ScientifiqueController.class.getMethod("getAllScientists", Optional.class));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new IncorrectPageException("numéro de page incorrect");
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EntityModel<Optional<Scientifique>> findById(int id) {
|
||||||
|
Optional<Scientifique> scientifiqueOptional = this.scientifiqueRepository.findById(id);
|
||||||
|
Scientifique scientifique = scientifiqueOptional.orElseThrow(() -> new ScientifiqueNotFoundException("Scientifique non trouvé avec l'ID : " + id));
|
||||||
|
|
||||||
|
Link selfLink = linkTo(methodOn(ScientifiqueController.class).getScientistById(id)).withSelfRel();
|
||||||
|
return EntityModel.of(Optional.ofNullable(scientifique), selfLink);
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,48 @@
|
|||||||
package fr.iut.sciencequest.sae.services;
|
package fr.iut.sciencequest.sae.services;
|
||||||
|
|
||||||
|
|
||||||
import fr.iut.sciencequest.sae.entities.Thematique;
|
import fr.iut.sciencequest.sae.entities.Thematique;
|
||||||
|
import fr.iut.sciencequest.sae.exceptions.DuplicatedFieldException;
|
||||||
|
import fr.iut.sciencequest.sae.exceptions.DuplicatedIdException;
|
||||||
|
import fr.iut.sciencequest.sae.exceptions.EntityNotFoundException;
|
||||||
|
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
|
||||||
|
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ThematiqueService implements IThematiqueService {
|
||||||
|
|
||||||
|
private final ThematiqueRepository thematiqueRepository;
|
||||||
|
|
||||||
|
public ThematiqueService(ThematiqueRepository thematiqueRepository){
|
||||||
|
this.thematiqueRepository = thematiqueRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkFieldsConstraints(Thematique thematique){
|
||||||
|
if(this.thematiqueRepository.existsByLibelle(thematique.getLibelle())){
|
||||||
|
throw new DuplicatedFieldException("libelle");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Thematique update(Thematique thematique){
|
||||||
|
if(!this.thematiqueRepository.existsById(thematique.getId())){
|
||||||
|
throw new EntityNotFoundException();
|
||||||
|
}
|
||||||
|
this.checkFieldsConstraints(thematique);
|
||||||
|
return this.thematiqueRepository.save(thematique);
|
||||||
|
}
|
||||||
|
|
||||||
public interface ThematiqueService {
|
@Override
|
||||||
public Thematique update(Thematique thematique);
|
public Thematique create(Thematique thematique){
|
||||||
|
if(this.thematiqueRepository.existsById(thematique.getId())){
|
||||||
|
throw new DuplicatedIdException();
|
||||||
|
}
|
||||||
|
this.checkFieldsConstraints(thematique);
|
||||||
|
return this.thematiqueRepository.save(thematique);
|
||||||
|
}
|
||||||
|
|
||||||
public Thematique create(Thematique thematique);
|
@Override
|
||||||
|
public Iterable<Thematique> findAll(){
|
||||||
|
return this.thematiqueRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
public Iterable<Thematique> findAll();
|
|
||||||
}
|
}
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
package fr.iut.sciencequest.sae.services;
|
|
||||||
|
|
||||||
import fr.iut.sciencequest.sae.entities.Thematique;
|
|
||||||
import fr.iut.sciencequest.sae.exceptions.DuplicatedFieldException;
|
|
||||||
import fr.iut.sciencequest.sae.exceptions.DuplicatedIdException;
|
|
||||||
import fr.iut.sciencequest.sae.exceptions.EntityNotFoundException;
|
|
||||||
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class ThematiqueServiceImpl implements ThematiqueService{
|
|
||||||
|
|
||||||
private final ThematiqueRepository thematiqueRepository;
|
|
||||||
|
|
||||||
public ThematiqueServiceImpl(ThematiqueRepository thematiqueRepository){
|
|
||||||
this.thematiqueRepository = thematiqueRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkFieldsConstraints(Thematique thematique){
|
|
||||||
if(this.thematiqueRepository.existsByLibelle(thematique.getLibelle())){
|
|
||||||
throw new DuplicatedFieldException("libelle");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Thematique update(Thematique thematique){
|
|
||||||
if(!this.thematiqueRepository.existsById(thematique.getId())){
|
|
||||||
throw new EntityNotFoundException();
|
|
||||||
}
|
|
||||||
this.checkFieldsConstraints(thematique);
|
|
||||||
return this.thematiqueRepository.save(thematique);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Thematique create(Thematique thematique){
|
|
||||||
if(this.thematiqueRepository.existsById(thematique.getId())){
|
|
||||||
throw new DuplicatedIdException();
|
|
||||||
}
|
|
||||||
this.checkFieldsConstraints(thematique);
|
|
||||||
return this.thematiqueRepository.save(thematique);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterable<Thematique> findAll(){
|
|
||||||
return this.thematiqueRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services.interfaces;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Difficulte;
|
||||||
|
|
||||||
|
public interface IDifficulteService {
|
||||||
|
Iterable<Difficulte> findAll();
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services.interfaces;
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Scientifique;
|
||||||
|
import org.springframework.hateoas.CollectionModel;
|
||||||
|
import org.springframework.hateoas.EntityModel;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface IScientifiqueService {
|
||||||
|
Scientifique update(Scientifique scientifique);
|
||||||
|
|
||||||
|
Scientifique create(Scientifique scientifique);
|
||||||
|
|
||||||
|
CollectionModel<EntityModel<Scientifique>> findAll(Integer page);
|
||||||
|
|
||||||
|
EntityModel<Optional<Scientifique>> findById(int id);
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services.interfaces;
|
||||||
|
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Thematique;
|
||||||
|
|
||||||
|
public interface IThematiqueService {
|
||||||
|
Thematique update(Thematique thematique);
|
||||||
|
|
||||||
|
Thematique create(Thematique thematique);
|
||||||
|
|
||||||
|
Iterable<Thematique> findAll();
|
||||||
|
}
|
Loading…
Reference in new issue