diff --git a/src/main/java/net/geant/nmaas/portal/service/WebhookEventService.java b/src/main/java/net/geant/nmaas/portal/service/WebhookEventService.java index 7423f74cf1c7752426a961e0cf8d71081a79c2fa..e137273624c2d7d0468acfc3c57685c832b1c586 100644 --- a/src/main/java/net/geant/nmaas/portal/service/WebhookEventService.java +++ b/src/main/java/net/geant/nmaas/portal/service/WebhookEventService.java @@ -23,7 +23,7 @@ public class WebhookEventService { private static final String WEBHOOK_EVENT_NOT_FOUND = "WebhookEvent not found."; @Autowired - public WebhookEventService(WebhookEventRepository webhookRepository, EncryptionService encryptionService, ModelMapper modelMapper){ + public WebhookEventService(WebhookEventRepository webhookRepository, EncryptionService encryptionService, ModelMapper modelMapper) { this.webhookRepository = webhookRepository; this.encryptionService = encryptionService; this.modelMapper = modelMapper; @@ -50,20 +50,21 @@ public class WebhookEventService { webhookEvent.setAuthorizationHeader(webhookEventDto.getAuthorizationHeader()); } - public void remove(Long id){ + public void remove(Long id) { WebhookEvent webhookEvent = webhookRepository.findById(id).orElseThrow(() -> new MissingElementException(WEBHOOK_EVENT_NOT_FOUND)); webhookRepository.delete(webhookEvent); } public List<WebhookEventDto> getAllWebhooks() { - return webhookRepository.findAll().stream().map(x -> - { + return webhookRepository.findAll().stream() + .map(x -> { try { - x.setTokenValue(x.getTokenValue() == null ? null : encryptionService.decrypt(x.getTokenValue())); + WebhookEventDto dto = modelMapper.map(x, WebhookEventDto.class); + dto.setTokenValue(x.getTokenValue() == null ? null : encryptionService.decrypt(x.getTokenValue())); + return dto; } catch (GeneralSecurityException e) { throw new RuntimeException(e); } - return modelMapper.map(x, WebhookEventDto.class); }).toList(); } @@ -74,7 +75,8 @@ public class WebhookEventService { public WebhookEventDto getById(Long id) throws GeneralSecurityException { WebhookEvent event = webhookRepository.findById(id) .orElseThrow(() -> new MissingElementException(String.format("WebhookEventType with id: %d cannot be found", id))); - event.setTokenValue(event.getTokenValue() == null ? null : encryptionService.decrypt(event.getTokenValue())); - return modelMapper.map(event, WebhookEventDto.class); + WebhookEventDto dto = modelMapper.map(event, WebhookEventDto.class); + dto.setTokenValue(event.getTokenValue() == null ? null : encryptionService.decrypt(event.getTokenValue())); + return dto; } }