diff --git a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java new file mode 100644 index 00000000000..fb170661586 --- /dev/null +++ b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java @@ -0,0 +1,89 @@ +package teammates.storage.sqlapi; + +import teammates.common.exception.EntityAlreadyExistsException; +import teammates.common.exception.EntityDoesNotExistException; +import teammates.common.exception.InvalidParametersException; +import teammates.common.util.HibernateUtil; +import teammates.storage.sqlentity.Notification; + +/** + * Handles CRUD operations for notifications. + * + * @see Notification + */ +public final class NotificationsDb extends EntitiesDb { + + private static final NotificationsDb instance = new NotificationsDb(); + + private NotificationsDb() { + // prevent initialization + } + + public static NotificationsDb inst() { + return instance; + } + + /** + * Creates a notification. + */ + public Notification createNotification(Notification notification) + throws InvalidParametersException, EntityAlreadyExistsException { + assert notification != null; + + notification.sanitizeForSaving(); + if (!notification.isValid()) { + throw new InvalidParametersException(notification.getInvalidityInfo()); + } + + if (getNotification(notification.getNotificationId().toString()) != null) { + throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, + notification.toString())); + } + + persist(notification); + return notification; + } + + /** + * Gets a notification by its unique ID. + */ + public Notification getNotification(String notificationId) { + assert notificationId != null; + + return HibernateUtil.getSessionFactory().getCurrentSession().get(Notification.class, notificationId); + } + + /** + * Updates a notification with {@link Notification}. + */ + public Notification updateNotification(Notification notification) + throws InvalidParametersException, EntityDoesNotExistException { + assert notification != null; + + notification.sanitizeForSaving(); + + if (!notification.isValid()) { + throw new InvalidParametersException(notification.getInvalidityInfo()); + } + + if (getNotification(notification.getNotificationId().toString()) == null) { + throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT); + } + + return merge(notification); + } + + /** + * Deletes a notification by its unique ID. + * + *

Fails silently if there is no such notification. + */ + public void deleteNotification(String notificationId) { + assert notificationId != null; + + Notification notification = getNotification(notificationId); + if (notification != null) { + delete(notification); + } + } +} diff --git a/src/main/java/teammates/storage/sqlentity/Notification.java b/src/main/java/teammates/storage/sqlentity/Notification.java index 56a7d31e961..11021507a51 100644 --- a/src/main/java/teammates/storage/sqlentity/Notification.java +++ b/src/main/java/teammates/storage/sqlentity/Notification.java @@ -11,9 +11,7 @@ import teammates.common.datatransfer.NotificationStyle; import teammates.common.datatransfer.NotificationTargetUser; -import teammates.common.datatransfer.attributes.NotificationAttributes; import teammates.common.util.FieldValidator; -import teammates.common.util.JsonUtils; import teammates.common.util.SanitizationHelper; import jakarta.persistence.Column; @@ -187,7 +185,9 @@ public void setUpdatedAt(Instant updatedAt) { @Override public String toString() { - return JsonUtils.toJson(this, NotificationAttributes.class); + return "Notification [id=" + notificationId + ", startTime=" + startTime + ", endTime=" + endTime + + ", style=" + style + ", targetUser=" + targetUser + ", shown=" + shown + ", createdAt=" + createdAt + + ", updatedAt=" + updatedAt + "]"; } @Override