From 2463c4993a6300cd498d74c2912e08826611d340 Mon Sep 17 00:00:00 2001 From: TDeSain Date: Wed, 20 Nov 2024 13:23:14 -0500 Subject: [PATCH] Refactor service and controller to handle Optional returns - Refactor `DashboardDrawerService` methods to return `Optional` - Update controller methods to handle `Optional` results with appropriate HTTP responses - Modify repository methods to return `Optional` for single item queries - Ensure consistency and error handling improvements across the service and controller layers --- .../DashboardDrawerController.java | 4 ++-- .../DashboardDrawerRepository.java | 6 +++--- .../DashboardDrawerService.java | 20 ++++++++----------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerController.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerController.java index 162b4e0..e94c058 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerController.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerController.java @@ -14,11 +14,11 @@ public class DashboardDrawerController { @GetMapping public ResponseEntity findAll() { - return ResponseEntity.ok(dashboardDrawerService.findAll()); + return dashboardDrawerService.findAll().map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @GetMapping("/{id}") public ResponseEntity findByDatasetId(@PathVariable Integer id) { - return ResponseEntity.ok(dashboardDrawerService.findByDatasetId(id)); + return dashboardDrawerService.findByDatasetId(id).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } } diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerRepository.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerRepository.java index 1ea8e67..8f0d298 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerRepository.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerRepository.java @@ -49,7 +49,7 @@ public List getDashboardDrawerRows() { } } - public List getDashboardDrawerRows(Integer datasetId) { + public Optional getDashboardDrawerRows(Integer datasetId) { String materializedViewSql = """ select * from dictionary_db.dict.dataset_meta_materialized_view dmmv where dmmv.dataset_id = :datasetId; """; @@ -73,10 +73,10 @@ public List getDashboardDrawerRows(Integer datasetId) { params.addValue("datasetId", datasetId); try { - return template.query(materializedViewSql, params, new DashboardDrawerRowMapper()); + return template.query(materializedViewSql, params, new DashboardDrawerRowMapper()).stream().findFirst(); } catch (Exception e) { log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage()); - return template.query(fallbackSql, params, new DashboardDrawerRowMapper()); + return template.query(fallbackSql, params, new DashboardDrawerRowMapper()).stream().findFirst(); } } } diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerService.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerService.java index 577fe51..4fbe1d8 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerService.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboarddrawer/DashboardDrawerService.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Service public class DashboardDrawerService { @@ -24,13 +25,13 @@ public DashboardDrawerService(DashboardDrawerRepository repository, @Value("${da * * @return All Dashboard Instances and their metadata. */ - public DashboardDrawerList findAll() { + public Optional findAll() { if (dashboardLayout.equalsIgnoreCase("bdc")) { List records = repository.getDashboardDrawerRows(); - return new DashboardDrawerList(records); + return Optional.of(new DashboardDrawerList(records)); } - return new DashboardDrawerList(new ArrayList<>()); + return Optional.of(new DashboardDrawerList(new ArrayList<>())); } /** @@ -40,15 +41,10 @@ public DashboardDrawerList findAll() { * @param datasetId the ID of the dataset to fetch. * @return a single Dashboard instance with drawer-specific metadata. */ - public DashboardDrawer findByDatasetId(Integer datasetId) { - if (dashboardLayout.equalsIgnoreCase("bdc")) { - List records = repository.getDashboardDrawerRows(datasetId); - - if (records.size() == 1) { - return records.getFirst(); - } + public Optional findByDatasetId(Integer datasetId) { + if ("bdc".equalsIgnoreCase(dashboardLayout)) { + return repository.getDashboardDrawerRows(datasetId); } - - return new DashboardDrawer(-1, "", "", new ArrayList<>(), "", new ArrayList<>(), "", ""); + return Optional.empty(); } }