All checks were successful
CI - Build & Test (develop) / build-and-test (pull_request) Successful in 38s
113 lines
4.6 KiB
Java
113 lines
4.6 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.paylaods.request.crudweb.QuartierPaylaodWeb;
|
|
import io.gmss.fiscad.persistence.repositories.decoupage.ArrondissementRepository;
|
|
import io.gmss.fiscad.persistence.repositories.decoupage.QuartierRepository;
|
|
import io.gmss.fiscad.service.EntityFromPayLoadService;
|
|
import lombok.AllArgsConstructor;
|
|
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
|
|
@AllArgsConstructor
|
|
public class QuartierServiceImpl implements QuartierService {
|
|
|
|
private final QuartierRepository quartierRepository;
|
|
private final ArrondissementRepository arrondissementRepository;
|
|
private final ArrondissementService arrondissementService;
|
|
private final EntityFromPayLoadService entityFromPayLoadService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public QuartierPaylaodWeb createQuartier(QuartierPaylaodWeb quartierPaylaodWeb) throws BadRequestException {
|
|
if (quartierPaylaodWeb.getId() != null) {
|
|
throw new BadRequestException("Impossible de créer un nouveau quartier ayant un id non null.");
|
|
}
|
|
|
|
if (quartierPaylaodWeb.getArrondissementId() == null) {
|
|
throw new BadRequestException("Impossible de créer un nouveau quartier : Veuillez préciser l'arrondissement");
|
|
}
|
|
|
|
|
|
if (!arrondissementRepository.existsById(quartierPaylaodWeb.getArrondissementId())) {
|
|
throw new BadRequestException("Impossible de créer un nouveau quartier : l'arrondissement n'existe pas");
|
|
}
|
|
Quartier quartier = entityFromPayLoadService.getQuartierFromPayLoadWeb(quartierPaylaodWeb);
|
|
quartier = quartierRepository.save(quartier);
|
|
|
|
|
|
Optional<QuartierPaylaodWeb> optionalQuartierPaylaodWeb = quartierRepository.findQuartierToDtoById(quartier.getId());
|
|
|
|
return optionalQuartierPaylaodWeb.orElse(null);
|
|
}
|
|
|
|
@Override
|
|
public QuartierPaylaodWeb updateQuartier(Long id, QuartierPaylaodWeb quartierPaylaodWeb) throws NotFoundException {
|
|
if (quartierPaylaodWeb.getId() == null) {
|
|
throw new BadRequestException("Impossible de mettre à jour un nouveau quartier ayant un id null.");
|
|
}
|
|
if (!quartierRepository.existsById(quartierPaylaodWeb.getId())) {
|
|
throw new NotFoundException("Impossible de trouver le quartier spécifié dans notre base de données.");
|
|
}
|
|
|
|
if (quartierPaylaodWeb.getArrondissementId() == null) {
|
|
throw new BadRequestException("Impossible de créer un nouveau quartier : Veuillez préciser l'arrondissement");
|
|
}
|
|
Quartier quartier = entityFromPayLoadService.getQuartierFromPayLoadWeb(quartierPaylaodWeb);
|
|
quartier = quartierRepository.save(quartier);
|
|
|
|
|
|
Optional<QuartierPaylaodWeb> optionalQuartierPaylaodWeb = quartierRepository.findQuartierToDtoById(quartier.getId());
|
|
|
|
return optionalQuartierPaylaodWeb.orElse(null);
|
|
}
|
|
|
|
@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<QuartierPaylaodWeb> getQuartierList(Pageable pageable) {
|
|
return quartierRepository.findAllQuartierToDtoPageable(pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<QuartierPaylaodWeb> getQuartierList() {
|
|
return quartierRepository.findAllQuartierToDto();
|
|
}
|
|
|
|
@Override
|
|
public List<QuartierPaylaodWeb> getQuartierListByArrondissementId(Long arrondissementId) {
|
|
return quartierRepository.findAllQuartierByArrondissementToDto(arrondissementId);
|
|
}
|
|
|
|
@Override
|
|
public Page<QuartierPaylaodWeb> getQuartierListByArrondissementId(Long arrondissementId, Pageable pageable) {
|
|
return quartierRepository.findAllQuartierByArrondissementToDtoPageable(arrondissementId,pageable);
|
|
}
|
|
|
|
@Override
|
|
public Optional<QuartierPaylaodWeb> getQuartierById(Long id) {
|
|
return quartierRepository.findQuartierToDtoById(id);
|
|
}
|
|
|
|
}
|