|
1 | 1 | import 'package:core/core.dart'; |
2 | 2 | import 'package:dart_frog/dart_frog.dart'; |
3 | | -import 'package:data_repository/data_repository.dart'; |
4 | 3 | 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'; |
6 | 5 | import 'package:logging/logging.dart'; |
7 | 6 |
|
8 | 7 | final _log = Logger('DataFetchMiddleware'); |
@@ -50,79 +49,30 @@ Middleware dataFetchMiddleware() { |
50 | 49 | } |
51 | 50 |
|
52 | 51 | /// Helper function to fetch an item from the correct repository based on the |
53 | | -/// model name. |
| 52 | +/// model name by using the [DataOperationRegistry]. |
54 | 53 | /// |
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. |
57 | 57 | /// |
58 | 58 | /// Throws [OperationFailedException] for unsupported model types. |
59 | 59 | Future<dynamic> _fetchItem( |
60 | 60 | RequestContext context, |
61 | 61 | String modelName, |
62 | 62 | String id, |
63 | 63 | ) 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 | | - |
70 | 64 | 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 | + ); |
125 | 73 | } |
| 74 | + |
| 75 | + return await fetcher(context, id); |
126 | 76 | } on NotFoundException { |
127 | 77 | // The repository will throw this if the item doesn't exist. |
128 | 78 | // We return null to let the main middleware handler throw a more |
|
0 commit comments