diff --git a/src/main/java/org/sadtech/social/core/repository/ContentRepository.java b/src/main/java/org/sadtech/social/core/repository/ContentRepository.java index 636afb4..c8fc79e 100644 --- a/src/main/java/org/sadtech/social/core/repository/ContentRepository.java +++ b/src/main/java/org/sadtech/social/core/repository/ContentRepository.java @@ -32,4 +32,16 @@ public interface ContentRepository { List betweenByAddDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); + /** + * Удаляет данные за указанный период + * + * @param dateFrom Дата начала + * @param dateTo Дата окончания + */ + void deleteAllByAddDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); + + void deleteAllByAddDateBefore(@NonNull LocalDateTime date); + + void deleteAllByAddDateAfter(@NonNull LocalDateTime date); + } diff --git a/src/main/java/org/sadtech/social/core/repository/impl/jpa/MailRepositoryJpaImpl.java b/src/main/java/org/sadtech/social/core/repository/impl/jpa/MailRepositoryJpaImpl.java index 833751f..6accaa1 100644 --- a/src/main/java/org/sadtech/social/core/repository/impl/jpa/MailRepositoryJpaImpl.java +++ b/src/main/java/org/sadtech/social/core/repository/impl/jpa/MailRepositoryJpaImpl.java @@ -34,4 +34,19 @@ public class MailRepositoryJpaImpl implements ContentRepository { return mailRepositoryJpa.findByAddDateBetween(dateFrom, dateTo); } + @Override + public void deleteAllByAddDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo) { + mailRepositoryJpa.deleteAllByAddDateBetween(dateFrom, dateTo); + } + + @Override + public void deleteAllByAddDateBefore(@NonNull LocalDateTime date) { + mailRepositoryJpa.deleteAllByAddDateBefore(date); + } + + @Override + public void deleteAllByAddDateAfter(@NonNull LocalDateTime date) { + mailRepositoryJpa.deleteAllByAddDateAfter(date); + } + } diff --git a/src/main/java/org/sadtech/social/core/repository/impl/local/MailRepositoryList.java b/src/main/java/org/sadtech/social/core/repository/impl/local/MailRepositoryList.java index 3dd9d6f..7708411 100644 --- a/src/main/java/org/sadtech/social/core/repository/impl/local/MailRepositoryList.java +++ b/src/main/java/org/sadtech/social/core/repository/impl/local/MailRepositoryList.java @@ -56,9 +56,23 @@ public class MailRepositoryList implements ContentRepository { return rezultMails; } + @Override + public void deleteAllByAddDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo) { + mails.removeIf(mail -> dateFrom.isBefore(mail.getAddDate()) && dateTo.isAfter(mail.getAddDate())); + } + + @Override + public void deleteAllByAddDateBefore(LocalDateTime date) { + mails.removeIf(mail -> date.isBefore(mail.getAddDate())); + } + + @Override + public void deleteAllByAddDateAfter(LocalDateTime date) { + mails.removeIf(mail -> date.isAfter(mail.getAddDate())); + } + private boolean isTimePeriod(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo, @NonNull LocalDateTime dateTime) { return dateFrom.isBefore(dateTime) && dateTo.isAfter(dateTime); } - } diff --git a/src/main/java/org/sadtech/social/core/repository/jpa/MailRepositoryJpa.java b/src/main/java/org/sadtech/social/core/repository/jpa/MailRepositoryJpa.java index dc93f80..d7ee062 100644 --- a/src/main/java/org/sadtech/social/core/repository/jpa/MailRepositoryJpa.java +++ b/src/main/java/org/sadtech/social/core/repository/jpa/MailRepositoryJpa.java @@ -17,8 +17,14 @@ import java.util.List; @Repository public interface MailRepositoryJpa extends JpaRepository { - List findByCreateDateBetween(LocalDateTime from, LocalDateTime to); + List findByCreateDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo); - List findByAddDateBetween(LocalDateTime from, LocalDateTime to); + List findByAddDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo); + + void deleteAllByAddDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo); + + void deleteAllByAddDateBefore(LocalDateTime date); + + void deleteAllByAddDateAfter(LocalDateTime date); } diff --git a/src/main/java/org/sadtech/social/core/service/MessageService.java b/src/main/java/org/sadtech/social/core/service/MessageService.java index 9ed2deb..a0644dd 100644 --- a/src/main/java/org/sadtech/social/core/service/MessageService.java +++ b/src/main/java/org/sadtech/social/core/service/MessageService.java @@ -18,26 +18,32 @@ public interface MessageService { /** * Получить список сообщений за заданный временной интервал * - * @param timeFrom Начало интервала - * @param timeTo Конец интервала + * @param dateFrom Начало интервала + * @param dateTo Конец интервала * @return Список сообщений */ - List getByAddDateTime(@NonNull LocalDateTime timeFrom, @NonNull LocalDateTime timeTo); + List getByAddDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); /** * Получить список ПОСЛЕДНИХ сообщений для каждого пользователя за заданных временной интервал * - * @param timeFrom Начало интервала - * @param timeTo Конец интервала + * @param dateFrom Начало интервала + * @param dateTo Конец интервала * @return Список сообщений */ - List getLastEventByCreateDateTime(@NonNull LocalDateTime timeFrom, @NonNull LocalDateTime timeTo); + List getLastEventByCreateDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); - List getLastEventByAddDateTime(@NonNull LocalDateTime timeFrom, @NonNull LocalDateTime timeTo); + List getLastEventByAddDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); /** * Возвращает новые сообщения от последнего запроса. */ List getNewMessage(); + void deleteAllByAddDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo); + + void deleteAllByAddDateBefore(@NonNull LocalDateTime date); + + void deleteAllByAddDateAfter(@NonNull LocalDateTime date); + } diff --git a/src/main/java/org/sadtech/social/core/service/impl/BoardCommentServiceImpl.java b/src/main/java/org/sadtech/social/core/service/impl/BoardCommentServiceImpl.java deleted file mode 100644 index cdede63..0000000 --- a/src/main/java/org/sadtech/social/core/service/impl/BoardCommentServiceImpl.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.sadtech.social.core.service.impl; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.sadtech.social.core.domain.content.BoardComment; -import org.sadtech.social.core.repository.ContentRepository; -import org.sadtech.social.core.service.MessageService; - -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -@Slf4j -@RequiredArgsConstructor -public class BoardCommentServiceImpl implements MessageService { - - private final ContentRepository commentRepository; - - @Override - public void add(BoardComment event) { - commentRepository.add(event); - } - - @Override - public List getByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) { - throw new IllegalStateException("Не реализовано"); - } - - @Override - public List getLastEventByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) { - log.trace("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo); - List mails = commentRepository.betweenByCreateDateTime(timeFrom, timeTo); - return getBoardComments(mails); - } - - @Override - public List getLastEventByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) { - log.trace("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo); - List mails = commentRepository.betweenByAddDateTime(timeFrom, timeTo); - return getBoardComments(mails); - } - - @Override - public List getNewMessage() { - throw new IllegalStateException("Не реализовано"); - } - - private List getBoardComments(List mails) { - Set people = new HashSet<>(); - List returnMails = new ArrayList<>(); - for (int i = mails.size() - 1; i >= 0; i--) { - if (!people.contains(mails.get(i).getPersonId())) { - returnMails.add(mails.get(i)); - people.add(mails.get(i).getPersonId()); - } - } - return returnMails; - } - -} diff --git a/src/main/java/org/sadtech/social/core/service/impl/MailServiceImpl.java b/src/main/java/org/sadtech/social/core/service/impl/MailServiceImpl.java index e62e4ee..4cd5bf1 100644 --- a/src/main/java/org/sadtech/social/core/service/impl/MailServiceImpl.java +++ b/src/main/java/org/sadtech/social/core/service/impl/MailServiceImpl.java @@ -1,5 +1,6 @@ package org.sadtech.social.core.service.impl; +import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.sadtech.social.core.domain.content.Mail; @@ -70,6 +71,21 @@ public class MailServiceImpl implements MailService { return lastEventByAddDateTime; } + @Override + public void deleteAllByAddDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo) { + mailRepository.deleteAllByAddDateBetween(dateFrom, dateTo); + } + + @Override + public void deleteAllByAddDateBefore(@NonNull LocalDateTime date) { + mailRepository.deleteAllByAddDateBefore(date); + } + + @Override + public void deleteAllByAddDateAfter(@NonNull LocalDateTime date) { + mailRepository.deleteAllByAddDateAfter(date); + } + private List getReturnMails(List mails) { Set people = new HashSet<>(); List returnMails = new ArrayList<>();