Добавлена возможность делать стримы избранными

This commit is contained in:
serega6531
2019-05-06 22:26:36 +03:00
parent e5fbb58911
commit d1a69db830
5 changed files with 39 additions and 13 deletions

View File

@@ -40,4 +40,14 @@ public class StreamController {
}
}
@PostMapping("/{id}/favorite")
public void favoriteStream(@PathVariable long id) {
streamService.setFavorite(id, true);
}
@PostMapping("/{id}/unfavorite")
public void unfavoriteStream(@PathVariable long id) {
streamService.setFavorite(id, false);
}
}

View File

@@ -14,4 +14,6 @@ public class Pagination {
private int pageSize;
private boolean favorites; // только для стримов, определяет, искать только избранные стримы или все
}

View File

@@ -43,4 +43,6 @@ public class Stream {
@ManyToMany(cascade = CascadeType.ALL)
private List<Pattern> foundPatterns;
private boolean favorite;
}

View File

@@ -9,14 +9,16 @@ import java.util.List;
public interface StreamRepository extends JpaRepository<Stream, Long> {
List<Stream> findAllByIdGreaterThan(long streamId, Pageable pageable);
List<Stream> findAllByFavorite(Pageable pageable, boolean favorite);
List<Stream> findAllByIdLessThan(long streamId, Pageable pageable);
List<Stream> findAllByIdGreaterThanAndFavorite(long streamId, boolean favorite, Pageable pageable);
List<Stream> findAllByService(CtfService service, Pageable pageable);
List<Stream> findAllByIdLessThanAndFavorite(long streamId, boolean favorite, Pageable pageable);
List<Stream> findAllByServiceAndIdGreaterThan(CtfService service, long streamId, Pageable pageable);
List<Stream> findAllByServiceAndFavorite(CtfService service, boolean favorite, Pageable pageable);
List<Stream> findAllByServiceAndIdLessThan(CtfService service, long streamId, Pageable pageable);
List<Stream> findAllByServiceAndIdGreaterThanAndFavorite(CtfService service, long streamId, boolean favorite, Pageable pageable);
List<Stream> findAllByServiceAndIdLessThanAndFavorite(CtfService service, long streamId, boolean favorite, Pageable pageable);
}

View File

@@ -53,7 +53,7 @@ public class StreamService {
);
if (!serviceOptional.isPresent()) {
log.info("Не удалось сохранить стрим: сервиса на порту {} или {} не существует",
log.warn("Не удалось сохранить стрим: сервиса на порту {} или {} не существует",
unfinishedStream.getFirstPort(), unfinishedStream.getSecondPort());
return;
}
@@ -68,7 +68,7 @@ public class StreamService {
packets.removeIf(packet -> packet.getContent().length == 0);
if(packets.isEmpty()) {
log.info("Стрим состоит только из пустых пакетов и не будет сохранен");
log.debug("Стрим состоит только из пустых пакетов и не будет сохранен");
return;
}
}
@@ -107,16 +107,26 @@ public class StreamService {
return repository.findById(id);
}
@Transactional
public void setFavorite(long id, boolean favorite) {
final Optional<Stream> streamOptional = repository.findById(id);
if(streamOptional.isPresent()) {
final Stream stream = streamOptional.get();
stream.setFavorite(favorite);
repository.save(stream);
}
}
public List<Stream> findAll(Pagination pagination) {
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
if(pagination.isFetchLatest()) { // последние стримы
return repository.findAll(page).getContent();
return repository.findAllByFavorite(page, pagination.isFavorites());
} else {
if (pagination.getDirection() == Sort.Direction.ASC) { // более новые стримы
return repository.findAllByIdGreaterThan(pagination.getStartingFrom(), page);
return repository.findAllByIdGreaterThanAndFavorite(pagination.getStartingFrom(), pagination.isFavorites(), page);
} else { // более старые стримы
return repository.findAllByIdLessThan(pagination.getStartingFrom(), page);
return repository.findAllByIdLessThanAndFavorite(pagination.getStartingFrom(), pagination.isFavorites(), page);
}
}
}
@@ -125,12 +135,12 @@ public class StreamService {
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
if(pagination.isFetchLatest()) { // последние стримы
return repository.findAllByService(service, page);
return repository.findAllByServiceAndFavorite(service, pagination.isFavorites(), page);
} else {
if (pagination.getDirection() == Sort.Direction.ASC) { // более новые стримы
return repository.findAllByServiceAndIdGreaterThan(service, pagination.getStartingFrom(), page);
return repository.findAllByServiceAndIdGreaterThanAndFavorite(service, pagination.getStartingFrom(), pagination.isFavorites(), page);
} else { // более старые стримы
return repository.findAllByServiceAndIdLessThan(service, pagination.getStartingFrom(), page);
return repository.findAllByServiceAndIdLessThanAndFavorite(service, pagination.getStartingFrom(), pagination.isFavorites(), page);
}
}
}