jour 5 -- début journée

Springboot
Victor SOULIER 1 year ago
parent 488d30a41c
commit 81a33c9460

@ -0,0 +1,23 @@
package fr.iut.sciencequest.sae.assemblers;
import fr.iut.sciencequest.sae.dto.thematique.ThematiqueSimpleDTO;
import fr.iut.sciencequest.sae.entities.Thematique;
import jakarta.annotation.Nullable;
import org.modelmapper.ModelMapper;
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
@Component
public class ThematiqueSimpleModelAssembler extends RepresentationModelAssemblerSupport<Thematique, ThematiqueSimpleDTO> {
public ThematiqueSimpleModelAssembler() {
super(Thematique.class, ThematiqueSimpleDTO.class);
}
@Override
@NonNull
public ThematiqueSimpleDTO toModel(@Nullable Thematique entity) {
ModelMapper mapper = new ModelMapper();
return mapper.map(entity, ThematiqueSimpleDTO.class);
}
}

@ -26,13 +26,11 @@ public class QuestionController extends Controller {
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public PagedModel<QuestionDTO> getAllQuestions(@PageableDefault(size = ApplicationConfig.DEFAULT_PAGEABLE_SIZE) Pageable p) {
try {
Page<Question> questionPage = questionService.findAll(p);
return pagedResourcesAssembler.toModel(questionPage, questionModelAssembler);
} catch (IllegalArgumentException e) {
throw new IncorrectPageException("numéro de page incorrect");
}
public PagedModel<QuestionDTO> getAllQuestions(@PageableDefault(size = ApplicationConfig.DEFAULT_PAGEABLE_SIZE) Pageable p/*,
@RequestParam(value = "scientifiqueId", defaultValue = "-1") Integer scientifiqueId*/) {
//Page<Question> questionPage = (scientifiqueId.equals(-1) ? questionService.findAll(p) : questionService.findAll(p)); //TEMPORAIRE NE PAS ENLEVER
return pagedResourcesAssembler.toModel(questionService.findAll(p), questionModelAssembler);
}
}

@ -1,13 +1,23 @@
package fr.iut.sciencequest.sae.controllers;
import fr.iut.sciencequest.sae.ApplicationConfig;
import fr.iut.sciencequest.sae.assemblers.IndiceModelAssembler;
import fr.iut.sciencequest.sae.assemblers.ThematiqueModelAssembler;
import fr.iut.sciencequest.sae.assemblers.ThematiqueSimpleModelAssembler;
import fr.iut.sciencequest.sae.dto.thematique.ThematiqueDTO;
import fr.iut.sciencequest.sae.dto.thematique.ThematiqueSimpleDTO;
import fr.iut.sciencequest.sae.entities.Difficulte;
import fr.iut.sciencequest.sae.entities.Thematique;
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.PagedModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@ -19,11 +29,14 @@ import org.springframework.web.bind.annotation.*;
public class ThematiqueController extends Controller {
private final IThematiqueService thematiqueService;
private final ThematiqueModelAssembler thematiqueModelAssembler;
private final ThematiqueSimpleModelAssembler thematiqueSimpleModelAssembler;
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
private final PagedResourcesAssembler<Thematique> pagedResourcesAssembler;
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public CollectionModel<Thematique> getAllThematiques() {
return getSelfLinkCollectionModel(this.thematiqueService.findAll(), "getAllThematiques");
public PagedModel<ThematiqueSimpleDTO> getAllThematiques(@PageableDefault(size = ApplicationConfig.DEFAULT_PAGEABLE_SIZE) Pageable p) {
return pagedResourcesAssembler.toModel(this.thematiqueService.findAll(p), thematiqueSimpleModelAssembler);
}
//TODO : gestion des erreurs remontées par @Valid

@ -5,7 +5,9 @@ import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ -18,10 +20,10 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
public class ValidationHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
@Nullable
@Override
@ResponseStatus(HttpStatus.BAD_REQUEST)
protected ResponseEntity<Object> hand(MethodArgumentNotValidException ex) {
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) ->{

@ -4,5 +4,10 @@ import fr.iut.sciencequest.sae.entities.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface QuestionRepository extends JpaRepository<Question, Integer> {}
public interface QuestionRepository extends JpaRepository<Question, Integer> {
}

@ -1,8 +1,13 @@
package fr.iut.sciencequest.sae.repositories;
import fr.iut.sciencequest.sae.entities.Question;
import fr.iut.sciencequest.sae.entities.Reponse;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ReponseRepository extends CrudRepository<Reponse, Integer> {}
public interface ReponseRepository extends CrudRepository<Reponse, Integer> {
}

@ -1,12 +1,13 @@
package fr.iut.sciencequest.sae.repositories;
import fr.iut.sciencequest.sae.entities.Thematique;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ThematiqueRepository extends CrudRepository<Thematique, Integer> {
public interface ThematiqueRepository extends JpaRepository<Thematique, Integer> {
boolean existsByLibelle(String libelle);
Thematique findThematiqueByLibelleEqualsIgnoreCase(String thematique);
}

@ -7,6 +7,8 @@ import fr.iut.sciencequest.sae.repositories.IndiceRepository;
import fr.iut.sciencequest.sae.services.interfaces.IIndiceService;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
@Service
public class IndiceService implements IIndiceService {
@ -36,4 +38,18 @@ public class IndiceService implements IIndiceService {
}
return this.indiceRepository.save(indice);
}
public Indice patch(Indice incompleteIndice, Indice destIndice) throws IllegalAccessException {
Class<?> indiceClass = Indice.class;
Field[] indiceFields = indiceClass.getDeclaredFields();
for(Field field: indiceFields){
field.setAccessible(true);
Object value = field.get(incompleteIndice);
if(value != null){
field.set(destIndice, value);
}
field.setAccessible(false);
}
return destIndice;
}
}

@ -6,17 +6,16 @@ import fr.iut.sciencequest.sae.exceptions.DuplicatedIdException;
import fr.iut.sciencequest.sae.exceptions.notFound.ThematiqueNotFoundException;
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@AllArgsConstructor
@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");
@ -41,8 +40,8 @@ public class ThematiqueService implements IThematiqueService {
}
@Override
public Iterable<Thematique> findAll(){
return this.thematiqueRepository.findAll();
public Page<Thematique> findAll(Pageable p){
return this.thematiqueRepository.findAll(p);
}
}

@ -2,11 +2,13 @@ package fr.iut.sciencequest.sae.services.interfaces;
import fr.iut.sciencequest.sae.entities.Thematique;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface IThematiqueService {
Thematique update(Thematique thematique);
Thematique create(Thematique thematique);
Iterable<Thematique> findAll();
Page<Thematique> findAll(Pageable p);
}

Loading…
Cancel
Save