From 8276e199c1d4e9370a96caf34c69c1444b224430 Mon Sep 17 00:00:00 2001 From: Philipp Schramm Date: Mon, 25 Jul 2022 14:42:43 +0200 Subject: [PATCH] RED-4771: Reformatted code and added @Transactional --- ...ficationPreferencesPersistenceService.java | 69 +++++++++++-------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/persistence/NotificationPreferencesPersistenceService.java b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/persistence/NotificationPreferencesPersistenceService.java index 62cef7e83..cb125312b 100644 --- a/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/persistence/NotificationPreferencesPersistenceService.java +++ b/persistence-service-v1/persistence-service-processor-v1/src/main/java/com/iqser/red/service/persistence/management/v1/processor/service/persistence/NotificationPreferencesPersistenceService.java @@ -1,15 +1,5 @@ package com.iqser.red.service.persistence.management.v1.processor.service.persistence; -import com.iqser.red.service.persistence.management.v1.processor.entity.notification.NotificationPreferencesEntity; -import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationPreferencesRepository; -import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationRepository; -import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationPreferences; -import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationType; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.BeanUtils; -import org.springframework.stereotype.Service; - -import javax.transaction.Transactional; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; @@ -17,6 +7,19 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import javax.transaction.Transactional; + +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import com.iqser.red.service.persistence.management.v1.processor.entity.notification.NotificationPreferencesEntity; +import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationPreferencesRepository; +import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationRepository; +import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationPreferences; +import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationType; + +import lombok.RequiredArgsConstructor; + @Service @RequiredArgsConstructor public class NotificationPreferencesPersistenceService { @@ -25,40 +28,45 @@ public class NotificationPreferencesPersistenceService { private final NotificationRepository notificationRepository; + @Transactional public void setNotificationPreference(String userId, NotificationPreferences newNotificationPreferences) { - notificationPreferencesRepository.findById(userId) - .ifPresentOrElse(oldNotificationPreferences -> { - // if we disabled the notification preferences just now - if (oldNotificationPreferences.isInAppNotificationsEnabled() != newNotificationPreferences.isInAppNotificationsEnabled()) { - notificationRepository.deleteAllByUserId(userId, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); - } + notificationPreferencesRepository.findById(userId).ifPresentOrElse(oldNotificationPreferences -> { - var removedNotificationTypes = new ArrayList<>(oldNotificationPreferences.getInAppNotifications()); - removedNotificationTypes.removeAll(newNotificationPreferences.getInAppNotifications()); + // if we disabled the notification preferences just now + if (oldNotificationPreferences.isInAppNotificationsEnabled() != newNotificationPreferences.isInAppNotificationsEnabled()) { + notificationRepository.deleteAllByUserId(userId, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); + } - if(!removedNotificationTypes.isEmpty()) { - notificationRepository.deleteAllByUserIdAndNotificationTypeIn(userId, removedNotificationTypes, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); - } + var removedNotificationTypes = new ArrayList<>(oldNotificationPreferences.getInAppNotifications()); + removedNotificationTypes.removeAll(newNotificationPreferences.getInAppNotifications()); - BeanUtils.copyProperties(newNotificationPreferences, oldNotificationPreferences); + if (!removedNotificationTypes.isEmpty()) { + notificationRepository.deleteAllByUserIdAndNotificationTypeIn(userId, removedNotificationTypes, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); + } - }, () -> { - NotificationPreferencesEntity notificationPreferencesEntity = new NotificationPreferencesEntity(); - BeanUtils.copyProperties(newNotificationPreferences, notificationPreferencesEntity); - notificationPreferencesEntity.setUserId(userId); - notificationPreferencesRepository.save(notificationPreferencesEntity); - }); + BeanUtils.copyProperties(newNotificationPreferences, oldNotificationPreferences); + + }, () -> { + NotificationPreferencesEntity notificationPreferencesEntity = new NotificationPreferencesEntity(); + BeanUtils.copyProperties(newNotificationPreferences, notificationPreferencesEntity); + notificationPreferencesEntity.setUserId(userId); + notificationPreferencesRepository.save(notificationPreferencesEntity); + }); } + @Transactional public void deleteNotificationPreferences(String userId) { + notificationPreferencesRepository.deleteById(userId); } + @Transactional public NotificationPreferencesEntity getOrCreateNotificationPreferences(String userId) { + return notificationPreferencesRepository.findById(userId).orElseGet(() -> { var notificationPreference = new NotificationPreferencesEntity(); @@ -70,6 +78,8 @@ public class NotificationPreferencesPersistenceService { }); } + + @Transactional public void initializePreferencesIfNotExists(String userId) { if (!notificationPreferencesRepository.existsByUserId(userId)) { @@ -77,7 +87,10 @@ public class NotificationPreferencesPersistenceService { } } + public List findAll() { + return notificationPreferencesRepository.findAll(); } + }