83 lines
3.3 KiB
Java
83 lines
3.3 KiB
Java
package io.gmss.fiscad.implementations.decoupage;
|
|
|
|
import io.gmss.fiscad.entities.decoupage.Arrondissement;
|
|
import io.gmss.fiscad.entities.decoupage.Commune;
|
|
import io.gmss.fiscad.exceptions.BadRequestException;
|
|
import io.gmss.fiscad.exceptions.NotFoundException;
|
|
import io.gmss.fiscad.interfaces.decoupage.ArrondissementService;
|
|
import io.gmss.fiscad.interfaces.decoupage.CommuneService;
|
|
import io.gmss.fiscad.repositories.decoupage.ArrondissementRepository;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
@Service
|
|
public class ArrondissementServiceImpl implements ArrondissementService {
|
|
|
|
private final ArrondissementRepository arrondissementRepository;
|
|
private final CommuneService communeService;
|
|
|
|
public ArrondissementServiceImpl(ArrondissementRepository arrondissementRepository, CommuneService communeService) {
|
|
this.arrondissementRepository = arrondissementRepository;
|
|
this.communeService = communeService;
|
|
}
|
|
|
|
@Override
|
|
public Arrondissement createArrondissement(Arrondissement arrondissement) throws BadRequestException {
|
|
if (arrondissement.getId() != null) {
|
|
throw new BadRequestException("Impossible de créer un nouvel arrondissement ayant un id non null.");
|
|
}
|
|
return arrondissementRepository.save(arrondissement);
|
|
}
|
|
|
|
@Override
|
|
public Arrondissement updateArrondissement(Long id, Arrondissement arrondissement) throws NotFoundException {
|
|
if (arrondissement.getId() == null) {
|
|
throw new BadRequestException("Impossible de mettre à jour un nouvel arrondissement ayant un id null.");
|
|
}
|
|
if (!arrondissementRepository.existsById(arrondissement.getId())) {
|
|
throw new NotFoundException("Impossible de trouver l'arrondissement spécifié dans notre base de données.");
|
|
}
|
|
return arrondissementRepository.save(arrondissement);
|
|
}
|
|
|
|
@Override
|
|
public void deleteArrondissement(Long id) throws NotFoundException {
|
|
Optional<Arrondissement> arrondissementOptional = arrondissementRepository.findById(id);
|
|
if (arrondissementOptional.isPresent()) {
|
|
arrondissementRepository.deleteById(arrondissementOptional.get().getId());
|
|
} else {
|
|
throw new NotFoundException("Impossible de trouver l'arrondissement spécifié dans notre base de données.");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Page<Arrondissement> getArrondissementList(Pageable pageable) {
|
|
return arrondissementRepository.findAll(pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<Arrondissement> getArrondissementList() {
|
|
return arrondissementRepository.findAll();
|
|
}
|
|
|
|
@Override
|
|
public Optional<Arrondissement> getArrondissementById(Long id) {
|
|
return arrondissementRepository.findById(id);
|
|
}
|
|
|
|
@Override
|
|
public List<Arrondissement> getArrondissementByComune(Long communeId) {
|
|
Optional<Commune> communeOptional = communeService.getCommuneById(communeId);
|
|
if (communeOptional.isEmpty()) {
|
|
throw new NotFoundException("Impossible de trouver la commune spécifiée.");
|
|
}
|
|
return arrondissementRepository.findAllByCommune(communeOptional.get());
|
|
}
|
|
|
|
|
|
}
|