From 6a0f88eedc7c612bfc1a97fd4e177cdbd6df259b Mon Sep 17 00:00:00 2001 From: Leonardo Date: Sun, 19 Jan 2025 19:51:16 -0300 Subject: [PATCH] Refactor AnalyticsHelperController for improved readability and maintainability --- .../analytics/AnalyticsHelperController.java | 217 +++++++++--------- 1 file changed, 107 insertions(+), 110 deletions(-) diff --git a/src/main/java/leonardo/labutilities/qualitylabpro/controllers/analytics/AnalyticsHelperController.java b/src/main/java/leonardo/labutilities/qualitylabpro/controllers/analytics/AnalyticsHelperController.java index e90316c..7d5497d 100644 --- a/src/main/java/leonardo/labutilities/qualitylabpro/controllers/analytics/AnalyticsHelperController.java +++ b/src/main/java/leonardo/labutilities/qualitylabpro/controllers/analytics/AnalyticsHelperController.java @@ -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 getAnalyticsById(@PathVariable Long id) { - return ResponseEntity.ok(analyticsHelperService.findOneById(id)); - } - public ResponseEntity>> getAllAnalyticsWithLinks(List names, Pageable pageable) { - Page 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>> - getAnalyticsByDateBetweenWithLinks - (List names, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable) { - - Page 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 createEntityModel(AnalyticsRecord record, Pageable pageable) { - return EntityModel.of(record, - linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel()); - } - - private CollectionModel> addPaginationLinks( - CollectionModel> collectionModel, - Page 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 getAnalyticsById(@PathVariable Long id) { + return ResponseEntity.ok(analyticsHelperService.findOneById(id)); + } + + public ResponseEntity>> getAllAnalyticsWithLinks( + List names, Pageable pageable) { + Page 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>> getAnalyticsByDateBetweenWithLinks( + List names, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable) { + + Page 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 createEntityModel(AnalyticsRecord record, Pageable pageable) { + return EntityModel.of(record, + linkTo(methodOn(getClass()).getAnalyticsById(record.id())).withSelfRel()); + } + + private CollectionModel> addPaginationLinks( + CollectionModel> collectionModel, + Page 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; + } }