Skip to content

Commit 5da4e11

Browse files
committed
refactor(middlewares): replace switch statement with registry in data fetcher
- Introduce DataOperationRegistry for extensible data fetching - Remove direct dependency on DataRepository - Simplify _fetchItem function using registry lookup - Improve maintainability and extendability of data fetching logic
1 parent c2e5964 commit 5da4e11

File tree

1 file changed

+15
-65
lines changed

1 file changed

+15
-65
lines changed

lib/src/middlewares/data_fetch_middleware.dart

Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'package:core/core.dart';
22
import 'package:dart_frog/dart_frog.dart';
3-
import 'package:data_repository/data_repository.dart';
43
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart';
5-
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart';
4+
import 'package:flutter_news_app_api_server_full_source_code/src/registry/data_operation_registry.dart';
65
import 'package:logging/logging.dart';
76

87
final _log = Logger('DataFetchMiddleware');
@@ -50,79 +49,30 @@ Middleware dataFetchMiddleware() {
5049
}
5150

5251
/// Helper function to fetch an item from the correct repository based on the
53-
/// model name.
52+
/// model name by using the [DataOperationRegistry].
5453
///
55-
/// This function contains the switch statement that maps a `modelName` string
56-
/// to a specific `DataRepository` call.
54+
/// This function looks up the appropriate fetcher function from the registry
55+
/// and invokes it. This avoids a large `switch` statement and makes the
56+
/// system easily extensible.
5757
///
5858
/// Throws [OperationFailedException] for unsupported model types.
5959
Future<dynamic> _fetchItem(
6060
RequestContext context,
6161
String modelName,
6262
String id,
6363
) async {
64-
// The `userId` is not needed here because this middleware's purpose is to
65-
// fetch the item regardless of ownership. Ownership is checked in a
66-
// subsequent middleware. We pass `null` for `userId` to ensure we are
67-
// performing a global lookup for the item.
68-
const String? userId = null;
69-
7064
try {
71-
switch (modelName) {
72-
case 'headline':
73-
return await context.read<DataRepository<Headline>>().read(
74-
id: id,
75-
userId: userId,
76-
);
77-
case 'topic':
78-
return await context
79-
.read<DataRepository<Topic>>()
80-
.read(id: id, userId: userId);
81-
case 'source':
82-
return await context.read<DataRepository<Source>>().read(
83-
id: id,
84-
userId: userId,
85-
);
86-
case 'country':
87-
return await context.read<DataRepository<Country>>().read(
88-
id: id,
89-
userId: userId,
90-
);
91-
case 'language':
92-
return await context.read<DataRepository<Language>>().read(
93-
id: id,
94-
userId: userId,
95-
);
96-
case 'user':
97-
return await context
98-
.read<DataRepository<User>>()
99-
.read(id: id, userId: userId);
100-
case 'user_app_settings':
101-
return await context.read<DataRepository<UserAppSettings>>().read(
102-
id: id,
103-
userId: userId,
104-
);
105-
case 'user_content_preferences':
106-
return await context
107-
.read<DataRepository<UserContentPreferences>>()
108-
.read(
109-
id: id,
110-
userId: userId,
111-
);
112-
case 'remote_config':
113-
return await context.read<DataRepository<RemoteConfig>>().read(
114-
id: id,
115-
userId: userId,
116-
);
117-
case 'dashboard_summary':
118-
// This is a special case that doesn't use a standard repository.
119-
return await context.read<DashboardSummaryService>().getSummary();
120-
default:
121-
_log.warning('Unsupported model type "$modelName" for fetch operation.');
122-
throw OperationFailedException(
123-
'Unsupported model type "$modelName" for fetch operation.',
124-
);
65+
final registry = context.read<DataOperationRegistry>();
66+
final fetcher = registry.itemFetchers[modelName];
67+
68+
if (fetcher == null) {
69+
_log.warning('Unsupported model type "$modelName" for fetch operation.');
70+
throw OperationFailedException(
71+
'Unsupported model type "$modelName" for fetch operation.',
72+
);
12573
}
74+
75+
return await fetcher(context, id);
12676
} on NotFoundException {
12777
// The repository will throw this if the item doesn't exist.
12878
// We return null to let the main middleware handler throw a more

0 commit comments

Comments
 (0)