Skip to content

Commit

Permalink
Refactor AnalyticsHelperController for improved readability and maint…
Browse files Browse the repository at this point in the history
…ainability
  • Loading branch information
LeonardoMeireles55 committed Jan 19, 2025
1 parent 9b907b8 commit 6a0f88e
Showing 1 changed file with 107 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,115 +19,112 @@

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

public class AnalyticsHelperController {
private final AnalyticsHelperService analyticsHelperService;

public AnalyticsHelperController(AnalyticsHelperService analyticsHelperService) {
this.analyticsHelperService = analyticsHelperService;
}
@GetMapping("/{id}")
public ResponseEntity<AnalyticsRecord> getAnalyticsById(@PathVariable Long id) {
return ResponseEntity.ok(analyticsHelperService.findOneById(id));
}
public ResponseEntity<CollectionModel<EntityModel<AnalyticsRecord>>> getAllAnalyticsWithLinks(List<String> names, Pageable pageable) {
Page<AnalyticsRecord> resultsList = analyticsHelperService.findAnalyticsPagedByNameIn(names, pageable);

// Create EntityModel for each record with its own self link
var entityModels = resultsList.getContent().stream()
.map(record -> EntityModel.of(record,
linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel()))
.collect(Collectors.toList());

var result = addPaginationLinks(CollectionModel.of(entityModels), resultsList, pageable);
return ResponseEntity.ok(result);

}

public ResponseEntity<CollectionModel<EntityModel<AnalyticsRecord>>>
getAnalyticsByDateBetweenWithLinks
(List<String> names, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable) {

Page<AnalyticsRecord> analyticsRecordPaged = analyticsHelperService
.findAnalyticsByNameInAndDateBetweenWithLinks(names, startDate, endDate, pageable);

if (analyticsRecordPaged == null) {
return ResponseEntity.noContent().build();
}
// Create EntityModel for each record with its own self link
var entityModels = analyticsRecordPaged.getContent().stream()
.map(record -> EntityModel.of(record,
linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel()))
.collect(Collectors.toList());

// Create the collection model with the entity models
var collectionModel = CollectionModel.of(entityModels);


// Add pagination links
var result = addPaginationLinks(collectionModel, analyticsRecordPaged, pageable);

return ResponseEntity.ok(result);
}

EntityModel<AnalyticsRecord> createEntityModel(AnalyticsRecord record, Pageable pageable) {
return EntityModel.of(record,
linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel());
}

private CollectionModel<EntityModel<AnalyticsRecord>> addPaginationLinks(
CollectionModel<EntityModel<AnalyticsRecord>> collectionModel,
Page<AnalyticsRecord> page,
Pageable pageable) {

UriComponentsBuilder uriBuilder = ServletUriComponentsBuilder.fromCurrentRequest();

// Clear any existing collection-level links to prevent duplication
// collectionModel.removeLinks();

// Link for the first page
collectionModel.add(Link.of(uriBuilder
.replaceQueryParam("page", 0)
.replaceQueryParam("size", pageable.getPageSize())
.toUriString().replace("%2520", "%20"))
.withRel("first"));

// Link for the previous page if it exists
if (page.hasPrevious()) {
collectionModel.add(Link.of(uriBuilder
.replaceQueryParam("page", pageable.getPageNumber() - 1)
.replaceQueryParam("size", pageable.getPageSize())
.toUriString().replace("%2520", "%20"))
.withRel("prev"));
}

// Link for the next page if it exists
if (page.hasNext()) {
collectionModel.add(Link.of(uriBuilder
.replaceQueryParam("page", pageable.getPageNumber() + 1)
.replaceQueryParam("size", pageable.getPageSize())
.toUriString().replace("%2520", "%20"))
.withRel("next"));
}

// Link for the last page
collectionModel.add(Link.of(uriBuilder
.replaceQueryParam("page", page.getTotalPages() - 1)
.replaceQueryParam("size", pageable.getPageSize())
.toUriString().replace("%2520", "%20"))
.withRel("last"));

// Add metadata about the current page
collectionModel.add(Link.of(uriBuilder
.replaceQueryParam("page", pageable.getPageNumber())
.replaceQueryParam("size", pageable.getPageSize())
.toUriString().replace("%2520", "%20"))
.withRel("current-page"));

// collectionModel.add(Link.of(String.valueOf(page.getTotalPages())).withSelfRel());
collectionModel.add(Link.of(String.valueOf(page.getTotalPages()), "totalPages"));
collectionModel.add(Link.of(String.valueOf(page.getNumber()), "currentPage"));


return collectionModel;
}
private final AnalyticsHelperService analyticsHelperService;

public AnalyticsHelperController(AnalyticsHelperService analyticsHelperService) {
this.analyticsHelperService = analyticsHelperService;
}

@GetMapping("/{id}")
public ResponseEntity<AnalyticsRecord> getAnalyticsById(@PathVariable Long id) {
return ResponseEntity.ok(analyticsHelperService.findOneById(id));
}

public ResponseEntity<CollectionModel<EntityModel<AnalyticsRecord>>> getAllAnalyticsWithLinks(
List<String> names, Pageable pageable) {
Page<AnalyticsRecord> resultsList =
analyticsHelperService.findAnalyticsPagedByNameIn(names, pageable);

// Create EntityModel for each record with its own self link
var entityModels = resultsList.getContent().stream()
.map(record -> EntityModel.of(record,
linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel()))
.collect(Collectors.toList());

var result = addPaginationLinks(CollectionModel.of(entityModels), resultsList, pageable);
return ResponseEntity.ok(result);

}

public ResponseEntity<CollectionModel<EntityModel<AnalyticsRecord>>> getAnalyticsByDateBetweenWithLinks(
List<String> names, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable) {

Page<AnalyticsRecord> analyticsRecordPaged = analyticsHelperService
.findAnalyticsByNameInAndDateBetweenWithLinks(names, startDate, endDate, pageable);

if (analyticsRecordPaged == null) {
return ResponseEntity.noContent().build();
}
// Create EntityModel for each record with its own self link
var entityModels = analyticsRecordPaged.getContent().stream()
.map(record -> EntityModel.of(record,
linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel()))
.collect(Collectors.toList());

// Create the collection model with the entity models
var collectionModel = CollectionModel.of(entityModels);


// Add pagination links
var result = addPaginationLinks(collectionModel, analyticsRecordPaged, pageable);

return ResponseEntity.ok(result);
}

EntityModel<AnalyticsRecord> createEntityModel(AnalyticsRecord record, Pageable pageable) {
return EntityModel.of(record,
linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel());
}

private CollectionModel<EntityModel<AnalyticsRecord>> addPaginationLinks(
CollectionModel<EntityModel<AnalyticsRecord>> collectionModel,
Page<AnalyticsRecord> page, Pageable pageable) {

UriComponentsBuilder uriBuilder =
ServletUriComponentsBuilder.fromCurrentRequest().replacePath("/backend-api"
+ ServletUriComponentsBuilder.fromCurrentRequest().build().getPath());

// Clear any existing collection-level links to prevent duplication
// collectionModel.removeLinks();

// Link for the first page
collectionModel.add(Link.of(uriBuilder.replaceQueryParam("page", 0)
.replaceQueryParam("size", pageable.getPageSize()).toUriString()
.replace("%2520", "%20")).withRel("first"));

// Link for the previous page if it exists
if (page.hasPrevious()) {
collectionModel
.add(Link.of(uriBuilder.replaceQueryParam("page", pageable.getPageNumber() - 1)
.replaceQueryParam("size", pageable.getPageSize()).toUriString()
.replace("%2520", "%20")).withRel("prev"));
}

// Link for the next page if it exists
if (page.hasNext()) {
collectionModel
.add(Link.of(uriBuilder.replaceQueryParam("page", pageable.getPageNumber() + 1)
.replaceQueryParam("size", pageable.getPageSize()).toUriString()
.replace("%2520", "%20")).withRel("next"));
}

// Link for the last page
collectionModel.add(Link.of(uriBuilder.replaceQueryParam("page", page.getTotalPages() - 1)
.replaceQueryParam("size", pageable.getPageSize()).toUriString()
.replace("%2520", "%20")).withRel("last"));

// Add metadata about the current page
collectionModel.add(Link.of(uriBuilder.replaceQueryParam("page", pageable.getPageNumber())
.replaceQueryParam("size", pageable.getPageSize()).toUriString()
.replace("%2520", "%20")).withRel("current-page"));

// collectionModel.add(Link.of(String.valueOf(page.getTotalPages())).withSelfRel());
collectionModel.add(Link.of(String.valueOf(page.getTotalPages()), "totalPages"));
collectionModel.add(Link.of(String.valueOf(page.getNumber()), "currentPage"));


return collectionModel;
}
}

0 comments on commit 6a0f88e

Please sign in to comment.