Pull request #496: RED-4771: Reformatted code and added @Transactional

Merge in RED/persistence-service from RED-4771 to master

* commit '8276e199c1d4e9370a96caf34c69c1444b224430':
  RED-4771: Reformatted code and added @Transactional
This commit is contained in:
Philipp Schramm 2022-07-25 14:47:09 +02:00 committed by Ali Oezyetimoglu
commit dbfe0e1cc3

View File

@ -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<NotificationPreferencesEntity> findAll() {
return notificationPreferencesRepository.findAll();
}
}