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.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.Optional;
@ -37,6 +41,27 @@ public class ScientifiqueController {
this.indiceRepository = indiceRepository;
}
private <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Optional<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));
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)
@ResponseBody
public CollectionModel<EntityModel<Scientifique>> getAllScientists(@RequestParam(name = "page") Optional<Integer> page) {
@ -44,25 +69,11 @@ public class ScientifiqueController {
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"));
CollectionModel<EntityModel<Scientifique>> result = CollectionModel.of(scientifiques, selfLink);
if (pagedResult.hasPrevious()) {
Link prevLink = linkTo(methodOn(ScientifiqueController.class).getAllScientists(Optional.of(pagedResult.previousPageable().getPageNumber()))).withRel("previous");
result.add(prevLink.expand(pagedResult.previousPageable().getPageNumber()));
}
if (pagedResult.hasNext()) {
Link nextLink = linkTo(methodOn(ScientifiqueController.class).getAllScientists(Optional.of(pagedResult.nextPageable().getPageNumber()))).withRel("next");
result.add(nextLink.expand(pagedResult.nextPageable().getPageNumber()));
}
return result;
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);
}
}
@ -83,25 +94,11 @@ public class ScientifiqueController {
Pageable paging = PageRequest.of(page.orElse(0), PAGE_SIZE);
Page<Indice> pagedResult = indiceRepository.findAll(paging);
List<EntityModel<Indice>> indices = pagedResult.map(EntityModel::of).toList();
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;
return getPageableCollectionModel(pagedResult, page, 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);
}
}

@ -4,11 +4,22 @@ import fr.iut.sciencequest.sae.entities.Thematique;
import fr.iut.sciencequest.sae.exceptions.DuplicatedEntity;
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Valid;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
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 java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/v1/thematiques")
public class ThematiqueController {
@ -18,6 +29,17 @@ public class ThematiqueController {
public ThematiqueController(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)
@ResponseStatus(HttpStatus.OK)
@ -27,14 +49,8 @@ public class ThematiqueController {
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Thematique postThematique(@RequestBody Thematique thematique, HttpServletRequest request){
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);
return thematique;
} catch (DataIntegrityViolationException e) {
throw new DuplicatedEntity("Une thématique avec le libelle : " + thematique.getLibelle() + " existe déjà");
}
public Thematique postThematique(@Valid @RequestBody Thematique thematique){
thematique = this.thematiqueRepository.save(thematique);
return thematique;
}
}

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

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

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

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

@ -5,4 +5,5 @@ spring.datasource.driver-class-name=org.postgresql.Driver
spring.sql.init.mode=always
spring.sql.init.schema-locations=classpath:schema.sql
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