Fix :: Erreur 401 sur toutes les ressources à la levée d'une exception quelconque.

Modification des controlleurs et ajout des blocs try.. catch
This commit is contained in:
2025-03-19 08:34:15 +01:00
parent 368d38a936
commit 7ebca1e1a3
262 changed files with 8390 additions and 3883 deletions

View File

@@ -41,37 +41,37 @@ public class SecteurServiceImpl implements SecteurService {
@Override
public Secteur createSecteur(SecteurPayload secteurPayload) throws BadRequestException {
if(secteurPayload.getId() != null ){
if (secteurPayload.getId() != null) {
throw new BadRequestException("Impossible de créer un nouveau secteur ayant un id non null.");
}
if(secteurPayload.getChefSecteurId()!=null && userRepository.existsById(secteurPayload.getChefSecteurId()) ) {
if (secteurPayload.getChefSecteurId() != null && userRepository.existsById(secteurPayload.getChefSecteurId())) {
Secteur secteur = getSecteurFromPayload(secteurPayload);
return secteurRepository.save(secteur);
}else{
} else {
throw new BadRequestException("Impossible de créer un nouveau secteur sans le chef le Chef Secteur.");
}
}
private Secteur getSecteurFromPayload(SecteurPayload secteurPayload) {
Secteur secteur=new Secteur();
Optional<User> optionalUser=userRepository.findById(secteurPayload.getChefSecteurId());
Secteur secteur = new Secteur();
Optional<User> optionalUser = userRepository.findById(secteurPayload.getChefSecteurId());
secteur.setChefSecteur(optionalUser.orElse(null));
Optional<Structure> optionalStructure=structureRepository.findById(secteurPayload.getStructureId());
Optional<Structure> optionalStructure = structureRepository.findById(secteurPayload.getStructureId());
secteur.setStructure(optionalStructure.orElse(null));
List<SecteurDecoupage> secteurDecoupageList=new ArrayList<>();
List<SecteurDecoupage> secteurDecoupageList = new ArrayList<>();
for (SecteurDecoupagePayload sdp: secteurPayload.getSecteurDecoupages()){
SecteurDecoupage sd=new SecteurDecoupage();
if(sdp.getSecteurId()!=null && secteurRepository.existsById(sdp.getSecteurId()) ){
for (SecteurDecoupagePayload sdp : secteurPayload.getSecteurDecoupages()) {
SecteurDecoupage sd = new SecteurDecoupage();
if (sdp.getSecteurId() != null && secteurRepository.existsById(sdp.getSecteurId())) {
sd.setSecteur(secteurRepository.findById(sdp.getSecteurId()).orElse(null));
}
if(sdp.getArrondissementId()!=null && arrondissementRepository.existsById(sdp.getArrondissementId()) ){
if (sdp.getArrondissementId() != null && arrondissementRepository.existsById(sdp.getArrondissementId())) {
sd.setArrondissement(arrondissementRepository.findById(sdp.getArrondissementId()).orElse(null));
}
if(sdp.getQuartierId()!=null && quartierRepository.existsById(sdp.getQuartierId()) ){
if (sdp.getQuartierId() != null && quartierRepository.existsById(sdp.getQuartierId())) {
sd.setQuartier(quartierRepository.findById(sdp.getQuartierId()).orElse(null));
}
sd.setDateDebut(sdp.getDateDebut());
@@ -88,17 +88,17 @@ public class SecteurServiceImpl implements SecteurService {
@Override
public Secteur updateSecteur(Long id, SecteurPayload secteurPayload) throws NotFoundException {
if(secteurPayload.getId() == null ){
if (secteurPayload.getId() == null) {
throw new BadRequestException("Impossible de mettre à jour un nouveau secteur ayant un id null.");
}
if(!secteurRepository.existsById(secteurPayload.getId())){
if (!secteurRepository.existsById(secteurPayload.getId())) {
throw new NotFoundException("Impossible de trouver le secteur spécifié.");
}
if(secteurPayload.getChefSecteurId()!=null && userRepository.existsById(secteurPayload.getChefSecteurId()) ) {
if (secteurPayload.getChefSecteurId() != null && userRepository.existsById(secteurPayload.getChefSecteurId())) {
Secteur secteur = getSecteurFromPayload(secteurPayload);
return secteurRepository.save(secteur);
}else{
} else {
throw new BadRequestException("Impossible de créer un nouveau secteur sans le chef le Chef Secteur.");
}
}
@@ -106,9 +106,9 @@ public class SecteurServiceImpl implements SecteurService {
@Override
public void deleteSecteur(Long id) throws NotFoundException {
Optional<Secteur> secteurOptional = secteurRepository.findById(id);
if(secteurOptional.isPresent()){
if (secteurOptional.isPresent()) {
secteurRepository.deleteById(secteurOptional.get().getId());
}else{
} else {
throw new NotFoundException("Impossible de trouver le secteur spécifié.");
}
}