Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination and sorting #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.fge.jsonpatch.JsonPatch;
import com.observatory.observationtracker.domain.celestialevent.dto.*;
import com.observatory.observationtracker.domain.celestialevent.models.CelestialEventStatus;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.MediaType;
Expand All @@ -24,8 +25,8 @@ public CelestialEventController(CelestialEventService celestialEventService) {
}

@GetMapping
public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getCelestialEvents() {
return celestialEventService.getAllCelestialEvents();
public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getCelestialEvents(Pageable pageable) {
return celestialEventService.getAllCelestialEvents(pageable);
}

@PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
Expand All @@ -35,8 +36,11 @@ public ResponseEntity<EntityModel<GetCelestialEventDto>> createCelestialEvent(@R
}

@GetMapping(params = "status")
public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getCelestialEventsByStatus(@RequestParam CelestialEventStatus status) {
return celestialEventService.getCelestialEventsByStatus(status);
public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getCelestialEventsByStatus(
Pageable pageable,
@RequestParam CelestialEventStatus status
) {
return celestialEventService.getCelestialEventsByStatus(status, pageable);
}

@GetMapping("/{celestialEventUuid}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
import com.observatory.observationtracker.domain.celestialevent.repositories.CelestialEventRepository;
import com.observatory.observationtracker.domain.useraccount.UserAccount;
import com.observatory.observationtracker.domain.useraccount.UserAccountRepository;
import com.observatory.observationtracker.domain.useraccount.dto.GetUserAccountDto;
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 @@ -49,20 +54,21 @@ public class CelestialEventService {
private final CelestialEventSlimDtoAssembler slimDtoAssembler;
private final UserAccountRepository userAccountRepository;
private final CelestialEventCommentRepository celestialEventCommentRepository;
private final PagedResourcesAssembler<GetSlimCelestialEventDto> pagedResourcesAssembler;


private final JacksonConfig jacksonConfig;
private final S3Service s3Service;
private final CelestialEventDtoMapper celestialEventDtoMapper;

public CelestialEventService(CelestialEventRepository celestialEventRepository,
CelestialEventDtoAssembler dtoAssembler
, JacksonConfig jacksonConfig, S3Service s3Service,
CelestialEventDtoMapper celestialEventDtoMapper,
CelestialEventDtoAssembler dtoAssembler, JacksonConfig jacksonConfig,
S3Service s3Service, CelestialEventDtoMapper celestialEventDtoMapper,
CelestialEventImageRepository celestialEventImageRepository,
UserAccountRepository userAccountRepository,
CelestialEventCommentRepository celestialEventCommentRepository,
CelestialEventSlimDtoAssembler slimDtoAssembler
) {
CelestialEventSlimDtoAssembler slimDtoAssembler,
PagedResourcesAssembler<GetSlimCelestialEventDto> pagedResourcesAssembler) {
this.celestialEventRepository = celestialEventRepository;
this.dtoAssembler = dtoAssembler;
this.jacksonConfig = jacksonConfig;
Expand All @@ -72,36 +78,38 @@ public CelestialEventService(CelestialEventRepository celestialEventRepository,
this.userAccountRepository = userAccountRepository;
this.celestialEventCommentRepository = celestialEventCommentRepository;
this.slimDtoAssembler = slimDtoAssembler;
this.pagedResourcesAssembler = pagedResourcesAssembler;
}

public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getAllCelestialEvents() {
List<CelestialEvent> allCelestialEvents = celestialEventRepository.findAll();
List<GetSlimCelestialEventDto> celestialEventDtos =
celestialEventDtoMapper.celestialEventListToGetSlimDtoList(allCelestialEvents);
public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getAllCelestialEvents(Pageable pageable) {
Page<GetSlimCelestialEventDto> pagedCelestialEvents =
celestialEventRepository.findAll(pageable).map(celestialEventDtoMapper::celestialEventToGetSlimDto);
PagedModel<EntityModel<GetSlimCelestialEventDto>> pagedCelestialEvent =
pagedResourcesAssembler.toModel(pagedCelestialEvents, slimDtoAssembler);

return ResponseEntity.status(HttpStatus.OK).body(
pagedCelestialEvent
.add(
linkTo(methodOn(CelestialEventController.class).getCelestialEventsByStatus(null,
null)).withRel("filter-by-status").withType("GET")
)
);
}

return ResponseEntity.status(HttpStatus.OK)
.body(CollectionModel.of(
slimDtoAssembler.toCollectionModel(celestialEventDtos),
linkTo(CelestialEventController.class).withSelfRel().withType("GET, POST"),
linkTo(methodOn(CelestialEventController.class).getCelestialEventsByStatus(null)).withRel(
"filter-by-status").withType("GET")
));
public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getCelestialEventsByStatus(CelestialEventStatus status, Pageable pageable) {
Page<GetSlimCelestialEventDto> events =
celestialEventRepository.findPagedCelestialEventByEventStatus(status, pageable)
.orElseThrow(() -> new CelestialEventStatusNotFoundException(status))
.map(celestialEventDtoMapper::celestialEventToGetSlimDto);

}
PagedModel<EntityModel<GetSlimCelestialEventDto>> pagedEvents = pagedResourcesAssembler.toModel(events,
slimDtoAssembler);

public ResponseEntity<CollectionModel<EntityModel<GetSlimCelestialEventDto>>> getCelestialEventsByStatus(CelestialEventStatus status) {
List<CelestialEvent> events =
celestialEventRepository.findCelestialEventByEventStatus(status).orElseThrow(() -> new CelestialEventStatusNotFoundException(status));
List<GetSlimCelestialEventDto> celestialEventDtos =
celestialEventDtoMapper.celestialEventListToGetSlimDtoList(events);
return ResponseEntity.status(HttpStatus.OK).body(
CollectionModel.of(
slimDtoAssembler.toCollectionModel(celestialEventDtos),
linkTo(methodOn(CelestialEventController.class).getCelestialEventsByStatus(null)).withSelfRel().withType("GET"),
linkTo(CelestialEventController.class).withRel("all").withType("GET, POST")
)
);
return ResponseEntity.status(HttpStatus.OK)
.body(
pagedEvents
.add(linkTo(CelestialEventController.class).withRel("all").withType("GET, POST"))
);
}

/*
Expand All @@ -114,8 +122,6 @@ public ResponseEntity<Void> updateCelestialEventStatus() {
events.stream().map(this::updateEventStatus).filter(Objects::nonNull).toList();

return ResponseEntity.status(HttpStatus.OK).build();

// return null;
}

/*
Expand All @@ -141,8 +147,7 @@ public ResponseEntity<EntityModel<GetCelestialEventDto>> getCelestialEventByUuid
return ResponseEntity.status(HttpStatus.OK).body(dtoAssembler.toModel(celestialEventDto).add(rootLink));
}

public ResponseEntity<EntityModel<GetCelestialEventDto>> createCelestialEvent(CreateCelestialEventDto celestialEvent,
List<MultipartFile> images) {
public ResponseEntity<EntityModel<GetCelestialEventDto>> createCelestialEvent(CreateCelestialEventDto celestialEvent, List<MultipartFile> images) {
try {
List<String> imageUrls = images.stream().map(image -> {
try {
Expand All @@ -163,8 +168,7 @@ public ResponseEntity<EntityModel<GetCelestialEventDto>> createCelestialEvent(Cr

Link rootLink = linkTo(CelestialEventController.class).withRel("all").withType("GET, POST");
return ResponseEntity.status(HttpStatus.CREATED).body(dtoAssembler.toModel(createdEvent).add(rootLink));
} catch (
RuntimeException e) {
} catch (RuntimeException e) {

throw new IncorrectCelestialEventFormatException();
}
Expand Down Expand Up @@ -233,9 +237,7 @@ public ResponseEntity<GetCelestialEventCommentDto> addCommentToCelestialEvent(St
GetCelestialEventCommentDto returnDto =
celestialEventDtoMapper.celestialEventCommentToGetDto(celestialEventComment);

return ResponseEntity.status(HttpStatus.CREATED).body(
returnDto
);
return ResponseEntity.status(HttpStatus.CREATED).body(returnDto);
}

public ResponseEntity<GetSlimCelestialEventCommentDto> addReplyToCelestialEventComment(String celestialEventUuid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.observatory.observationtracker.domain.celestialevent.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.observatory.observationtracker.domain.celestialevent.models.CelestialEvent;
import com.observatory.observationtracker.domain.celestialevent.models.CelestialEventStatus;

import java.sql.Timestamp;
Expand All @@ -18,6 +19,17 @@ public class GetSlimCelestialEventDto {
private Timestamp updatedTimestamp;
private CelestialEventStatus eventStatus;

// public GetSlimCelestialEventDto(CelestialEvent event) {
// this.uuid = event.getUuid();
// this.celestialEventName = event.getCelestialEventName();
// this.celestialEventDescription = event.getCelestialEventDescription();
// this.celestialEventDateTime = event.getCelestialEventDateTime();
// this.createdTimestamp = event.getCreatedTimestamp();
// this.updatedTimestamp = event.getUpdatedTimestamp();
// this.eventStatus = event.getEventStatus();
//
// }

public String getUuid() {
return uuid;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.observatory.observationtracker.domain.celestialevent.repositories;

import com.observatory.observationtracker.domain.celestialevent.CelestialEventController;
import com.observatory.observationtracker.domain.celestialevent.models.CelestialEvent;
import com.observatory.observationtracker.domain.celestialevent.models.CelestialEventStatus;
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 @@ -12,6 +15,8 @@
public interface CelestialEventRepository extends JpaRepository<CelestialEvent, Long> {
Optional<List<CelestialEvent>> findCelestialEventByEventStatus(CelestialEventStatus status);

Optional<Page<CelestialEvent>> findPagedCelestialEventByEventStatus(CelestialEventStatus status, Pageable page);

@Query("SELECT c FROM CelestialEvent c LEFT JOIN FETCH c.comments com WHERE c.uuid = :uuid AND com.parentComment " +
"IS NULL")
Optional<CelestialEvent> findByNullParentComment(@Param("uuid") String uuid);
Expand Down
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
Loading
Loading