From caf2f9a3a8091bdd9aef23365556f450beef5f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kr=C3=ADdl?= <xkridl@fi.muni.cz> Date: Sat, 15 Apr 2023 16:39:41 +0200 Subject: [PATCH] Make CountryFacadeImpl to use new exception handling mechanism --- .../core/facade/country/CountryFacadeImpl.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/cz/muni/fi/pa165/core/facade/country/CountryFacadeImpl.java b/core/src/main/java/cz/muni/fi/pa165/core/facade/country/CountryFacadeImpl.java index 6f2b634..b5b9576 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/facade/country/CountryFacadeImpl.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/facade/country/CountryFacadeImpl.java @@ -34,7 +34,7 @@ public class CountryFacadeImpl implements CountryFacade<Long> { @Override public Optional<CountryDto> findById(Long id) { Country foundEntity = countryService.findById(id) - .orElseThrow(() -> new ResourceNotFoundException("Country with id " + id + " not found.")); + .orElseThrow(ResourceNotFoundException::new); return Optional.of(countryMapper.toDto(foundEntity)); } @@ -48,6 +48,9 @@ public class CountryFacadeImpl implements CountryFacade<Long> { @Override public void deleteById(Long id) { + if (!countryService.existsById(id)) { + throw new ResourceNotFoundException(); + } countryService.deleteById(id); } @@ -58,10 +61,12 @@ public class CountryFacadeImpl implements CountryFacade<Long> { @Override public CountryDto update(Long id, NewCountryDtoRequest newCountryDtoRequest) { - Country updatedEntity = countryService.findById(id) - .orElseThrow(() -> new ResourceNotFoundException("Country with id " + id + " not found.")); - updatedEntity.setName(newCountryDtoRequest.getName()); - countryService.save(updatedEntity); + if (!countryService.existsById(id)) { + throw new ResourceNotFoundException(); + } + + Country newEntity = countryMapper.toEntityFromNewRequest(newCountryDtoRequest); + Country updatedEntity = countryService.update(id, newEntity); return countryMapper.toDto(updatedEntity); } -- GitLab