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

@ -1,13 +1,23 @@
package fr.iut.sciencequest.sae.controllers; 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.IndiceModelAssembler;
import fr.iut.sciencequest.sae.assemblers.ThematiqueModelAssembler; 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.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.entities.Thematique;
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService; import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.AllArgsConstructor; 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.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.PagedModel;
import org.springframework.http.HttpStatus; 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.*;
@ -19,11 +29,14 @@ import org.springframework.web.bind.annotation.*;
public class ThematiqueController extends Controller { public class ThematiqueController extends Controller {
private final IThematiqueService thematiqueService; private final IThematiqueService thematiqueService;
private final ThematiqueModelAssembler thematiqueModelAssembler; private final ThematiqueModelAssembler thematiqueModelAssembler;
private final ThematiqueSimpleModelAssembler thematiqueSimpleModelAssembler;
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
private final PagedResourcesAssembler<Thematique> pagedResourcesAssembler;
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public CollectionModel<Thematique> getAllThematiques() { public PagedModel<ThematiqueSimpleDTO> getAllThematiques(@PageableDefault(size = ApplicationConfig.DEFAULT_PAGEABLE_SIZE) Pageable p) {
return getSelfLinkCollectionModel(this.thematiqueService.findAll(), "getAllThematiques"); return pagedResourcesAssembler.toModel(this.thematiqueService.findAll(p), thematiqueSimpleModelAssembler);
} }
//TODO : gestion des erreurs remontées par @Valid //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.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice; 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 { public class ValidationHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class) @Nullable
@Override
@ResponseStatus(HttpStatus.BAD_REQUEST) @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<>(); Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) ->{ 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.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @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; package fr.iut.sciencequest.sae.repositories;
import fr.iut.sciencequest.sae.entities.Question;
import fr.iut.sciencequest.sae.entities.Reponse; import fr.iut.sciencequest.sae.entities.Reponse;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @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; package fr.iut.sciencequest.sae.repositories;
import fr.iut.sciencequest.sae.entities.Thematique; import fr.iut.sciencequest.sae.entities.Thematique;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface ThematiqueRepository extends CrudRepository<Thematique, Integer> { public interface ThematiqueRepository extends JpaRepository<Thematique, Integer> {
boolean existsByLibelle(String libelle); boolean existsByLibelle(String libelle);
Thematique findThematiqueByLibelleEqualsIgnoreCase(String thematique); Thematique findThematiqueByLibelleEqualsIgnoreCase(String thematique);
} }

@ -7,6 +7,8 @@ import fr.iut.sciencequest.sae.repositories.IndiceRepository;
import fr.iut.sciencequest.sae.services.interfaces.IIndiceService; import fr.iut.sciencequest.sae.services.interfaces.IIndiceService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
@Service @Service
public class IndiceService implements IIndiceService { public class IndiceService implements IIndiceService {
@ -36,4 +38,18 @@ public class IndiceService implements IIndiceService {
} }
return this.indiceRepository.save(indice); 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.exceptions.notFound.ThematiqueNotFoundException;
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository; import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService; 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; import org.springframework.stereotype.Service;
@AllArgsConstructor
@Service @Service
public class ThematiqueService implements IThematiqueService { public class ThematiqueService implements IThematiqueService {
private final ThematiqueRepository thematiqueRepository; private final ThematiqueRepository thematiqueRepository;
public ThematiqueService(ThematiqueRepository thematiqueRepository){
this.thematiqueRepository = thematiqueRepository;
}
private void checkFieldsConstraints(Thematique thematique){ private void checkFieldsConstraints(Thematique thematique){
if(this.thematiqueRepository.existsByLibelle(thematique.getLibelle())){ if(this.thematiqueRepository.existsByLibelle(thematique.getLibelle())){
throw new DuplicatedFieldException("libelle"); throw new DuplicatedFieldException("libelle");
@ -41,8 +40,8 @@ public class ThematiqueService implements IThematiqueService {
} }
@Override @Override
public Iterable<Thematique> findAll(){ public Page<Thematique> findAll(Pageable p){
return this.thematiqueRepository.findAll(); return this.thematiqueRepository.findAll(p);
} }
} }

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

Loading…
Cancel
Save