Springboot
Alix JEUDI--LEMOINE 1 year ago
parent 67ad1764a7
commit 600945bb16

@ -20,6 +20,10 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -37,32 +41,39 @@ public class ScientifiqueController {
this.indiceRepository = indiceRepository; this.indiceRepository = indiceRepository;
} }
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) private <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Optional<Integer> page, Method method, Object... args) {
@ResponseBody List<EntityModel<T>> entities = pagedResult.map(EntityModel::of).toList();
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);
List<EntityModel<Scientifique>> scientifiques = pagedResult.map(EntityModel::of).toList();
Link selfLink = linkTo(methodOn(ScientifiqueController.class).getAllScientists(page)).withSelfRel().expand(page.map(Object::toString).orElse("0")); 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));
CollectionModel<EntityModel<Scientifique>> result = CollectionModel.of(scientifiques, selfLink); CollectionModel<EntityModel<T>> result = CollectionModel.of(entities, selfLink);
if (pagedResult.hasPrevious()) { if (pagedResult.hasPrevious()) {
Link prevLink = linkTo(methodOn(ScientifiqueController.class).getAllScientists(Optional.of(pagedResult.previousPageable().getPageNumber()))).withRel("previous"); List<Object> previousObj = new ArrayList<>(List.of(args)); previousObj.add(pagedResult.previousPageable().getPageNumber());
result.add(prevLink.expand(pagedResult.previousPageable().getPageNumber())); result.add(linkTo(ScientifiqueController.class, method, previousObj.toArray()).withRel("previous"));
} }
if (pagedResult.hasNext()) { if (pagedResult.hasNext()) {
Link nextLink = linkTo(methodOn(ScientifiqueController.class).getAllScientists(Optional.of(pagedResult.nextPageable().getPageNumber()))).withRel("next"); List<Object> nextObj = new ArrayList<>(List.of(args)); nextObj.add(pagedResult.nextPageable().getPageNumber());
result.add(nextLink.expand(pagedResult.nextPageable().getPageNumber())); result.add(linkTo(ScientifiqueController.class, method, nextObj.toArray()).withRel("next"));
} }
return result; return result;
}
@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) { } 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);
} }
} }
@ -83,25 +94,11 @@ public class ScientifiqueController {
Pageable paging = PageRequest.of(page.orElse(0), PAGE_SIZE); Pageable paging = PageRequest.of(page.orElse(0), PAGE_SIZE);
Page<Indice> pagedResult = indiceRepository.findAll(paging); Page<Indice> pagedResult = indiceRepository.findAll(paging);
List<EntityModel<Indice>> indices = pagedResult.map(EntityModel::of).toList(); return getPageableCollectionModel(pagedResult, page, ScientifiqueController.class.getMethod("getScientistHints", int.class, Optional.class), id);
Link selfLink = linkTo(methodOn(ScientifiqueController.class).getScientistHints(id, page)).withSelfRel().expand(page.map(Object::toString).orElse("0"));
CollectionModel<EntityModel<Indice>> result = CollectionModel.of(indices, selfLink);
if (pagedResult.hasPrevious()) {
Link prevLink = linkTo(methodOn(ScientifiqueController.class).getScientistHints(id, Optional.of(pagedResult.previousPageable().getPageNumber()))).withRel("previous");
result.add(prevLink.expand(pagedResult.previousPageable().getPageNumber()));
}
if (pagedResult.hasNext()) {
Link nextLink = linkTo(methodOn(ScientifiqueController.class).getScientistHints(id, Optional.of(pagedResult.nextPageable().getPageNumber()))).withRel("next");
result.add(nextLink.expand(pagedResult.nextPageable().getPageNumber()));
}
return result;
} 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);
} }
} }

@ -4,11 +4,22 @@ import fr.iut.sciencequest.sae.entities.Thematique;
import fr.iut.sciencequest.sae.exceptions.DuplicatedEntity; import fr.iut.sciencequest.sae.exceptions.DuplicatedEntity;
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository; import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Valid;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/api/v1/thematiques") @RequestMapping("/api/v1/thematiques")
public class ThematiqueController { public class ThematiqueController {
@ -18,6 +29,17 @@ public class ThematiqueController {
public ThematiqueController(ThematiqueRepository thematiqueRepository) { public ThematiqueController(ThematiqueRepository thematiqueRepository) {
this.thematiqueRepository = thematiqueRepository; this.thematiqueRepository = thematiqueRepository;
} }
/*
@ExceptionHandler(ConstraintViolationException.class)
public void t(ConstraintViolationException e){
throw new RuntimeException();
}*/
@ExceptionHandler(org.hibernate.exception.ConstraintViolationException.class)
public void
handleConstraintViolationException(ConstraintViolationException ex){
System.out.println("o ?");
}
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ -27,14 +49,8 @@ public class ThematiqueController {
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public Thematique postThematique(@RequestBody Thematique thematique, HttpServletRequest request){ public Thematique postThematique(@Valid @RequestBody Thematique thematique){
if(this.thematiqueRepository.existsById(thematique.getId()))
throw new DuplicatedEntity("Une thématique avec l'ID "+thematique.getId()+" existe déjà");
try{
thematique = this.thematiqueRepository.save(thematique); thematique = this.thematiqueRepository.save(thematique);
return thematique; return thematique;
} catch (DataIntegrityViolationException e) {
throw new DuplicatedEntity("Une thématique avec le libelle : " + thematique.getLibelle() + " existe déjà");
}
} }
} }

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

@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.URL;
import java.util.Date; import java.util.Date;
@ -32,6 +33,7 @@ public class Scientifique {
@JoinColumn(name="idthematique") @JoinColumn(name="idthematique")
private Thematique thematique; private Thematique thematique;
@URL
@Column(name = "photo") @Column(name = "photo")
private String pathToPhoto; private String pathToPhoto;

@ -6,6 +6,7 @@ import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.UniqueElements;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor

@ -23,7 +23,7 @@ public class Utilisateur extends Joueur{
private String email; private String email;
@NotBlank @NotBlank
@Column(name = "motdepasse") @Column(name = "password")
private String motDePasse; private String motDePasse;
} }

@ -6,3 +6,4 @@ spring.sql.init.mode=always
spring.sql.init.schema-locations=classpath:schema.sql spring.sql.init.schema-locations=classpath:schema.sql
spring.jackson.serialization.indent_output = true spring.jackson.serialization.indent_output = true
server.error.include-message=always server.error.include-message=always
spring.jpa.open-in-view=false
Loading…
Cancel
Save