From cef8d81e376672ce3d403ea738a8dbe25a3a641d Mon Sep 17 00:00:00 2001 From: emj Date: Mon, 14 Aug 2023 22:05:47 +0200 Subject: [PATCH] backend | Change the transactional annotation --- .../appservices/ProviderApplicationService.java | 17 ++++++++++++++++- .../api/provider/ProviderControllerIT.java | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hla-product-ordering/backend/src/main/java/com/amigoscode/appservices/ProviderApplicationService.java b/hla-product-ordering/backend/src/main/java/com/amigoscode/appservices/ProviderApplicationService.java index d68b2d1..2909b24 100644 --- a/hla-product-ordering/backend/src/main/java/com/amigoscode/appservices/ProviderApplicationService.java +++ b/hla-product-ordering/backend/src/main/java/com/amigoscode/appservices/ProviderApplicationService.java @@ -2,14 +2,19 @@ import com.amigoscode.domain.provider.PageProvider; import com.amigoscode.domain.provider.Provider; +import com.amigoscode.domain.provider.ProviderAlreadyExistsException; import com.amigoscode.domain.provider.ProviderService; +import com.amigoscode.external.storage.provider.ProviderEntity; +import lombok.extern.java.Log; import lombok.RequiredArgsConstructor; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor +@Log public class ProviderApplicationService { private final ProviderService providerService; @@ -26,8 +31,18 @@ public PageProvider findAll(Pageable pageable) { } @Transactional - public Provider save(Provider providerToSave) { + public Provider saveTransaction(Provider providerToSave) { return providerService.save(providerToSave, authenticationFacade.getLoggedInUserId()); + + } + + public Provider save(Provider providerToSave) { + try { + return saveTransaction(providerToSave); + } catch (DataIntegrityViolationException ex) { + log.warning("Provider " + providerToSave.getEmail() + " already exits in db"); + throw new ProviderAlreadyExistsException(); + } } @Transactional public void update(Provider provider) { diff --git a/hla-product-ordering/backend/src/test/java/com/amigoscode/api/provider/ProviderControllerIT.java b/hla-product-ordering/backend/src/test/java/com/amigoscode/api/provider/ProviderControllerIT.java index dd6212c..d1db678 100644 --- a/hla-product-ordering/backend/src/test/java/com/amigoscode/api/provider/ProviderControllerIT.java +++ b/hla-product-ordering/backend/src/test/java/com/amigoscode/api/provider/ProviderControllerIT.java @@ -80,7 +80,7 @@ void admin_should_get_response_code_conflict_when_provider_is_in_db() { ErrorResponse.class); //then - assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); }