Доделана пагинация в стримах

This commit is contained in:
serega6531
2019-05-01 19:53:29 +03:00
parent bb517516e1
commit b4a9903d1b
3 changed files with 59 additions and 18 deletions

View File

@@ -1,34 +1,43 @@
package ru.serega6531.packmate.controller; package ru.serega6531.packmate.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable; import ru.serega6531.packmate.model.CtfService;
import org.springframework.web.bind.annotation.RequestMapping; import ru.serega6531.packmate.model.Pagination;
import org.springframework.web.bind.annotation.RestController;
import ru.serega6531.packmate.model.Stream; import ru.serega6531.packmate.model.Stream;
import ru.serega6531.packmate.service.ServicesService;
import ru.serega6531.packmate.service.StreamService; import ru.serega6531.packmate.service.StreamService;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/api/stream/") @RequestMapping("/api/stream/")
public class StreamController { public class StreamController {
private final StreamService service; private final StreamService streamService;
private final ServicesService servicesService;
@Autowired @Autowired
public StreamController(StreamService service) { public StreamController(StreamService streamService, ServicesService servicesService) {
this.service = service; this.streamService = streamService;
this.servicesService = servicesService;
} }
@GetMapping("/all") @PostMapping("/all")
public List<Stream> getStreams() { public List<Stream> getStreams(@RequestBody Pagination pagination) {
return service.findAll(); return streamService.findAll(pagination);
} }
@GetMapping("/{port}") @PostMapping("/{port}")
public List<Stream> getStreams(@PathVariable int port) { public List<Stream> getStreams(@PathVariable int port, @RequestBody Pagination pagination) {
return service.findAllByServicePort(port); final Optional<CtfService> serviceOptional = servicesService.findByPort(port);
if(serviceOptional.isPresent()) {
return streamService.findAllByService(pagination, serviceOptional.get());
} else {
return Collections.emptyList();
}
} }
} }

View File

@@ -1,12 +1,22 @@
package ru.serega6531.packmate.repository; package ru.serega6531.packmate.repository;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.serega6531.packmate.model.CtfService;
import ru.serega6531.packmate.model.Stream; import ru.serega6531.packmate.model.Stream;
import java.util.List; import java.util.List;
public interface StreamRepository extends JpaRepository<Stream, Long> { public interface StreamRepository extends JpaRepository<Stream, Long> {
List<Stream> findAllByService_Port(int port); List<Stream> findAllByIdGreaterThan(long streamId, Pageable pageable);
List<Stream> findAllByIdLessThan(long streamId, Pageable pageable);
List<Stream> findAllByService(CtfService service, Pageable pageable);
List<Stream> findAllByServiceAndIdGreaterThan(CtfService service, long streamId, Pageable pageable);
List<Stream> findAllByServiceAndIdLessThan(CtfService service, long streamId, Pageable pageable);
} }

View File

@@ -3,6 +3,8 @@ package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.serega6531.packmate.model.*; import ru.serega6531.packmate.model.*;
@@ -93,12 +95,32 @@ public class StreamService {
return repository.findById(id); return repository.findById(id);
} }
public List<Stream> findAll() { public List<Stream> findAll(Pagination pagination) {
return repository.findAll(); PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
if(pagination.isFetchLatest()) { // последние стримы
return repository.findAll(page).getContent();
} else {
if (pagination.getDirection() == Sort.Direction.ASC) { // более новые стримы
return repository.findAllByIdGreaterThan(pagination.getStartingFrom(), page);
} else { // более старые стримы
return repository.findAllByIdLessThan(pagination.getStartingFrom(), page);
}
}
} }
public List<Stream> findAllByServicePort(int port) { public List<Stream> findAllByService(Pagination pagination, CtfService service) {
return repository.findAllByService_Port(port); PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
if(pagination.isFetchLatest()) { // последние стримы
return repository.findAllByService(service, page);
} else {
if (pagination.getDirection() == Sort.Direction.ASC) { // более новые стримы
return repository.findAllByServiceAndIdGreaterThan(service, pagination.getStartingFrom(), page);
} else { // более старые стримы
return repository.findAllByServiceAndIdLessThan(service, pagination.getStartingFrom(), page);
}
}
} }
} }