From 30281214475cfe9c841c1b0919797469eee76c77 Mon Sep 17 00:00:00 2001 From: Omar Date: Tue, 5 Mar 2024 02:09:52 -0500 Subject: [PATCH] Added pagination to observation endpoints with bulk fetches --- .../observation/ObservationController.java | 10 ++-- .../observation/ObservationService.java | 49 +++++++++---------- .../repositories/ObservationRepository.java | 8 ++- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationController.java b/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationController.java index 8006d96..2d9efde 100644 --- a/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationController.java +++ b/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationController.java @@ -2,8 +2,10 @@ import com.github.fge.jsonpatch.JsonPatch; import com.observatory.observationtracker.domain.observation.dto.*; +import org.springframework.data.domain.Pageable; import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; +import org.springframework.hateoas.PagedModel; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -21,8 +23,8 @@ public ObservationController(ObservationService service) { } @GetMapping(params = "userUuid") - public ResponseEntity>> getAllObservationsOfUser(@RequestParam String userUuid) { - return service.getAllObservations(userUuid); + public ResponseEntity>> getAllObservationsOfUser(@RequestParam String userUuid, Pageable pageable) { + return service.getAllObservations(userUuid, pageable); } @PostMapping(params = {"userUuid", "celestialEventUuid"}, @@ -53,8 +55,8 @@ public ResponseEntity deleteObservation(@PathVariable String observationUu } @GetMapping - public ResponseEntity>> getPublishedObservations() { - return service.getPublishedCourses(); + public ResponseEntity>> getPublishedObservations(Pageable pageable) { + return service.getPublishedCourses(pageable); } @PostMapping(value = "/{observationUuid}/comments", params = "userUuid", consumes = diff --git a/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationService.java b/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationService.java index aef525a..0dcfef6 100644 --- a/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationService.java +++ b/api/src/main/java/com/observatory/observationtracker/domain/observation/ObservationService.java @@ -26,9 +26,13 @@ import com.observatory.observationtracker.domain.useraccount.exceptions.UserNotFoundException; import com.observatory.observationtracker.rabbitmq.notifications.CommentNotificationProducer; import com.observatory.observationtracker.rabbitmq.notifications.ReplyNotificationProducer; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PagedResourcesAssembler; import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.Link; +import org.springframework.hateoas.PagedModel; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @@ -47,6 +51,7 @@ public class ObservationService { private final ObservationDtoAssembler observationDtoAssembler; private final ObservationSlimDtoAssembler observationSlimDtoAssembler; private final ObservationCommentRepository commentRepository; + private final PagedResourcesAssembler pagedResourcesAssembler; private final S3Service s3Service; private final JacksonConfig jacksonConfig; @@ -64,7 +69,8 @@ public ObservationService(ObservationRepository observationRepository, UserAccou JacksonConfig jacksonConfig, ObservationDtoMapper dtoMapper, CommentNotificationProducer commentNotificationProducer, - ReplyNotificationProducer replyNotificationProducer) { + ReplyNotificationProducer replyNotificationProducer, + PagedResourcesAssembler pagedResourcesAssembler) { this.observationDtoAssembler = observationDtoAssembler; this.userRepository = userRepository; this.observationRepository = observationRepository; @@ -76,21 +82,19 @@ public ObservationService(ObservationRepository observationRepository, UserAccou this.observationSlimDtoAssembler = observationSlimDtoAssembler; this.commentNotificationProducer = commentNotificationProducer; this.replyNotificationProducer = replyNotificationProducer; + this.pagedResourcesAssembler = pagedResourcesAssembler; } - public ResponseEntity>> getAllObservations(String userUuid) { - List observations = - observationRepository.findByOwnerUuid(userUuid).orElseThrow(() -> new UserNotFoundException(userUuid)); + public ResponseEntity>> getAllObservations(String userUuid, + Pageable pageable) { + Page observations = + observationRepository.findByOwnerUuid(userUuid, pageable).map(dtoMapper::observationToGetSlimDto); - List observationDtos = dtoMapper.observationListToGetSlimDtoList(observations); - CollectionModel> assembledRequest = - observationSlimDtoAssembler.toCollectionModel(observationDtos); + PagedModel> pagedObservations = + pagedResourcesAssembler.toModel(observations, observationSlimDtoAssembler); return ResponseEntity.status(HttpStatus.OK).body( - CollectionModel.of( - assembledRequest, - linkTo(methodOn(ObservationController.class).getAllObservationsOfUser(userUuid)).withSelfRel().withType("GET, POST") - ) + pagedObservations ); } @@ -182,29 +186,20 @@ public ResponseEntity deleteObservation(String observationUuid) { try { observationRepository.delete(observation); -// observation.getImages().forEach(observationImage -> s3Service.deleteImage(observationImage.getUrl())); return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } catch (RuntimeException e) { throw new RuntimeException("There was an error in deletion"); } } - public ResponseEntity>> getPublishedCourses() { - List observations = - observationRepository.findObservationsByIsPublishedIsTrue().orElseThrow(); + public ResponseEntity>> getPublishedCourses(Pageable pageable) { + Page observations = + observationRepository.findObservationsByIsPublishedIsTrue(pageable).map(dtoMapper::observationToGetSlimDto); - List observationDtos = dtoMapper.observationListToGetSlimDtoList(observations); + PagedModel> pagedObservations = + pagedResourcesAssembler.toModel(observations, observationSlimDtoAssembler); - CollectionModel> assembledRequest = - observationSlimDtoAssembler.toCollectionModel(observationDtos); - - return ResponseEntity.status(HttpStatus.OK).body( - CollectionModel.of( - assembledRequest, - linkTo(methodOn(ObservationController.class).getPublishedObservations()).withSelfRel() -// linkTo(methodOn().withSelfRel().withType("GET, POST") - ) - ); + return ResponseEntity.status(HttpStatus.OK).body(pagedObservations); } public ResponseEntity addCommentToObservation(String observationUuid, @@ -254,7 +249,7 @@ public ResponseEntity addReplyToObservation(String obs commentRepository.save(parentComment); GetObservationCommentDto returnDto = dtoMapper.observationCommentToGetDto(reply); - + replyNotificationProducer.addReplyMessage( dtoMapper.observationCommentToGetDto(parentComment).getAuthor(), dtoMapper.observationCommentToGetDto(reply).getAuthor(), diff --git a/api/src/main/java/com/observatory/observationtracker/domain/observation/repositories/ObservationRepository.java b/api/src/main/java/com/observatory/observationtracker/domain/observation/repositories/ObservationRepository.java index 3294bc9..0604998 100644 --- a/api/src/main/java/com/observatory/observationtracker/domain/observation/repositories/ObservationRepository.java +++ b/api/src/main/java/com/observatory/observationtracker/domain/observation/repositories/ObservationRepository.java @@ -1,6 +1,8 @@ package com.observatory.observationtracker.domain.observation.repositories; import com.observatory.observationtracker.domain.observation.models.Observation; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -10,8 +12,10 @@ public interface ObservationRepository extends JpaRepository { Optional findObservationByUuid(String uuid); - Optional> findObservationsByIsPublishedIsTrue(); - Optional> findByOwnerUuid(String uuid); + Page findObservationsByIsPublishedIsTrue(Pageable pageable); + + + Page findByOwnerUuid(String uuid, Pageable pageable); @Query("SELECT o FROM Observation o LEFT JOIN FETCH o.comments com WHERE o.uuid = :uuid AND com.parentComment " + "IS NULL")