Jour 3 - pause midi -

Springboot
Alix JEUDI--LEMOINE 1 year ago
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();
}
}

@ -1,7 +1,5 @@
package fr.iut.sciencequest.sae.controllers;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import fr.iut.sciencequest.sae.entities.Question;
import fr.iut.sciencequest.sae.exceptions.IncorrectPageException;
import fr.iut.sciencequest.sae.repositories.QuestionRepository;
@ -13,13 +11,15 @@ import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
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;
@Controller
@RestController
@RequestMapping("/api/v1/questions")
public class QuestionController {

@ -1,14 +1,12 @@
package fr.iut.sciencequest.sae.controllers;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
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.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.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
@ -25,25 +23,27 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
@RestController
@RequestMapping("/api/v1/scientifiques")
public class ScientifiqueController {
private static final int PAGE_SIZE = 1;
private final ScientifiqueRepository scientifiqueRepository;
private final IScientifiqueService scientifiqueService;
private final IndiceRepository indiceRepository;
public ScientifiqueController(ScientifiqueRepository scientifiqueRepository, IndiceRepository indiceRepository) {
this.scientifiqueRepository = scientifiqueRepository;
private final int PAGE_SIZE = 1;
public ScientifiqueController(IScientifiqueService scientifiqueService, IndiceRepository indiceRepository) {
this.scientifiqueService = scientifiqueService;
this.indiceRepository = indiceRepository;
}
private <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Optional<Integer> page, Method method, Object... args) {
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.orElse(0));
Link selfLink = linkTo(ScientifiqueController.class, method, selfObj.toArray()).withSelfRel().expand(page.orElse(0));
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);
@ -63,41 +63,30 @@ public class ScientifiqueController {
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CollectionModel<EntityModel<Scientifique>> getAllScientists(@RequestParam(name = "page") Optional<Integer> page) {
try {
Pageable paging = PageRequest.of(page.orElse(0), 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);
}
return this.scientifiqueService.findAll(page.orElse(0));
}
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public EntityModel<Optional<Scientifique>> getScientistById(@PathVariable 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);
return this.scientifiqueService.findById(id);
}
@GetMapping(value="/{id}/indices", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public CollectionModel<EntityModel<Indice>> getScientistHints(@PathVariable int id, @RequestParam(name = "page") Optional<Integer> page) {
try {
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 getPageableCollectionModel(pagedResult, page, ScientifiqueController.class.getMethod("getScientistHints", int.class, Optional.class), id);
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);
}
}*/
}
@PostMapping(value="/{id}/indices", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

@ -1,7 +1,7 @@
package fr.iut.sciencequest.sae.controllers;
import fr.iut.sciencequest.sae.entities.Thematique;
import fr.iut.sciencequest.sae.services.ThematiqueService;
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@ -10,10 +10,9 @@ import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1/thematiques")
public class ThematiqueController {
private final IThematiqueService thematiqueService;
private final ThematiqueService thematiqueService;
public ThematiqueController(ThematiqueService thematiqueService) {
public ThematiqueController(IThematiqueService thematiqueService) {
this.thematiqueService = thematiqueService;
}
@ -23,7 +22,6 @@ public class ThematiqueController {
return this.thematiqueService.findAll();
}
//TODO : gestion des erreurs remontées par @Valid
//TODO : ajouter liens hateos
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)

@ -0,0 +1,6 @@
package fr.iut.sciencequest.sae.entities;
public interface IidAndLibelleOnly{
int getId();
String getLibelle();
}

@ -22,6 +22,8 @@ public class Indice {
private String libelle;
@NotNull
@Column(name="idscientifique")
private int idScientifique;
@JoinColumn(name="idscientifique")
@ManyToOne
private Scientifique scientifique;
}

@ -1,5 +1,6 @@
package fr.iut.sciencequest.sae.repositories;
import fr.iut.sciencequest.sae.entities.IidAndLibelleOnly;
import fr.iut.sciencequest.sae.entities.Indice;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
@ -7,5 +8,6 @@ import org.springframework.stereotype.Repository;
@Repository
public interface IndiceRepository extends PagingAndSortingRepository<Indice, Integer>, CrudRepository<Indice, Integer> {
Iterable<IidAndLibelleOnly> findByScientifiqueId(int id);
}

@ -6,4 +6,5 @@ import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ScientifiqueRepository extends PagingAndSortingRepository<Scientifique, Integer>, CrudRepository<Scientifique, Integer> {}
public interface ScientifiqueRepository extends PagingAndSortingRepository<Scientifique, Integer>, CrudRepository<Scientifique, Integer> {
}

@ -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;
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 {
public Thematique update(Thematique thematique);
@Override
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…
Cancel
Save