parent
b73afa868b
commit
91cca9e810
@ -1,56 +1,41 @@
|
|||||||
package fr.iut.sciencequest.sae.controllers;
|
package fr.iut.sciencequest.sae.controllers;
|
||||||
|
|
||||||
import fr.iut.sciencequest.sae.entities.Thematique;
|
import fr.iut.sciencequest.sae.entities.Thematique;
|
||||||
import fr.iut.sciencequest.sae.exceptions.DuplicatedEntity;
|
import fr.iut.sciencequest.sae.services.ThematiqueService;
|
||||||
import fr.iut.sciencequest.sae.repositories.ThematiqueRepository;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.validation.ConstraintViolationException;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
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 {
|
||||||
|
|
||||||
private final ThematiqueRepository thematiqueRepository;
|
private final ThematiqueService thematiqueService;
|
||||||
|
|
||||||
public ThematiqueController(ThematiqueRepository thematiqueRepository) {
|
public ThematiqueController(ThematiqueService thematiqueService) {
|
||||||
this.thematiqueRepository = thematiqueRepository;
|
this.thematiqueService = thematiqueService;
|
||||||
}
|
|
||||||
/*
|
|
||||||
@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)
|
||||||
public Iterable<Thematique> getAllThematiques() {
|
public Iterable<Thematique> getAllThematiques() {
|
||||||
return this.thematiqueRepository.findAll();
|
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)
|
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public Thematique postThematique(@Valid @RequestBody Thematique thematique){
|
public Thematique createThematique(@Valid @RequestBody Thematique thematique){
|
||||||
thematique = this.thematiqueRepository.save(thematique);
|
return this.thematiqueService.create(thematique);
|
||||||
return thematique;
|
}
|
||||||
|
|
||||||
|
@PatchMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public Thematique updateThematique(@PathVariable("id") int id, @Valid @RequestBody Thematique thematique){
|
||||||
|
thematique.setId(id);
|
||||||
|
return this.thematiqueService.update(thematique);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut.sciencequest.sae.exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||||
|
public class EntityNotFoundException extends RuntimeException {
|
||||||
|
public EntityNotFoundException(){
|
||||||
|
super("entity not found");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut.sciencequest.sae.services;
|
||||||
|
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.sae.entities.Thematique;
|
||||||
|
|
||||||
|
public interface ThematiqueService {
|
||||||
|
public Thematique update(Thematique thematique);
|
||||||
|
|
||||||
|
public Thematique create(Thematique thematique);
|
||||||
|
|
||||||
|
public Iterable<Thematique> findAll();
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
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.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ThematiqueServiceImpl implements ThematiqueService{
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue