Рефакторинг

This commit is contained in:
upagge 2020-05-05 00:18:02 +03:00
parent b56b806e6f
commit 46de13d138
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
7 changed files with 79 additions and 72 deletions

View File

@ -32,4 +32,16 @@ public interface ContentRepository<T extends Message> {
List<T> 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);
}

View File

@ -34,4 +34,19 @@ public class MailRepositoryJpaImpl implements ContentRepository<Mail> {
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);
}
}

View File

@ -56,9 +56,23 @@ public class MailRepositoryList implements ContentRepository<Mail> {
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);
}
}

View File

@ -17,8 +17,14 @@ import java.util.List;
@Repository
public interface MailRepositoryJpa extends JpaRepository<Mail, Integer> {
List<Mail> findByCreateDateBetween(LocalDateTime from, LocalDateTime to);
List<Mail> findByCreateDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo);
List<Mail> findByAddDateBetween(LocalDateTime from, LocalDateTime to);
List<Mail> findByAddDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo);
void deleteAllByAddDateBetween(LocalDateTime dateFrom, LocalDateTime dateTo);
void deleteAllByAddDateBefore(LocalDateTime date);
void deleteAllByAddDateAfter(LocalDateTime date);
}

View File

@ -18,26 +18,32 @@ public interface MessageService<T extends Message> {
/**
* Получить список сообщений за заданный временной интервал
*
* @param timeFrom Начало интервала
* @param timeTo Конец интервала
* @param dateFrom Начало интервала
* @param dateTo Конец интервала
* @return Список сообщений
*/
List<T> getByAddDateTime(@NonNull LocalDateTime timeFrom, @NonNull LocalDateTime timeTo);
List<T> getByAddDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo);
/**
* Получить список ПОСЛЕДНИХ сообщений для каждого пользователя за заданных временной интервал
*
* @param timeFrom Начало интервала
* @param timeTo Конец интервала
* @param dateFrom Начало интервала
* @param dateTo Конец интервала
* @return Список сообщений
*/
List<T> getLastEventByCreateDateTime(@NonNull LocalDateTime timeFrom, @NonNull LocalDateTime timeTo);
List<T> getLastEventByCreateDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo);
List<T> getLastEventByAddDateTime(@NonNull LocalDateTime timeFrom, @NonNull LocalDateTime timeTo);
List<T> getLastEventByAddDateTime(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo);
/**
* Возвращает новые сообщения от последнего запроса.
*/
List<T> getNewMessage();
void deleteAllByAddDateBetween(@NonNull LocalDateTime dateFrom, @NonNull LocalDateTime dateTo);
void deleteAllByAddDateBefore(@NonNull LocalDateTime date);
void deleteAllByAddDateAfter(@NonNull LocalDateTime date);
}

View File

@ -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<BoardComment> {
private final ContentRepository<BoardComment> commentRepository;
@Override
public void add(BoardComment event) {
commentRepository.add(event);
}
@Override
public List<BoardComment> getByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
throw new IllegalStateException("Не реализовано");
}
@Override
public List<BoardComment> getLastEventByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.trace("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo);
List<BoardComment> mails = commentRepository.betweenByCreateDateTime(timeFrom, timeTo);
return getBoardComments(mails);
}
@Override
public List<BoardComment> getLastEventByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.trace("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo);
List<BoardComment> mails = commentRepository.betweenByAddDateTime(timeFrom, timeTo);
return getBoardComments(mails);
}
@Override
public List<BoardComment> getNewMessage() {
throw new IllegalStateException("Не реализовано");
}
private List<BoardComment> getBoardComments(List<BoardComment> mails) {
Set<Long> people = new HashSet<>();
List<BoardComment> 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;
}
}

View File

@ -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<Mail> getReturnMails(List<Mail> mails) {
Set<Long> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();