Skip to content

Commit

Permalink
Added pagination to observation endpoints with bulk fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
ris-tlp committed Mar 5, 2024
1 parent dc844cc commit 3028121
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -21,8 +23,8 @@ public ObservationController(ObservationService service) {
}

@GetMapping(params = "userUuid")
public ResponseEntity<CollectionModel<EntityModel<GetSlimObservationDto>>> getAllObservationsOfUser(@RequestParam String userUuid) {
return service.getAllObservations(userUuid);
public ResponseEntity<PagedModel<EntityModel<GetSlimObservationDto>>> getAllObservationsOfUser(@RequestParam String userUuid, Pageable pageable) {
return service.getAllObservations(userUuid, pageable);
}

@PostMapping(params = {"userUuid", "celestialEventUuid"},
Expand Down Expand Up @@ -53,8 +55,8 @@ public ResponseEntity<Void> deleteObservation(@PathVariable String observationUu
}

@GetMapping
public ResponseEntity<CollectionModel<EntityModel<GetSlimObservationDto>>> getPublishedObservations() {
return service.getPublishedCourses();
public ResponseEntity<PagedModel<EntityModel<GetSlimObservationDto>>> getPublishedObservations(Pageable pageable) {
return service.getPublishedCourses(pageable);
}

@PostMapping(value = "/{observationUuid}/comments", params = "userUuid", consumes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +51,7 @@ public class ObservationService {
private final ObservationDtoAssembler observationDtoAssembler;
private final ObservationSlimDtoAssembler observationSlimDtoAssembler;
private final ObservationCommentRepository commentRepository;
private final PagedResourcesAssembler<GetSlimObservationDto> pagedResourcesAssembler;

private final S3Service s3Service;
private final JacksonConfig jacksonConfig;
Expand All @@ -64,7 +69,8 @@ public ObservationService(ObservationRepository observationRepository, UserAccou
JacksonConfig jacksonConfig,
ObservationDtoMapper dtoMapper,
CommentNotificationProducer commentNotificationProducer,
ReplyNotificationProducer replyNotificationProducer) {
ReplyNotificationProducer replyNotificationProducer,
PagedResourcesAssembler<GetSlimObservationDto> pagedResourcesAssembler) {
this.observationDtoAssembler = observationDtoAssembler;
this.userRepository = userRepository;
this.observationRepository = observationRepository;
Expand All @@ -76,21 +82,19 @@ public ObservationService(ObservationRepository observationRepository, UserAccou
this.observationSlimDtoAssembler = observationSlimDtoAssembler;
this.commentNotificationProducer = commentNotificationProducer;
this.replyNotificationProducer = replyNotificationProducer;
this.pagedResourcesAssembler = pagedResourcesAssembler;
}

public ResponseEntity<CollectionModel<EntityModel<GetSlimObservationDto>>> getAllObservations(String userUuid) {
List<Observation> observations =
observationRepository.findByOwnerUuid(userUuid).orElseThrow(() -> new UserNotFoundException(userUuid));
public ResponseEntity<PagedModel<EntityModel<GetSlimObservationDto>>> getAllObservations(String userUuid,
Pageable pageable) {
Page<GetSlimObservationDto> observations =
observationRepository.findByOwnerUuid(userUuid, pageable).map(dtoMapper::observationToGetSlimDto);

List<GetSlimObservationDto> observationDtos = dtoMapper.observationListToGetSlimDtoList(observations);
CollectionModel<EntityModel<GetSlimObservationDto>> assembledRequest =
observationSlimDtoAssembler.toCollectionModel(observationDtos);
PagedModel<EntityModel<GetSlimObservationDto>> 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
);
}

Expand Down Expand Up @@ -182,29 +186,20 @@ public ResponseEntity<Void> 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<CollectionModel<EntityModel<GetSlimObservationDto>>> getPublishedCourses() {
List<Observation> observations =
observationRepository.findObservationsByIsPublishedIsTrue().orElseThrow();
public ResponseEntity<PagedModel<EntityModel<GetSlimObservationDto>>> getPublishedCourses(Pageable pageable) {
Page<GetSlimObservationDto> observations =
observationRepository.findObservationsByIsPublishedIsTrue(pageable).map(dtoMapper::observationToGetSlimDto);

List<GetSlimObservationDto> observationDtos = dtoMapper.observationListToGetSlimDtoList(observations);
PagedModel<EntityModel<GetSlimObservationDto>> pagedObservations =
pagedResourcesAssembler.toModel(observations, observationSlimDtoAssembler);

CollectionModel<EntityModel<GetSlimObservationDto>> 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<GetObservationCommentDto> addCommentToObservation(String observationUuid,
Expand Down Expand Up @@ -254,7 +249,7 @@ public ResponseEntity<GetObservationCommentDto> addReplyToObservation(String obs
commentRepository.save(parentComment);

GetObservationCommentDto returnDto = dtoMapper.observationCommentToGetDto(reply);

replyNotificationProducer.addReplyMessage(
dtoMapper.observationCommentToGetDto(parentComment).getAuthor(),
dtoMapper.observationCommentToGetDto(reply).getAuthor(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,8 +12,10 @@

public interface ObservationRepository extends JpaRepository<Observation, Long> {
Optional<Observation> findObservationByUuid(String uuid);
Optional<List<Observation>> findObservationsByIsPublishedIsTrue();
Optional<List<Observation>> findByOwnerUuid(String uuid);
Page<Observation> findObservationsByIsPublishedIsTrue(Pageable pageable);


Page<Observation> 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")
Expand Down

0 comments on commit 3028121

Please sign in to comment.