Skip to content

Commit

Permalink
Refactor service and controller to handle Optional returns
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
TDeSain committed Nov 20, 2024
1 parent 6ad0322 commit 2463c49
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class DashboardDrawerController {

@GetMapping
public ResponseEntity<DashboardDrawerList> findAll() {
return ResponseEntity.ok(dashboardDrawerService.findAll());
return dashboardDrawerService.findAll().map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@GetMapping("/{id}")
public ResponseEntity<DashboardDrawer> findByDatasetId(@PathVariable Integer id) {
return ResponseEntity.ok(dashboardDrawerService.findByDatasetId(id));
return dashboardDrawerService.findByDatasetId(id).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<DashboardDrawer> getDashboardDrawerRows() {
}
}

public List<DashboardDrawer> getDashboardDrawerRows(Integer datasetId) {
public Optional<DashboardDrawer> getDashboardDrawerRows(Integer datasetId) {
String materializedViewSql = """
select * from dictionary_db.dict.dataset_meta_materialized_view dmmv where dmmv.dataset_id = :datasetId;
""";
Expand All @@ -73,10 +73,10 @@ public List<DashboardDrawer> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class DashboardDrawerService {
Expand All @@ -24,13 +25,13 @@ public DashboardDrawerService(DashboardDrawerRepository repository, @Value("${da
*
* @return All Dashboard Instances and their metadata.
*/
public DashboardDrawerList findAll() {
public Optional<DashboardDrawerList> findAll() {
if (dashboardLayout.equalsIgnoreCase("bdc")) {
List<DashboardDrawer> 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<>()));
}

/**
Expand All @@ -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<DashboardDrawer> records = repository.getDashboardDrawerRows(datasetId);

if (records.size() == 1) {
return records.getFirst();
}
public Optional<DashboardDrawer> findByDatasetId(Integer datasetId) {
if ("bdc".equalsIgnoreCase(dashboardLayout)) {
return repository.getDashboardDrawerRows(datasetId);
}

return new DashboardDrawer(-1, "", "", new ArrayList<>(), "", new ArrayList<>(), "", "");
return Optional.empty();
}
}

0 comments on commit 2463c49

Please sign in to comment.