pagination to events

master
Dino Dizdarevic 2018-09-18 21:44:31 +02:00
parent b2badb1720
commit f3d6bdfd59
2 changed files with 14 additions and 3 deletions

View File

@ -8,9 +8,12 @@ import ba.steleks.repository.EventsJpaRepository;
import ba.steleks.repository.MediaJpaRepository; import ba.steleks.repository.MediaJpaRepository;
import ba.steleks.util.ProxyHeaders; import ba.steleks.util.ProxyHeaders;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.awt.print.Pageable;
@RestController @RestController
public class ActualEventsController { public class ActualEventsController {
@ -26,9 +29,11 @@ public class ActualEventsController {
} }
@RequestMapping(path = "/realEvents", method = RequestMethod.GET) @RequestMapping(path = "/realEvents", method = RequestMethod.GET)
public ResponseEntity<?> getRealEvents() { public ResponseEntity<?> getRealEvents(
@RequestParam( "page" ) int page,
@RequestParam( "size" ) int size) {
Iterable<Event> result; Iterable<Event> result;
result = repository.findByEventTypeId(EVENT_TYPE_EVENT); result = repository.findAllByEventTypeId(EVENT_TYPE_EVENT, new PageRequest(page, size));
return ResponseEntity return ResponseEntity
.status(HttpStatus.OK) .status(HttpStatus.OK)
.body(new Object() { .body(new Object() {
@ -38,8 +43,11 @@ public class ActualEventsController {
}); });
} }
@RequestMapping(path = "/realEvents", method = RequestMethod.POST) @RequestMapping(path = "/realEvents", method = RequestMethod.POST)
public ResponseEntity<?> addEvents(@RequestBody EventRequest eventRequest, @RequestHeader(ProxyHeaders.USER_ID) String userId) throws ExternalServiceException { public ResponseEntity<?> addEvents(
@RequestBody EventRequest eventRequest,
@RequestHeader(ProxyHeaders.USER_ID) String userId) throws ExternalServiceException {
return eventController.addEventWithType(eventRequest, userId, EVENT_TYPE_EVENT); return eventController.addEventWithType(eventRequest, userId, EVENT_TYPE_EVENT);
} }
} }

View File

@ -5,6 +5,8 @@ package ba.steleks.repository;
import ba.steleks.model.Event; import ba.steleks.model.Event;
import ba.steleks.repository.projection.EventProjection; import ba.steleks.repository.projection.EventProjection;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@ -14,4 +16,5 @@ import java.util.List;
@RepositoryRestResource(excerptProjection = EventProjection.class) @RepositoryRestResource(excerptProjection = EventProjection.class)
public interface EventsJpaRepository extends PagingAndSortingRepository<Event, Long> { public interface EventsJpaRepository extends PagingAndSortingRepository<Event, Long> {
List<Event> findByEventTypeId(Long eventTypeId); List<Event> findByEventTypeId(Long eventTypeId);
Page<Event> findAllByEventTypeId(Long eventTypeId, Pageable pageable);
} }