-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added public notification settings on own profile page
Added missing view elements on calendar subpages Moved EventNotificationJob into job package
- Loading branch information
1 parent
ffb5c10
commit c327d35
Showing
18 changed files
with
529 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/main/java/de/webalf/slotbot/assembler/NotificationSettingAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package de.webalf.slotbot.assembler; | ||
|
||
import de.webalf.slotbot.model.NotificationSetting; | ||
import de.webalf.slotbot.model.dtos.referenceless.NotificationSettingsReferencelessDto; | ||
import lombok.experimental.UtilityClass; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* @author Alf | ||
* @since 12.08.2021 | ||
*/ | ||
@UtilityClass | ||
public final class NotificationSettingAssembler { | ||
private NotificationSettingsReferencelessDto toReferencelessDto(NotificationSetting notificationSetting) { | ||
return NotificationSettingsReferencelessDto.builder() | ||
.hoursBeforeEvent(notificationSetting.getHoursBeforeEvent()) | ||
.minutesBeforeEvent(notificationSetting.getMinutesBeforeEvent()) | ||
.build(); | ||
} | ||
|
||
public static List<NotificationSettingsReferencelessDto> toReferencelessDtoList(Iterable<? extends NotificationSetting> notificationSettings) { | ||
return StreamSupport.stream(notificationSettings.spliterator(), false) | ||
.map(NotificationSettingAssembler::toReferencelessDto) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/de/webalf/slotbot/controller/NotificationSettingsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.webalf.slotbot.controller; | ||
|
||
import de.webalf.slotbot.assembler.NotificationSettingAssembler; | ||
import de.webalf.slotbot.model.dtos.NotificationSettingDto; | ||
import de.webalf.slotbot.model.dtos.referenceless.NotificationSettingsReferencelessDto; | ||
import de.webalf.slotbot.service.NotificationSettingsService; | ||
import de.webalf.slotbot.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
import static de.webalf.slotbot.util.permissions.ApplicationPermissionHelper.HAS_ROLE_EVERYONE; | ||
import static de.webalf.slotbot.util.permissions.PermissionHelper.assertIsLoggedInUser; | ||
|
||
/** | ||
* @author Alf | ||
* @since 12.08.2021 | ||
*/ | ||
@RestController | ||
@RequestMapping("/notifications") | ||
@RequiredArgsConstructor(onConstructor_ = @Autowired) | ||
public class NotificationSettingsController { | ||
private final NotificationSettingsService notificationSettingsService; | ||
private final UserService userService; | ||
|
||
@DeleteMapping("/{userId}") | ||
@PreAuthorize(HAS_ROLE_EVERYONE) | ||
public ResponseEntity<Void> deleteAllByUser(@PathVariable(name = "userId") String userId) { | ||
assertIsLoggedInUser(userId); | ||
notificationSettingsService.deleteAllByUser(userService.find(Long.parseLong(userId))); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@PutMapping("/{userId}") | ||
@PreAuthorize(HAS_ROLE_EVERYONE) | ||
public List<NotificationSettingsReferencelessDto> updateNotificationSettings(@PathVariable(name = "userId") String userId, @RequestBody List<NotificationSettingDto> notificationSettings) { | ||
assertIsLoggedInUser(userId); | ||
return NotificationSettingAssembler.toReferencelessDtoList( | ||
notificationSettingsService.updatePublicNotificationSettings(userService.find(Long.parseLong(userId)), notificationSettings) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -475,5 +475,6 @@ public void archive() { | |
} | ||
|
||
setDiscordInformation(null); | ||
//TODO delete notification settings | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/de/webalf/slotbot/model/NotificationSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.webalf.slotbot.model; | ||
|
||
import lombok.*; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import javax.persistence.*; | ||
|
||
/** | ||
* @author Alf | ||
* @since 11.08.2021 | ||
*/ | ||
@Entity | ||
@Table(name = "notification_setting", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})}) | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@SuperBuilder | ||
public class NotificationSetting extends AbstractSuperIdEntity { | ||
@ManyToOne(optional = false) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
@NonNull | ||
private User user; | ||
|
||
@Column(name = "hours_before") | ||
private int hoursBeforeEvent; | ||
|
||
@Column(name = "minutes_before") | ||
private int minutesBeforeEvent; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "event_id") | ||
private Event event; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/de/webalf/slotbot/model/dtos/NotificationSettingDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.webalf.slotbot.model.dtos; | ||
|
||
import de.webalf.slotbot.model.dtos.referenceless.NotificationSettingsReferencelessDto; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
/** | ||
* @author Alf | ||
* @since 12.08.2021 | ||
*/ | ||
@EqualsAndHashCode(callSuper = true) | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Data | ||
@SuperBuilder | ||
public class NotificationSettingDto extends NotificationSettingsReferencelessDto { | ||
private EventDto event; | ||
} |
20 changes: 20 additions & 0 deletions
20
...java/de/webalf/slotbot/model/dtos/referenceless/NotificationSettingsReferencelessDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.webalf.slotbot.model.dtos.referenceless; | ||
|
||
import de.webalf.slotbot.model.dtos.AbstractIdEntityDto; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
/** | ||
* @author Alf | ||
* @since 12.08.2021 | ||
*/ | ||
@EqualsAndHashCode(callSuper = true) | ||
@RequiredArgsConstructor | ||
@Data | ||
@SuperBuilder | ||
public class NotificationSettingsReferencelessDto extends AbstractIdEntityDto { | ||
private int hoursBeforeEvent; | ||
private int minutesBeforeEvent; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/de/webalf/slotbot/repository/NotificationSettingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.webalf.slotbot.repository; | ||
|
||
import de.webalf.slotbot.model.NotificationSetting; | ||
import de.webalf.slotbot.model.User; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Alf | ||
* @since 11.08.2021 | ||
*/ | ||
@Repository | ||
public interface NotificationSettingRepository extends SuperIdEntityJpaRepository<NotificationSetting> { | ||
List<NotificationSetting> findAllByUserAndEventIsNull(User user); | ||
|
||
void deleteAllByUser(User user); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/de/webalf/slotbot/service/NotificationSettingsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package de.webalf.slotbot.service; | ||
|
||
import de.webalf.slotbot.model.NotificationSetting; | ||
import de.webalf.slotbot.model.User; | ||
import de.webalf.slotbot.model.dtos.NotificationSettingDto; | ||
import de.webalf.slotbot.repository.NotificationSettingRepository; | ||
import de.webalf.slotbot.util.DtoUtils; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Alf | ||
* @since 11.08.2021 | ||
*/ | ||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor(onConstructor_ = @Autowired) | ||
public class NotificationSettingsService { | ||
private final NotificationSettingRepository notificationSettingRepository; | ||
|
||
/** | ||
* Finds all {@link NotificationSetting}s for the given {@link User} that don't have an event link | ||
* | ||
* @param user to get settings for | ||
* @return all public settings of user | ||
*/ | ||
public List<NotificationSetting> findAllPublicSettings(User user) { | ||
return notificationSettingRepository.findAllByUserAndEventIsNull(user); | ||
} | ||
|
||
/** | ||
* Sets the public {@link NotificationSetting}s for the given {@link User} | ||
* | ||
* @param user to update settings for | ||
* @param notificationSettingDtos new complete list with new or updated settings | ||
* @return saved public notification settings | ||
*/ | ||
public List<NotificationSetting> updatePublicNotificationSettings(User user, @NonNull List<NotificationSettingDto> notificationSettingDtos) { | ||
List<NotificationSetting> notificationSettings = new ArrayList<>(); | ||
notificationSettingDtos.forEach(notificationSettingDto -> | ||
notificationSettings.add(updateOrCreateNotificationSetting(user, notificationSettingDto))); | ||
|
||
//Delete removed settings | ||
final List<NotificationSetting> existingSettings = new ArrayList<>(findAllPublicSettings(user)); | ||
existingSettings.removeIf(existingSetting -> | ||
notificationSettings.stream().anyMatch(notificationSetting -> existingSetting.getId() == notificationSetting.getId())); | ||
notificationSettingRepository.deleteAll(existingSettings); | ||
|
||
return notificationSettings; | ||
} | ||
|
||
/** | ||
* Updates or creates a new {@link NotificationSetting} for the given user | ||
* | ||
* @param user to notify | ||
* @param dto new or updated setting | ||
* @return saved setting | ||
*/ | ||
private NotificationSetting updateOrCreateNotificationSetting(User user, @NonNull NotificationSettingDto dto) { | ||
final NotificationSetting notificationSetting = notificationSettingRepository.findById(dto.getId()).orElseGet(() -> NotificationSetting.builder().user(user).build()); | ||
|
||
DtoUtils.ifPresent(dto.getHoursBeforeEvent(), notificationSetting::setHoursBeforeEvent); | ||
DtoUtils.ifPresent(dto.getMinutesBeforeEvent(), notificationSetting::setMinutesBeforeEvent); | ||
|
||
return notificationSettingRepository.save(notificationSetting); | ||
} | ||
|
||
/** | ||
* Removes all {@link NotificationSetting}s for the given user | ||
* | ||
* @param user to deactivate notification for | ||
*/ | ||
public void deleteAllByUser(User user) { | ||
notificationSettingRepository.deleteAllByUser(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...slotbot/service/EventNotificationJob.java → ...bot/service/job/EventNotificationJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.