86 lines
3.3 KiB
Java
86 lines
3.3 KiB
Java
package io.gmss.fiscad.implementations.decoupage;
|
|
|
|
import io.gmss.fiscad.entities.decoupage.Arrondissement;
|
|
import io.gmss.fiscad.entities.decoupage.Quartier;
|
|
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.QuartierService;
|
|
import io.gmss.fiscad.repositories.decoupage.QuartierRepository;
|
|
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 QuartierServiceImpl implements QuartierService {
|
|
|
|
private final QuartierRepository quartierRepository;
|
|
private final ArrondissementService arrondissementService;
|
|
|
|
public QuartierServiceImpl(QuartierRepository quartierRepository, ArrondissementService arrondissementService) {
|
|
this.quartierRepository = quartierRepository;
|
|
this.arrondissementService = arrondissementService;
|
|
}
|
|
|
|
@Override
|
|
public Quartier createQuartier(Quartier quartier) throws BadRequestException {
|
|
if (quartier.getId() != null) {
|
|
throw new BadRequestException("Impossible de créer un nouveau quartier ayant un id non null.");
|
|
}
|
|
return quartierRepository.save(quartier);
|
|
}
|
|
|
|
@Override
|
|
public Quartier updateQuartier(Long id, Quartier quartier) throws NotFoundException {
|
|
if (quartier.getId() == null) {
|
|
throw new BadRequestException("Impossible de mettre à jour un nouveau quartier ayant un id null.");
|
|
}
|
|
if (!quartierRepository.existsById(quartier.getId())) {
|
|
throw new NotFoundException("Impossible de trouver le quartier spécifié dans notre base de données.");
|
|
}
|
|
return quartierRepository.save(quartier);
|
|
}
|
|
|
|
@Override
|
|
public void deleteQuartier(Long id) throws NotFoundException {
|
|
Optional<Quartier> quartierOptional = quartierRepository.findById(id);
|
|
if (quartierOptional.isPresent()) {
|
|
quartierRepository.deleteById(quartierOptional.get().getId());
|
|
} else {
|
|
throw new NotFoundException("Impossible de trouver le quartier spécifié dans notre base de données.");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Page<Quartier> getQuartierList(Pageable pageable) {
|
|
return quartierRepository.findAll(pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<Quartier> getQuartierList() {
|
|
return quartierRepository.findAll();
|
|
}
|
|
|
|
@Override
|
|
public Optional<Quartier> getQuartierById(Long id) {
|
|
if (quartierRepository.existsById(id)) {
|
|
return quartierRepository.findById(id);
|
|
} else {
|
|
throw new NotFoundException("Impossible de trouver le quartier spécifié dans la base de données.");
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public List<Quartier> getQuartierByArrondissement(Long arrondissementId) {
|
|
Optional<Arrondissement> arrondissementOptional = arrondissementService.getArrondissementById(arrondissementId);
|
|
if (arrondissementOptional.isEmpty()) {
|
|
throw new NotFoundException("Impossible de trouver l'arrondissement spécifié.");
|
|
}
|
|
return quartierRepository.getAllByArrondissement(arrondissementOptional.get());
|
|
}
|
|
}
|