Skip to content

Commit dbae817

Browse files
committed
refactor(api): use repository count method for dashboard summary
Replaces the `readAll()` calls in the `DashboardSummaryService` with the more efficient `count()` method from the data repository. This avoids fetching all documents from the database just to get their count, significantly improving the performance of the dashboard summary endpoint by leveraging the native counting capabilities of the database.
1 parent 5207475 commit dbae817

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

lib/src/services/dashboard_summary_service.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ class DashboardSummaryService {
2323

2424
/// Calculates and returns the current dashboard summary.
2525
///
26-
/// This method fetches all items from the required repositories to count them
27-
/// and constructs a [DashboardSummary] object.
26+
/// This method fetches the counts of all items from the required
27+
/// repositories and constructs a [DashboardSummary] object.
2828
Future<DashboardSummary> getSummary() async {
2929
// Use Future.wait to fetch all counts in parallel for efficiency.
3030
final results = await Future.wait([
31-
_headlineRepository.readAll(),
32-
_topicRepository.readAll(),
33-
_sourceRepository.readAll(),
31+
_headlineRepository.count(),
32+
_topicRepository.count(),
33+
_sourceRepository.count(),
3434
]);
3535

36-
// The results are PaginatedResponse objects.
37-
final headlineResponse = results[0] as PaginatedResponse<Headline>;
38-
final topicResponse = results[1] as PaginatedResponse<Topic>;
39-
final sourceResponse = results[2] as PaginatedResponse<Source>;
36+
// The results are integers.
37+
final headlineCount = results[0];
38+
final topicCount = results[1];
39+
final sourceCount = results[2];
4040

4141
return DashboardSummary(
4242
id: 'dashboard_summary', // Fixed ID for the singleton summary
43-
headlineCount: headlineResponse.items.length,
44-
topicCount: topicResponse.items.length,
45-
sourceCount: sourceResponse.items.length,
43+
headlineCount: headlineCount,
44+
topicCount: topicCount,
45+
sourceCount: sourceCount,
4646
);
4747
}
4848
}

0 commit comments

Comments
 (0)