Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@

# OPTIONAL: Window for the /data API endpoints, in minutes.
# RATE_LIMIT_DATA_API_WINDOW_MINUTES=60

# OPTIONAL: The cache duration for the CountryQueryService, in minutes.
# Defaults to 15 minutes if not specified.
# COUNTRY_SERVICE_CACHE_MINUTES=15
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ analyzer:
avoid_catching_errors: ignore
document_ignores: ignore
one_member_abstracts: ignore
cascade_invocations: ignore
exclude:
- build/**
linter:
Expand Down
8 changes: 8 additions & 0 deletions lib/src/config/app_dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_news_app_api_server_full_source_code/src/config/environm
import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permission_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/auth_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/auth_token_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/country_query_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/database_seeding_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/default_user_preference_limit_service.dart';
Expand Down Expand Up @@ -69,6 +70,7 @@ class AppDependencies {
late final PermissionService permissionService;
late final UserPreferenceLimitService userPreferenceLimitService;
late final RateLimitService rateLimitService;
late final CountryQueryService countryQueryService;

/// Initializes all application dependencies.
///
Expand Down Expand Up @@ -238,6 +240,11 @@ class AppDependencies {
connectionManager: _mongoDbConnectionManager,
log: Logger('MongoDbRateLimitService'),
);
countryQueryService = CountryQueryService(
countryRepository: countryRepository,
log: Logger('CountryQueryService'),
cacheDuration: EnvironmentConfig.countryServiceCacheDuration,
);

_isInitialized = true;
_log.info('Application dependencies initialized successfully.');
Expand All @@ -255,6 +262,7 @@ class AppDependencies {
await _mongoDbConnectionManager.close();
tokenBlacklistService.dispose();
rateLimitService.dispose();
countryQueryService.dispose(); // Dispose the new service
_isInitialized = false;
_log.info('Application dependencies disposed.');
}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/config/environment_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,14 @@ abstract final class EnvironmentConfig {
int.tryParse(_env['RATE_LIMIT_DATA_API_WINDOW_MINUTES'] ?? '60') ?? 60;
return Duration(minutes: minutes);
}

/// Retrieves the cache duration in minutes for the CountryQueryService.
///
/// The value is read from the `COUNTRY_SERVICE_CACHE_MINUTES` environment
/// variable. Defaults to 15 minutes if not set or if parsing fails.
static Duration get countryServiceCacheDuration {
final minutes =
int.tryParse(_env['COUNTRY_SERVICE_CACHE_MINUTES'] ?? '15') ?? 15;
return Duration(minutes: minutes);
}
}
28 changes: 22 additions & 6 deletions lib/src/registry/data_operation_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:core/core.dart';
import 'package:dart_frog/dart_frog.dart';
import 'package:data_repository/data_repository.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/country_query_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart';

// --- Typedefs for Data Operations ---
Expand Down Expand Up @@ -128,12 +129,27 @@ class DataOperationRegistry {
sort: s,
pagination: p,
),
'country': (c, uid, f, s, p) => c.read<DataRepository<Country>>().readAll(
userId: uid,
filter: f,
sort: s,
pagination: p,
),
'country': (c, uid, f, s, p) async {
// Check for special filters that require aggregation.
if (f != null &&
(f.containsKey('hasActiveSources') ||
f.containsKey('hasActiveHeadlines'))) {
// Use the injected CountryQueryService for complex queries.
final countryQueryService = c.read<CountryQueryService>();
return countryQueryService.getFilteredCountries(
filter: f,
pagination: p,
sort: s,
);
}
// Fallback to standard readAll if no special filters are present.
return c.read<DataRepository<Country>>().readAll(
userId: uid,
filter: f,
sort: s,
pagination: p,
);
},
'language': (c, uid, f, s, p) => c
.read<DataRepository<Language>>()
.readAll(userId: uid, filter: f, sort: s, pagination: p),
Expand Down
Loading
Loading