82 lines
3.0 KiB
Java
82 lines
3.0 KiB
Java
package io.gmss.fiscad.implementations.decoupage;
|
|
|
|
import io.gmss.fiscad.entities.decoupage.Commune;
|
|
import io.gmss.fiscad.entities.decoupage.Departement;
|
|
import io.gmss.fiscad.exceptions.BadRequestException;
|
|
import io.gmss.fiscad.exceptions.NotFoundException;
|
|
import io.gmss.fiscad.interfaces.decoupage.CommuneService;
|
|
import io.gmss.fiscad.interfaces.decoupage.DepartementService;
|
|
import io.gmss.fiscad.repositories.decoupage.CommuneRepository;
|
|
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 CommuneServiceImpl implements CommuneService {
|
|
|
|
private final CommuneRepository communeRepository;
|
|
private final DepartementService departementService;
|
|
|
|
public CommuneServiceImpl(CommuneRepository communeRepository, DepartementService departementService) {
|
|
this.communeRepository = communeRepository;
|
|
this.departementService = departementService;
|
|
}
|
|
|
|
@Override
|
|
public Commune createCommune(Commune commune) throws BadRequestException {
|
|
if (commune.getId() != null) {
|
|
throw new BadRequestException("Impossible de créer une nouvelle commune ayant un id non null.");
|
|
}
|
|
return communeRepository.save(commune);
|
|
}
|
|
|
|
@Override
|
|
public Commune updateCommune(Long id, Commune commune) throws NotFoundException {
|
|
if (commune.getId() == null) {
|
|
throw new BadRequestException("Impossible de mettre à jour une nouvelle commune ayant un id null.");
|
|
}
|
|
if (!communeRepository.existsById(commune.getId())) {
|
|
throw new NotFoundException("Impossible de trouver la commune spécifiée dans notre base de données.");
|
|
}
|
|
return communeRepository.save(commune);
|
|
}
|
|
|
|
@Override
|
|
public void deleteCommune(Long id) throws NotFoundException {
|
|
Optional<Commune> communeOptional = communeRepository.findById(id);
|
|
if (communeOptional.isPresent()) {
|
|
communeRepository.deleteById(communeOptional.get().getId());
|
|
} else {
|
|
throw new NotFoundException("Impossible de trouver la commune spécifiée dans notre base de données.");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Page<Commune> getCommuneList(Pageable pageable) {
|
|
return communeRepository.findAll(pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<Commune> getCommuneList() {
|
|
return communeRepository.findAll();
|
|
}
|
|
|
|
|
|
@Override
|
|
public List<Commune> getCommunesByDepartement(Long departementId) {
|
|
Optional<Departement> departementOptional = departementService.getDepartementById(departementId);
|
|
if (departementOptional.isEmpty()) {
|
|
throw new NotFoundException("Impossible de trouver le département spécifié.");
|
|
}
|
|
return communeRepository.findAllByDepartement(departementOptional.get());
|
|
}
|
|
|
|
@Override
|
|
public Optional<Commune> getCommuneById(Long id) {
|
|
return communeRepository.findById(id);
|
|
}
|
|
}
|