-
Notifications
You must be signed in to change notification settings - Fork 0
Feature dashboard summary api handling #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7af82b0
e1187f5
4a06426
4133a82
be5b899
b1278a0
7973922
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:ht_data_repository/ht_data_repository.dart'; | ||
import 'package:ht_shared/ht_shared.dart'; | ||
|
||
/// {@template dashboard_summary_service} | ||
/// A service responsible for calculating the dashboard summary data on demand. | ||
/// | ||
/// This service aggregates data from various repositories to provide a | ||
/// real-time overview of key metrics in the system. | ||
/// {@endtemplate} | ||
class DashboardSummaryService { | ||
/// {@macro dashboard_summary_service} | ||
const DashboardSummaryService({ | ||
required HtDataRepository<Headline> headlineRepository, | ||
required HtDataRepository<Category> categoryRepository, | ||
required HtDataRepository<Source> sourceRepository, | ||
}) : _headlineRepository = headlineRepository, | ||
_categoryRepository = categoryRepository, | ||
_sourceRepository = sourceRepository; | ||
|
||
final HtDataRepository<Headline> _headlineRepository; | ||
final HtDataRepository<Category> _categoryRepository; | ||
final HtDataRepository<Source> _sourceRepository; | ||
|
||
/// Calculates and returns the current dashboard summary. | ||
/// | ||
/// This method fetches all items from the required repositories to count them | ||
/// and constructs a [DashboardSummary] object. | ||
Future<DashboardSummary> getSummary() async { | ||
// Use Future.wait to fetch all counts in parallel for efficiency. | ||
final results = await Future.wait([ | ||
_headlineRepository.readAll(), | ||
_categoryRepository.readAll(), | ||
_sourceRepository.readAll(), | ||
]); | ||
|
||
// The results are PaginatedResponse objects. | ||
final headlineResponse = results[0] as PaginatedResponse<Headline>; | ||
final categoryResponse = results[1] as PaginatedResponse<Category>; | ||
final sourceResponse = results[2] as PaginatedResponse<Source>; | ||
|
||
return DashboardSummary( | ||
id: 'dashboard_summary', // Fixed ID for the singleton summary | ||
headlineCount: headlineResponse.items.length, | ||
categoryCount: categoryResponse.items.length, | ||
sourceCount: sourceResponse.items.length, | ||
); | ||
Comment on lines
+30
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing the results of
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import 'package:ht_api/src/registry/model_registry.dart'; | |
import 'package:ht_api/src/services/auth_service.dart'; | ||
import 'package:ht_api/src/services/auth_token_service.dart'; | ||
import 'package:ht_api/src/services/default_user_preference_limit_service.dart'; | ||
import 'package:ht_api/src/services/dashboard_summary_service.dart'; | ||
import 'package:ht_api/src/services/jwt_auth_token_service.dart'; | ||
import 'package:ht_api/src/services/token_blacklist_service.dart'; | ||
import 'package:ht_api/src/services/user_preference_limit_service.dart'; | ||
|
@@ -175,6 +176,14 @@ Handler middleware(Handler handler) { | |
_createUserContentPreferencesRepository(); | ||
final appConfigRepository = _createAppConfigRepository(); | ||
|
||
// Instantiate the new DashboardSummaryService with its dependencies | ||
final dashboardSummaryService = DashboardSummaryService( | ||
headlineRepository: headlineRepository, | ||
categoryRepository: categoryRepository, | ||
sourceRepository: sourceRepository, | ||
); | ||
print('[MiddlewareSetup] DashboardSummaryService instantiated.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
const uuid = Uuid(); | ||
|
||
// --- Auth Dependencies --- | ||
|
@@ -308,6 +317,7 @@ Handler middleware(Handler handler) { | |
.use( | ||
provider<AuthService>((_) => authService), | ||
) // Reads other services/repos | ||
.use(provider<DashboardSummaryService>((_) => dashboardSummaryService)) | ||
// --- 5. RBAC Service Provider --- | ||
// PURPOSE: Provides the PermissionService for authorization checks. | ||
// ORDER: Must be provided before any middleware or handlers that use it | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import 'dart:io'; | |
import 'package:dart_frog/dart_frog.dart'; | ||
import 'package:ht_api/src/rbac/permission_service.dart'; | ||
import 'package:ht_api/src/registry/model_registry.dart'; | ||
import 'package:ht_api/src/services/dashboard_summary_service.dart'; | ||
import 'package:ht_api/src/services/user_preference_limit_service.dart'; // Import UserPreferenceLimitService | ||
import 'package:ht_data_repository/ht_data_repository.dart'; | ||
import 'package:ht_shared/ht_shared.dart'; | ||
|
@@ -122,6 +123,9 @@ Future<Response> _handleGet( | |
id: id, | ||
userId: userIdForRepoCall, | ||
); // userId should be null for AppConfig | ||
case 'dashboard_summary': | ||
final service = context.read<DashboardSummaryService>(); | ||
item = await service.getSummary(); | ||
Comment on lines
+126
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
default: | ||
// This case should ideally be caught by middleware, but added for safety | ||
// Throw an exception to be caught by the errorHandler | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getSummary
implementation fetches all items from multiple repositories to count them usingreadAll()
followed by.items.length
. This can be inefficient, especially as the number of headlines, categories, or sources grows. Consider introducing a dedicatedcount()
method to theHtDataRepository
interface to allow optimized implementations (e.g., usingCOUNT(*)
in a database).